POJ 1797 Heavy Transportation

Heavy Transportation

Time Limit: 3000ms
Memory Limit: 30000KB
This problem will be judged on  PKU. Original ID: 1797
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 
解题:dijkstra的乱用
q.push(make_pair(INF,1));
写错一句了。。。。不过 由于。。。。不影响计算结果。。。。。。。
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 const int maxn = 1010;

18 struct arc{

19     int to,w,next;

20     arc(int x = 0,int y = 0,int z = -1){

21         to = x;

22         w = y;

23         next = z;

24     }

25 };

26 arc e[200000];

27 int d[maxn],head[maxn],tot,n,m;

28 bool done[maxn];

29 void add(int u,int v,int w){

30     e[tot] = arc(v,w,head[u]);

31     head[u] = tot++;

32     e[tot] = arc(u,w,head[v]);

33     head[v] = tot++;

34 }

35 void dijkstra(){

36     for(int i = 1; i <= n; ++i){

37         d[i] = 0;

38         done[i] = false;

39     }

40     d[1] = INF;

41     priority_queue< pii,vector< pii >,less< pii > >q;

42     q.push(make_pair(0,1));

43     while(!q.empty()){

44         int u = q.top().second;

45         q.pop();

46         if(done[u]) continue;

47         done[u] = true;

48         for(int i = head[u]; ~i; i = e[i].next){

49             if(d[e[i].to] < min(d[u],e[i].w)){

50                 d[e[i].to] = min(d[u],e[i].w);

51                 q.push(make_pair(d[e[i].to],e[i].to));

52             }

53         }

54         if(done[n]) break;

55     }

56 }

57 int main() {

58     int T,u,v,w,cs = 1;

59     scanf("%d",&T);

60     while(T--){

61         scanf("%d %d",&n,&m);

62         memset(head,-1,sizeof(head));

63         for(int i = tot = 0; i < m; ++i){

64             scanf("%d %d %d",&u,&v,&w);

65             add(u,v,w);

66         }

67         dijkstra();

68         printf("Scenario #%d:\n%d\n\n",cs++,d[n]);

69     }

70     return 0;

71 }
View Code

 

你可能感兴趣的:(port)