题意:

  在小明出生的星球X上有n国家。

  一些国家通过结盟而拥有良好的双边关系,因此他们的公民得益于这些政策,使得所有这些国家之间的旅行变得免费。

  但是,不同联盟之间的旅行就不是这么容易了。如果可能,它必须花一些钱。

  请帮小明计算出国家之间的最小花费。

  输入:

    输入包含多组样例。

    每组样例的第一行有两个整数 n 和 m 。(1<=n<=10^5, 1<=m<=10^5)

    接下来的m行,每行有x, y, c。 其中 c 表示 x, y之间的花费。如果c为0,则表示x和y属于同一个联盟。(1<=x, y<=n, 0<=c<=10^9)

    你可以假设两个国家之间没有多于一条路。

    然后下一行是一个整数q,代表有多少个询问。(1<=q<=200)

    接下来有q行,每行包括x, y。(1<=x, y<=n)

    保证不会超过两百个联盟。

    当输入n=0时,结束。

  输出:

    对于每组输入,每个询问输出一行。如果不可能,输出"-1"。

思路:

  对于同一个联盟的国家,我们可以把他们看成一个国家,因为他们一定是连起来的。 然后跑floyd。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const int MAXN = (int) 1e5+;
const int MAXC = ; struct Edge{
int x, y;
ll c;
}a[MAXN]; int p[MAXN];
int f[MAXN];
ll d[MAXC][MAXC];
int n, m, q;
int cnt; int Find(int x) {
int res = x;
int t; while (f[res]!=res) res = f[res];
while (x!=res) {
t = x;
x = f[x];
f[t] = res;
}
return res;
} void Union(int x, int y) {
f[Find(x)] = Find(y);
} void floyd() {
for (int k = ; k < cnt; k++)
for (int i = ; i < cnt; i++)
for (int j = ; j < cnt; j++)
if (-!=d[i][k] && -!=d[k][j]) {
if (-==d[i][j]) {
d[i][j] = d[i][k]+d[k][j];
} else
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
} int main() {
#ifdef Phantom01
freopen("WHU1542.txt", "r", stdin);
#endif // Phantom01 while (scanf("%d", &n)!=EOF) {
if (==n) break;
scanf("%d", &m);
cnt = ;
for (int i = ; i <= n; i++)
f[i] = i;
for (int i = ; i < m; i++) {
scanf("%d%d%lld", &a[i].x, &a[i].y, &a[i].c);
if ( == a[i].c) Union(a[i].x, a[i].y);
}
for (int i = ; i <= n; i++) {
if (i==f[i]) //如果是根,
p[i] = cnt++;
}
for (int i = ; i < cnt; i++) {
for (int j = ; j < cnt; j++)
d[i][j] = -;
d[i][i] = ;
}
int x, y;
for (int i = ; i < m; i++) {
x = p[Find(a[i].x)];
y = p[Find(a[i].y)];
d[x][y] = -==d[x][y] ? a[i].c : min(d[x][y], a[i].c);
d[y][x] = -==d[y][x] ? a[i].c : min(d[y][x], a[i].c);
}
floyd();
scanf("%d", &q);
for (int i = ; i < q; i++) {
scanf("%d%d", &x, &y);
printf("%lld\n", d[p[Find(x)]][p[Find(y)]]);
}
} return ;
}

  

WHU 1542 Countries (floyd)的更多相关文章

  1. WOJ 1542 Countries 并查集转化新点+最短路

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1542 今天做武大的网赛题,哎,还是不够努力啊,只搞出三个 这个题目一看就是个最短路,但是题 ...

  2. [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)

    Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...

  3. Countries in War -POJ3114Tarjan缩点+SPFA

    Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...

  4. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  5. UVa 104 - Arbitrage(Floyd动态规划)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. ZOJ1221 && UVA567:Risk(Floyd)

    Risk is a board game in which several opposing players attempt to conquer the world. The gameboard c ...

  7. HDU 5418——Victor and World——————【状态压缩+floyd】

    Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Other ...

  8. uva 104 Arbitrage (DP + floyd)

    uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...

  9. floyd算法学习笔记

    算法思路 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1):又 ...

随机推荐

  1. 51nod 1572 宝岛地图 (预处理四个方向的最大步数优化时间,时间复杂度O(n*m+k))

    题目: 这题如果没有时间限制的话暴力可以解,暴力的话时间复杂度大概是O(k*n),1s的话非常悬. 所以我们需要换个思路,我们对每个点预处理四个方向最多能走的步数,这个预处理时间复杂度是O(n*m). ...

  2. Devexpress控件使用二:barManager

    1.拖放控件 2.两种按钮显示形式 1)上面是大图标,下面是说明 a.Add → Largebutton 注:勾选 Show DesignTime enancements 才会出现Add b.添加图片 ...

  3. HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.

    1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this: // Be ...

  4. 【原创】关于not in的一些事情

    早上到公司,收到一条cocall消息,是某哥们遇到的疑惑,可能很多新手并不知情: 请教个问题 我执行 . select * from t_htgl_htpswj t where t.c_wjmc = ...

  5. 压缩图片C#算法

    转载自 http://www.open-open.com/lib/view/open1391348644910.html using System.IO; using System.Drawing; ...

  6. hdu1010 - dfs,奇偶剪枝

    题目链接 给一个迷宫,问从起点到终点存不存在一条长度为T的路径. ------------------------------------------------------------------- ...

  7. HDU 4190 Distributing Ballot Boxes【二分答案】

    题意:给出n个城市,n个城市分别的居民,m个盒子,为了让每个人都投上票,问每个盒子应该装多少张票 二分盒子装的票数, 如果mid<=m,说明偏大了,r应该向下逼近 ,r=mid 如果mid> ...

  8. 获取新浪微博的Access_token

    最近想爬取新浪微博的评论,百度了一下,有个新浪开放平台提供了这个API 于是按照它的说明,去获取Access_token: 1.点击微链接 2.立即创建微链接 3.选择网页应用 4.填写信息后提交 5 ...

  9. 栈(stack)--c实现(使用双链表)

    是不是直接贴代码不太好,我竟然不知道说什么. 写这个考虑的问题,或者是纠结的问题是这个头指针怎么处理,也就是栈的顶部,最后采用的是初始化第一个栈空间浪费掉,栈顶是有元素的.好像应该去学习下画图,没图不 ...

  10. numpy基础篇-简单入门教程4

    np.set_printoptions(precision=3),只显示小数点后三位 np.random.seed(100) rand_arr = np.random.random([2, 2]) n ...