WHU 1542 Countries (floyd)
题意:
在小明出生的星球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)的更多相关文章
- WOJ 1542 Countries 并查集转化新点+最短路
http://acm.whu.edu.cn/land/problem/detail?problem_id=1542 今天做武大的网赛题,哎,还是不够努力啊,只搞出三个 这个题目一看就是个最短路,但是题 ...
- [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)
Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...
- Countries in War -POJ3114Tarjan缩点+SPFA
Countries in War Time Limit: 1000MS Memory Limit: 65536K Description In the year 2050, after differe ...
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- UVa 104 - Arbitrage(Floyd动态规划)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- ZOJ1221 && UVA567:Risk(Floyd)
Risk is a board game in which several opposing players attempt to conquer the world. The gameboard c ...
- HDU 5418——Victor and World——————【状态压缩+floyd】
Victor and World Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Other ...
- uva 104 Arbitrage (DP + floyd)
uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...
- floyd算法学习笔记
算法思路 路径矩阵 通过一个图的权值矩阵求出它的每两点间的最短路径矩阵.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1):又 ...
随机推荐
- Android 对话框黑色边框的解决
代码解决 : Dialog dialog = new Dialog(this); Window win = dialog.getWindow(); win.setBackgroundDrawableR ...
- SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...
- 优动漫PAINT个人版绘制树叶教程
超详细树叶绘制法,更有配套绘树小TIPE!让你画树So Easy~一秒变身,画树达人! 优动漫PAINT个人版软件下载:http://www.dongmansoft.com/chanpin.html ...
- 基本数据类型(dict)
1.定义 dict => {"key":'value',"a":1} 字典是无序的,字典是可变的 字典的键 => 可哈希(不可变),唯一 字典的值 ...
- GDOI2017 再次酱油记
Day 0 13:00 pm 啊...今天中午一点钟从ez出发,感觉吼有趣啊.出发前先大喊一声****,在书包里放一本党史,感觉玄学可以救命[滑稽] 15:00 pm 到达东莞,坐标:石龙名冠金凯悦大 ...
- Cause of 400 Bad Request Errors
The 400 Bad Request error displays inside the Internet browser window, just as web pages do. Cause o ...
- Solr全文检索引擎配置及使用方法
介绍 Solr是一款开源的全文检索引擎,基于lucene.拥有完善的可配置功能界面.具有丰富的查询语言,可扩展,可优化. 下载安装 进入solr官网下载包(这里我使用的版本是8.0) http://w ...
- Zabbix分布式配置
Zabbix是一个分布式监控系统,它可以以一个中心点.多个分节点的模式运行,使用Proxy能大大的降低Zabbix Server的压力,Zabbix Proxy可以运行在独立的服务器上,安装Zabbi ...
- python 中进制转换及format(),int()函数用法
python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示: v_code=15 # 2进制 x=for ...
- HDU——T 2444 The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...