UVa 10099 - The Tourist Guide(Floyd, 最大生成树)
来源:互联网 发布:美的网络专供 质量 编辑:程序博客网 时间:2024/06/05 16:37
链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1040
题目:
Problem D
The Tourist Guide
Input: standard input
Output: standard output
Mr. G. works as a tourist guide. His current assignment is to take some tourists from one city to another. Some two-way roads connect the cities. For each pair of neighboring cities there is a bus service that runs only between those two cities and uses the road that directly connects them. Each bus service has a limit on the maximum number of passengers it can carry. Mr. G. has a map showing the cities and the roads connecting them. He also has the information regarding each bus service. He understands that it may not always be possible for him to take all the tourists to the destination city in a single trip. For example, consider the following road map of 7 cities. The edges connecting the cities represent the roads and the number written on each edge indicates the passenger limit of the bus service that runs on that road.
Now, if he wants to take 99 tourists from city 1 to city 7, he will require at least 5 trips, since he has to ride the bus with each group, and the route he should take is : 1 - 2 - 4 - 7.
But, Mr. G. finds it difficult to find the best route all by himself so that he may be able to take all the tourists to the destination city in minimum number of trips. So, he seeks your help.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: N (N<= 100) and R representing respectively the number of cities and the number of road segments. Then R lines will follow each containing three integers: C1, C2 andP. C1 and C2 are the city numbers and P (P> 1) is the limit on the maximum number of passengers to be carried by the bus service between the two cities. City numbers are positive integers ranging from 1 to N. The (R + 1)-th line will contain three integers: S, D andT representing respectively the starting city, the destination city and the number of tourists to be guided.
The input will end with two zeroes for N and R.
Output
For each test case in the input first output the scenario number. Then output the minimum number of trips required for this case on a separate line. Print a blank line after the output of each test case.
Sample Input
7 101 2 30
1 3 15
1 4 10
2 4 25
2 5 60
3 4 40
3 6 20
4 7 35
5 7 20
6 7 30
1 7 99
0 0
Sample Output
Scenario #1Minimum Number of Trips = 5
分析与总结:
和上一题UVa 10048 - Audiophobia(Floyd, Kruskal)基本类似, a到b的所有路径,每条路径上都有一个最小的权值x, 求所有路径中的最大的x。
求出x之后,要算出一共要走几个来回,每个来回带一组人到目标,那么只需要直接 T/(x-1)【向上取整】,便得到答案, T为导游所带的总人数。
特别需要注意是每一趟带的人数是x-1, 因为导游也要占一个位置。
代码:
1.Kruskal方法
#include<cstdio>#include<algorithm>#define N 10005using namespace std;int n,m,beg,end,limit,f[N],rank[N];struct Edge{ int u,v,val; friend bool operator<(const Edge&a,const Edge&b){ return a.val>b.val; }}arr[N];inline void init(){ for(int i=0; i<=n; ++i) f[i]=i,rank[i]=0;}int find(int x){ int i,j=x; while(j!=f[j]) j=f[j]; while(x!=j){ i=f[x]; f[x]=j; x=i; } return j;}bool Union(int x,int y){ int a=find(x),b=find(y); if(a==b)return false; if(rank[a]>rank[b]) f[b]=a; else{ if(rank[a]==rank[b]) ++rank[b]; f[a]=b; } return true;}int main(){ int a,b,c,cas=1; while(~scanf("%d%d",&n,&m)&&n+m){ for(int i=0; i<m; ++i){ scanf("%d%d%d",&a,&b,&c); arr[i].u=a,arr[i].v=b,arr[i].val=c; } scanf("%d%d%d",&beg,&end,&limit); init(); sort(arr,arr+m); int ans; for(int i=0; i<m; ++i){ Union(arr[i].u,arr[i].v); a=find(beg), b=find(end); if(a==b){ ans = arr[i].val; break; } } int t=(limit%(ans-1)==0)?(limit/(ans-1)):(limit/(ans-1)+1); printf("Scenario #%d\n", cas++); printf("Minimum Number of Trips = %d\n\n",t); } return 0;}
2. Floyd
#include<cstdio>#include<algorithm>const int N = 105;const int INF = 1000000000;using namespace std;int n,m,beg,end,limit,d[N][N];inline void read_graph(){ for(int i=1; i<=n; ++i){ d[i][i] = 0; for(int j=i+1; j<=n; ++j) d[i][j]=d[j][i]=INF; } int a,b,c; for(int i=0; i<m; ++i){ scanf("%d%d%d",&a,&b,&c); d[a][b]=d[b][a]=c; }}inline void Floyd(){ for(int k=1; k<=n; ++k) for(int i=1; i<=n; ++i){ for(int j=1; j<=n; ++j){ int tmp = min(d[i][k], d[k][j]); if(d[i][j]==INF) d[i][j] = tmp; else d[i][j] = max(d[i][j],tmp); } }}int main(){ int a,b,c,cas=1; while(~scanf("%d%d",&n,&m)&&n+m){ read_graph(); scanf("%d%d%d",&beg,&end,&limit); Floyd(); int ans = d[beg][end]; int t=(limit%(ans-1)==0)?(limit/(ans-1)):(limit/(ans-1)+1); printf("Scenario #%d\n", cas++); printf("Minimum Number of Trips = %d\n\n",t); } return 0;}
—— 生命的意义,在于赋予它意义。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)
- UVa 10099 - The Tourist Guide(Floyd, 最大生成树)
- UVA 10099 - The Tourist Guide(floyd)
- The Tourist Guide - UVa 10099 Floyd
- UVA 10099The Tourist Guide【floyd】
- uva 10099 - The Tourist Guide(最大生成树, 最小边最大)
- UVa 10099 The Tourist Guide (最大生成树+最小边的最大化+Kruskal)
- uva 10099 - The Tourist Guide(最小生成树)
- uva 10099 The Tourist Guide(最“大”生成树)
- 10099 - The Tourist Guide//floyd
- UVA - 10099 The Tourist Guide (floyd+dp)
- uva 10099 The Tourist Guide
- UVa 10099 The Tourist Guide
- Uva 10099 - The Tourist Guide
- UVA 10099 - The Tourist Guide
- UVa:10099 The Tourist Guide
- UVA - 10099 The Tourist Guide
- UVa 10099 - The Tourist Guide
- UVA 10099 The Tourist Guide
- 机器学习中的数学(5)-强大的矩阵奇异值分解(SVD)及其应用
- iPhone开发--AVAudionPlayer支持后台播放
- dul实验(expdp)
- 2012年校园招聘华为一道面试题---之我的解法
- Present()
- UVa 10099 - The Tourist Guide(Floyd, 最大生成树)
- 数据结构之红黑树
- Emacs和Vim:神的编辑器和编辑器之神(ZZ)
- C/C++内存泄漏及检测
- iphone开发者证书下载 安装
- vim只能粘贴50行的问题
- 把SATA模式由IDE模式改为AHCI引起系统启动蓝屏问题的解决方法
- c++ 实现static const对象在类声明中指定初值
- iOS开发:安装真机调试证书