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):又 ...
随机推荐
- navigator.clipboard 浏览器原生剪贴板
浏览器原生剪贴板 navigator.clipboard 写入 navigator.clipboard.writeText navigator.clipboard.writeText('Linr Te ...
- (转载)RecyclerView之ItemDecoration由浅入深
RecyclerView之ItemDecoration由浅入深 作者 小武站台 关注 2016.09.19 18:20 字数 1155 阅读 10480评论 15喜欢 91赞赏 3 译文的GitHub ...
- POJ 1182 食物链 (并查集解法)(详细注释)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78510 Accepted: 23396 Description ...
- hiho1080 - 数据结构 线段树(入门题,两个lazy tag)
题目链接 维护区间和,两个操作:一个是将某个区间设置成一个值,一个是将某个区间增加一个固定值 /**************************************************** ...
- BZOJ 2555: SubString 后缀自动机_LCT
很水的一道题,就是有些细节没注意到. 比如说将调试信息误以为是最终结果而多调了20分钟QAQ ..... 我们注意到,每新加一个节点,改变的是该节点沿着 Parent 走一直走到根节点. 对应的,在 ...
- 洛谷P1494 [国家集训队]小Z的袜子
Code: #include<cstdio> #include<iostream> #include<algorithm> #include<cstring& ...
- scrapy框架学习
一.初窥Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为了 页面抓取 (更确切来说, 网 ...
- Springboot+swagger2的接口文档开发
一.创建一个SpringBoot项目 1. 2. 3. 4. 把web里的web选中,SQL里选择自己需要的,点击next 二.创建各项所需的controller,configure等 1. 项目布局 ...
- vue非父子组件间传参问题
最近在使用vue进行开发,遇到了组件之间传参的问题,此处主要是针对非父子组件之间的传参问题进行总结,方法如下:一.如果两个组件用友共同的父组件,即 FatherComponent.vue代码 < ...
- pythonweb django的学习
Django 环境搭建及创建项目 首先安装django包,我使用的是pycharm,所以直接在IDE中就可以直接安装,但是django还需要手动配置系统变量 找到python根目录下的django文件 ...