题意:

  在小明出生的星球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. python 3.x 学习笔记12 (反射 and 异常)

    1.反射通过字符串映射或修改程序运行时的状态.属性.方法 getattr(obj,name_str):  根据字符串name_str去获取obj对象里的对应的方法的内存地址 hasttr(obj,na ...

  2. 移动端font-size适配

    html{font-size:10px} @media screen and (min-width:321px) and (max-width:375px){html{font-size:11px}} ...

  3. Concurrency pattern

    In software engineering, concurrency patterns are those types of design patterns that deal with the  ...

  4. ActiveMQ学习笔记(3)----JMS的可靠性机制

    1. 消息接收确认 JMS消息只有在被确认之后,才认为已经被成功的消费了,消息成功消费通常包含三个阶段:客户接收消息,客户处理消息和消息被确认. 在事务性会话中,当一个事务被提交的时候,确认自动发生. ...

  5. 洛谷P2045 方格取数加强版 最小费用流

    Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...

  6. LCT笔记

    先存个代码 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...

  7. tinymce原装插件源码分析(三)-code

    code: 用于显示源码.主要包含一个弹框.设置显示内容以及内容的更新. function showDialog() { var win = editor.windowManager.open({ t ...

  8. HDU-4370 '0 or 1' 最短路 要考虑连通性

    题目链接:https://cn.vjudge.net/problem/HDU-4370 题意 给一个矩阵C(nn),要我们找到一个矩阵X(nn),满足以下条件: X_{12}+X_{13}+...X_ ...

  9. Vue style里面使用@import引入外部css, 作用域是全局的解决方案

    问题描述 使用@import引入外部css,作用域却是全局的 <template> </template> <script> export default { na ...

  10. Android群英传-拼图游戏puzzle-代码设计和实现

    上个周末,3个小时总体上读完了<Android群英传>,本周主要在研究代码层次的设计和实现.  编译安装在手机上,玩了几把,结合代码,一周时间才掌握了整体的思路.  大部分时间,其实花在了 ...