[POJ] 1797 Heavy Transportation

Heavy Transportation

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 19039   Accepted: 5084

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

TUD Programming Contest 2004, Darmstadt, Germany
 
 
题解:无向图,每条边有一个最大载重量,汽车不得超过此载重量。求从1到n汽车最多能运的货物重量。根据数据规模和题意,用spfa即可。维护一个数组dist[i]定义为在i点时汽车最大载重。初始化dist[i]=-1;dist[1]=MAX_NUMBER 松弛为dist[u]=max(dist[u],min(dist[v],cost[u][v])),v点到u点。
特别注意一下输出格式,两个case输出中间有空行。
 
代码:
  1 #include<stdio.h>
  2 #include<stdbool.h>
  3 #include<limits.h>
  4 #include<string.h>
  5 int i,j,n,m,tot,
  6     toit[110000],next[110000],list[1100],cost[110000],
  7     q[500000],dist[110000];
  8 
  9 bool can[1100];
 10 
 11 int 
 12 pre()
 13 {
 14     memset(toit,0,sizeof(toit));
 15     memset(next,0,sizeof(next));
 16     memset(list,0,sizeof(list));
 17     memset(q,0,sizeof(q));
 18     memset(cost,0,sizeof(cost));
 19     memset(can,true,sizeof(can));
 20     memset(dist,255,sizeof(dist));
 21     tot=0;
 22     return 0;
 23 }
 24 int 
 25 min(int a,int b)
 26 {
 27     if(a<b) return(a);
 28     else return(b);
 29 }
 30 
 31 int 
 32 add(int x,int y,int z)
 33 {
 34     tot++;
 35     toit[tot]=y;
 36     next[tot]=list[x];
 37     cost[tot]=z;
 38     list[x]=tot;
 39     
 40     tot++;
 41     toit[tot]=x;
 42     next[tot]=list[y];
 43     cost[tot]=z;
 44     list[y]=tot;
 45     return 0;
 46 }
 47 
 48 int 
 49 init()
 50 {
 51     int x,y,z;
 52     scanf("%d%d\n",&n,&m);
 53     for(i=1;i<=m;i++)
 54     {
 55         scanf("%d%d%d",&x,&y,&z);
 56         add(x,y,z);
 57     }
 58     return 0;
 59 }
 60 
 61 int 
 62 spfa(int s)
 63 {
 64     int i,head,tail,k,v;
 65     head=0;tail=1;
 66     q[1]=s;
 67     can[s]=false;
 68     dist[s]=3511111;
 69     
 70     while(head!=tail)
 71     {
 72         head=head%500000+1;
 73         v=q[head];
 74         k=list[v];
 75         
 76         while(k!=0)
 77         {
 78             if(min(dist[v],cost[k])>dist[toit[k]])
 79             {
 80                 dist[toit[k]]=min(dist[v],cost[k]);
 81                 if(can[toit[k]])
 82                 {
 83                     tail=tail%500000+1;
 84                     q[tail]=toit[k];
 85                     can[toit[k]]=false;
 86                 }
 87             }
 88             k=next[k];
 89         }
 90         can[v]=true;
 91     }
 92     return 0;
 93 }
 94 
 95 int 
 96 main()
 97 {
 98     int ca,num,ci;
 99     num=0;
100     scanf("%d\n",&ci);
101     
102     for(ca=1;ca<=ci;ca++)
103     {
104         pre();
105         init();
106         spfa(1);
107     
108         printf("Scenario #%d:\n%d\n",ca,dist[n]);
109         printf("\n");
110     }
111     return 0;
112 }
113     
114     

 

 
 
posted @ 2014-03-22 22:38  SXISZERO  阅读(190)  评论(0编辑  收藏  举报