uva 10099 The Tourist Guide
2015-08-14 12:58
1386 查看
原题
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 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, C2and P. C1and C2are 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 and T 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 10
1 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 #1
Minimum Number of Trips = 5
题目大意
给你个图,然后给你起点和终点和人数,问你导游把这群人从起点带到终点得用多少次。(坑)导游也算一个人,导游回来不算次数,导游最后也得到终点。
思路见代码向下方
代码1
代码2
[b]思路
这题真是天坑啊,我最先写的代码1,首先找出这个图的最大生成树,然后就能得到从起点到到终点一个唯一最优路径。然后用深度优先搜索和栈记录路径节点,结果一顿wa。后来写了代码2,用floyed,速度虽然慢了一点,但还是wa。醉了,自己搞了n个数据都过了,还是没用,最后上网上找发现最后一定要加一个换行,否则就wa。加个换行以后就都过了~~ 晕
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 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, C2and P. C1and C2are 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 and T 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 10
1 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 #1
Minimum Number of Trips = 5
题目大意
给你个图,然后给你起点和终点和人数,问你导游把这群人从起点带到终点得用多少次。(坑)导游也算一个人,导游回来不算次数,导游最后也得到终点。
思路见代码向下方
代码1
[code]#include<iostream> #include<algorithm> #include<map> #include<string> #include<cstring> #include<sstream> #include<cstdio> #include<vector> #include<cmath> #include<stack> using namespace std; const int N=102; const int maxint=9999999; struct edge { int u,v,cost; }; edge es[10001]; int Matrix ; bool mark ; stack<int> si; bool cmp(const edge &e1,const edge &e2) { return e1.cost>e2.cost; } int n,r; int c1,c2,p; int s,d,t,res; int father ,rank ; void ini(int x) { res=maxint; memset(mark,0,sizeof(mark)); for(int i=1;i<=x;i++) { father[i]=i; rank[i]=0; for(int j=1;j<=x;j++) Matrix[i][j]=maxint; } } int Find(int x) { if(father[x]==x) return x; else return father[x]=Find(father[x]); } bool merge(int a,int b) { int x=Find(a); int y=Find(b); if(x==y) return false; if(rank[x]<rank[y]) { father[x]=y; } else { father[y]=x; if(rank[x]==rank[y]) rank[y]++; } return true; } void kruskal(int x) { sort(es+1,es+1+x,cmp); for(int i=1;i<=x;i++) { edge e=es[i]; if(merge(e.v,e.u)) Matrix[e.v][e.u]=Matrix[e.u][e.v]=e.cost; } } bool dfs(int x) { if(x==d) return true; for(int i=1;i<=n;i++) { if(Matrix[x][i]!=maxint&&!mark[x][i]&&i!=x) { mark[x][i]=mark[i][x]=true; si.push(Matrix[x][i]); if(dfs(i)) return true; else { si.pop(); } } } return false; } int main() { ios::sync_with_stdio(false); int k=1; while(cin>>n>>r) { while(!si.empty()) si.pop(); if(n+r==0) break; for(int i=1;i<=r;i++) { cin>>c1>>c2>>p; es[i].u=c1;es[i].v=c2;es[i].cost=p; } ini(n); cin>>s>>d>>t; kruskal(r); /* for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) cout<<Matrix[i][j]<<" "; cout<<endl; }*/ dfs(s); while(!si.empty()) { res=min(res,si.top()); si.pop(); } res--; if(t%res) { cout<<"Scenario #"<<k++<<endl; cout<<"Minimum Number of Trips = "<<t/res+1<<endl; } else { cout<<"Scenario #"<<k++<<endl; cout<<"Minimum Number of Trips = "<<t/res<<endl; } cout<<endl; } return 0; }
代码2
[code]#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<cstring> #include<string> #include<cmath> #include <iomanip> #include<queue> #include<cstring> #include<sstream> #include<map> using namespace std; const int N=103; const int maxint=999999; int Matrix ; int n,r; int c1,c2,p; int s,t,d; int floyed(int x,int a,int b) { int res=maxint; for(int k=1;k<=x;k++) { for(int i=1;i<=x;i++) { for(int j=1;j<=x;j++) { Matrix[i][j]=max(Matrix[i][j],min(Matrix[i][k],Matrix[k][j])); } } } return Matrix[a]; } int main() { ios::sync_with_stdio(false); int k=1; while(cin>>n>>r,n+r) { memset(Matrix,0,sizeof(Matrix)); for(int i=1;i<=r;i++) { cin>>c1>>c2>>p; Matrix[c1][c2]=Matrix[c2][c1]=p; } cin>>s>>d>>t; int ans=floyed(n,s,d); ans--; if(t%ans) { cout<<"Scenario #"<<k++<<endl; cout<<"Minimum Number of Trips = "<<t/ans+1<<endl; } else { cout<<"Scenario #"<<k++<<endl; cout<<"Minimum Number of Trips = "<<t/ans<<endl; } cout<<endl; } return 0; }
[b]思路
这题真是天坑啊,我最先写的代码1,首先找出这个图的最大生成树,然后就能得到从起点到到终点一个唯一最优路径。然后用深度优先搜索和栈记录路径节点,结果一顿wa。后来写了代码2,用floyed,速度虽然慢了一点,但还是wa。醉了,自己搞了n个数据都过了,还是没用,最后上网上找发现最后一定要加一个换行,否则就wa。加个换行以后就都过了~~ 晕
相关文章推荐
- apue.h头文件(UNIX环境高级编程)
- apue.h头文件(UNIX环境高级编程)
- Codeforces 559B Equivalent Strings 想法题
- BIGINT UNSIGNED value is out of range in..的解决方法
- FIneCMS /dayrui/libraries/Chart/ofc_upload_image.php Arbitrary File Upload Vul
- UVA 题目1521 GCD Guessing Game(数论+贪心)
- poj 2299 Ultra-QuickSort(树状数组)
- xcode工程文件格式(Xcode Project File Format)详解
- php require_once 的使用及与require的区别
- request对象的几个常用方法
- Win7安vc2008编译报LINK : fatal error LNK1000: Internal error during IncrBuildImage
- No enclosing instance of type WallpaperService is available due to some intermediate constructor inv
- 封装同步的UIActionSheet
- poj 3080 Blue Jeans
- Android.mk prebuild
- LeetCode:Unique Binary Search Trees Ⅱ
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
- 使用4.3.11版本的hibernate结合druid连接池遇到的问题
- POJ 2533 Longest Ordered Subsequence
- easyui 密码框验证