A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the roads in the shortest route: some roads may have obstacles (e.g. bridge overpass, tunnels) which limit heights of the goods transported. Therefore, the company would like to transport as much as possible each trip, and then choose the shortest route that can be used to transport that amount.
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded. Input
The input consists of a number of cases. Each case starts with two integers, separated by a space, on a line. These two integers are the number of cities (C) and the number of roads (R). There are at most 1000 cities, numbered from 1. This is followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allowed on that road, and the length of that road. The maximum height for each road is a positive integer, except that a height of -1 indicates that there is no height limit on that road. The length of each road is a positive integer at most 1000. Every road can be travelled in both directions, and there is at most one road connecting each distinct pair of cities. Finally, the last line of each case consists of the start and end city numbers, as well as the height limit (a positive integer) of the cargo truck. The input terminates when C = R = 0.
Output
For each case, print the case number followed by the maximum height of the cargo truck allowed and the length of the shortest route. Use the format as shown in the sample output. If it is not possible to reach the end city from the start city, print "cannot reach destination" after the case number. Print a blank line between the output of the cases.
Sample Input
Sample Output
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded. Input
5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 10 5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 4 3 1 1 2 -1 100 1 3 10 0 0
Case 1: maximum height = 7 length of shortest route = 20 Case 2: maximum height = 4 length of shortest route = 8 Case 3: cannot reach destination
题意:给你两个点x,y;要你求从x到y这一路径中最大的速度于最小速度的查,每一条边都会给你速度;
将每条路的速度进行排序,选定第i小的边作为生成树的第一条边,然后从此边开始不断加入树中,知道x和y在一个集合中了,不断更新差就能得到最小值了
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct node{int a,b,w;}eg[1010]; bool cmp(node x,node y){return x.w<y.w;} int fa[230]; int n,m; void init() { for(int i=0;i<=n;i++)fa[i]=i; } int findfa(int x) { return x==fa[x]?x:fa[x]=findfa(fa[x]); } void mer(int a,int b) { int pa=findfa(a); int pb=findfa(b); if(pa!=pb)fa[pb]=pa; } int main() { while(cin>>n>>m) { for(int i=0;i<m;i++) { cin>>eg[i].a>>eg[i].b>>eg[i].w; } sort(eg,eg+m,cmp); int Q;cin>>Q; for(int t=0;t<Q;t++) { int u,v;cin>>u>>v; int ans=INT_MAX; for(int i=0;i<m;i++) { init(); for(int j=i;j<m;j++) { mer(eg[j].a,eg[j].b); if(findfa(v)==findfa(u)) { ans=min(ans,eg[j].w-eg[i].w); break; }}} if(ans==INT_MAX)printf("-1\n"); else printf("%d\n",ans); } } }看来就算是图论中,如果涉及到最大和最小的问题也适合将数据排序再求解,而Kruskal就是这样的一种算法