Hrbeu 1209
来源:互联网 发布:艾灸淘宝推广文章 编辑:程序博客网 时间:2024/05/16 09:11
题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1209
Go To Play
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 343 Accepted: 131
Description
There are N(N<=50) good friends. One day they want to play together in one of their home. There may be road between two houses. We can describe a house as an vertex and a road as a edge between two vertexes. Every one goes at the same speed. At the very beginning, everyone stays at their own home. Now they want to play together as early as possible, that's to say they expect the latest one reach destination as early as possible. But they don't know whose house to get together in, can you help them?
Input
Input contains multiple cases. The first line is a integer T representing the number of test cases.The first line of each case contains two integers N and M. Then M lines follow. Each of the M lines contains three integers a, b, and c,which means that vertex a and b exist a edge whose distance is c.
Output
For each test case find which vertex to get together. If there is no such vertex print a single line "No solution." instead.
Sample Input
3
3 2
0 1 10
1 2 10
4 3
0 1 10
1 2 10
2 3 10
4 2
0 1 5
2 3 7
Sample Output
1
1
No solution.
首先判断连通性,如果不连通输出“No solution.”,注意后面还有个“.”,坑在这好长时间。
之后用floyd求一遍最短路,最后再扫一遍就行了。
/************************************************************************* > File Name: HEU/1209.cpp > Author: magicyang > Mail:273868471@qq.com > Created Time: 2014年08月30日 星期六 13时24分41秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#include<vector>using namespace std;int parent[55];int dist[55][55];const int maxn=1000000000;int Find(int x){ if(parent[x]==x) return x; else return parent[x]=Find(parent[x]);}void start(int n){ for(int i=0;i<=n;i++) parent[i]=i;}void floyd(int n){ for(int k=0;k<n;k++) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(dist[i][j]>dist[i][k]+dist[k][j]) dist[i][j]=dist[i][k]+dist[k][j]; } } }}int check(int n){ int sum=0; for(int i=0;i<n;i++) { if(parent[i]==i) sum++; } if(sum>1) return 1; else return 0;}int main(){ int t; cin>>t; while(t--) { int n,m; cin>>n>>m; start(n); for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { if(i==j) dist[i][j]=0; else dist[i][j]=maxn; } } for(int i=1;i<=m;i++) { int x,y,z; cin>>x>>y>>z; int xx=Find(x),yy=Find(y); if(xx!=yy) parent[xx]=yy; if(dist[x][y]>z) { dist[x][y]=z; dist[y][x]=z; } } if(check(n)) cout<<"No solution."<<endl; else { floyd(n); /*for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cout<<dist[i][j]<<" "; cout<<endl; }*/ int ans=0; int minn=maxn; for(int i=0;i<n;i++) { int maxx=0; for(int j=0;j<n;j++) { if(dist[i][j]>maxx) maxx=dist[i][j]; } if(maxx<minn) { minn=maxx; ans=i; } } cout<<ans<<endl; } }}
- Hrbeu 1209
- HRBEU 占武卖花
- Hrbeu 1255
- Hrbeu 1039
- HRBEU 1121 二分枚举
- HRBEU 油田合并
- HRBEU/HEUOJ----1015 Arctic Network
- HRBEU/HEUOJ----1059 France '98
- hrbeu 第四次周赛简要题解
- HRBEU 石头剪刀布1
- hrbeu Kth Largest (二重二分)
- ACM Hrbeu OJ 1208 Arrangement
- HRBEU/HEUOJ----1018 Temple of Dune
- HRBEU/HEUOJ----1019 Ferry Loading II
- HRBEU 1402 Valid date format 模拟题
- HRBEU 1392 朋友相会 SPFA 最短路
- 基础算法题解(hrbeu OJ)
- ACM Hrbeu OJ 1001 Forests || POJ 2419
- Win7下qt5.3.1+opencv2.4.9编译环境的搭建
- C语言 const类型变量
- 简单计算题!我就是笨!
- 品尝人生之苦:褚时健与他的褚橙
- iOS iphone5屏幕适配 autosizing [此博文包含图片]
- Hrbeu 1209
- javap的基本用法
- ios7状态栏以及导航栏
- STL array的operator[]方法(15)
- const 和 extern 用法小結
- UF超滤膜:超滤膜在酒店行业的应用
- 保护好大脑的海马区,老了也不怕健忘
- 【IOS 开发】Object - C 入门 之 数据类型详解
- wikioi 1029 中序遍历总数