ZOJ Problem Set - 1952

来源:互联网 发布:全方位安全软件 编辑:程序博客网 时间:2024/05/22 05:20

Heavy Cargo

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.

Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.


Input

The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2 <= n <= 200) and the number of road segments r (1 <= r <= 19900) making up the street network. 

Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions. 

The last line of the test case contains two city names: start and destination. 

Input will be terminated by two values of 0 for n and r.


Output

For each test case, print three lines:

  • a line saying "Scenario #x" where x is the number of the test case
  • a line saying "y tons" where y is the maximum possible load
  • a blank line


Sample Input

4 3
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Muenchen
5 5
Karlsruhe Stuttgart 100
Stuttgart Ulm 80
Ulm Muenchen 120
Karlsruhe Hamburg 220
Hamburg Muenchen 170
Muenchen Karlsruhe
0 0


Sample Output

Scenario #1
80 tons

Scenario #2
170 tons


这题是路径最小值最大化,起初我看着怎么这么像最小瓶颈树,后来想了想最小瓶颈树刚好和它反过来,即求路径的最大值最小化。这两个够像兄弟的,但算法却不一样。前者用floyd,后者用kruskal或者prim。

这里只要稍加修改floyd中的式子即可,adj[i][j]=max(adj[i][j],min(adj[i][k],adj[k][j]));

注意adj[i][j]原先可能就有值,这个也算作一种可能。此时插入k,整条路的最小值取决于min(adj[i][k],adj[k][j]),这又是新的一种可能,对于不同可能都是一种解的方案,因此只需求max即可。这里仍然需要注意初始化

adj[i][j](i==j)为无穷大,至于为什么,还是空间优化的原因,之前我发过的一篇有提到过。那么最初没有连边的应初始化为什么呢?显然因为后面要取max,因此这个值要比所有解都来得小,可以选0或者负数。这些值都是会被更新为正数的,因为题目里交代了任意两点均有路径可达。

代码:

#include<cstdio>#include<iostream>#include<cstring>#include<map>using namespace std;map<string,int> mp;string s1,s2;int adj[210][210];const int inf=0x3f3f3f3f;int main(){    int n,r,w,fr,to,cas=1;    while(scanf("%d%d",&n,&r),n){        memset(adj,0,sizeof adj);        for(int i=0;i<n;i++) adj[i][i]=inf;        mp.clear();        int tot=-1;        for(int i=0;i<r;i++){            cin>>s1>>s2>>w;            if(!mp.count(s1)) mp[s1]=++tot;            fr=mp[s1];            if(!mp.count(s2)) mp[s2]=++tot;            to=mp[s2];            adj[fr][to]=adj[to][fr]=w;        }        for(int k=0;k<n;k++)            for(int i=0;i<n;i++)                for(int j=0;j<n;j++)                    adj[i][j]=max(adj[i][j],min(adj[i][k],adj[k][j]));        cin>>s1>>s2;        printf("Scenario #%d\n%d tons\n\n",cas++,adj[mp[s1]][mp[s2]]);    }return 0;}


0 0
原创粉丝点击