Heavy Transportation(最短路 + dp)

Heavy Transportation
Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 1797

Descriptionios

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.
 
不用鄰接表會超時。注意輸出格式。
 
AC Code:
 1 /**
 2 *Dijkstra + 靜態鄰接表 + 優先隊列優化
 3 */
 4 
 5 #include <iostream>
 6 #include <deque>
 7 #include <queue>
 8 #include <cstdio>
 9 #include <cstring>
10 #include <algorithm>
11 
12 using namespace std;
13 
14 const int MAXV = 1001;  //最大邊數
15 const int INF = 0x3f3f3f3f;  //最大權值
16 struct Edge
17 {
18     int to;  
19     int link;  
20     int w;  
21     void set_val(int a, int b, int c){to = a, link = b, w = c;}
22 }edge[MAXV * MAXV >> 1];  //存儲邊
23 int pre[MAXV];
24 
25 struct Node
26 {
27     int v;  //頂點的標號
28     int w;  //頂點v到源點的最短路
29     Node(int a, int b) {v = a; w = b;}
30     void set_val(int a, int b) {v = a; w = b;}
31 };  //設立該結構體的目的:做爲優先隊列的結點
32 int d[MAXV];  //記錄最短路
33 bool done[MAXV];  //記錄是否已找到最短路,避免重複訪問
34 int n, m;
35 
36 bool operator < (const Node& x, const Node& y)
37 {
38     return x.w < y.w;
39 }
40 
41 int main()
42 {
43     int t, ca = 1;
44     scanf("%d", &t);
45     while(t--){
46         scanf("%d %d", &n, &m);
47         //創建靜態鄰接表
48         memset(pre, -1, sizeof(pre));
49         for(int i = 0; m--; ){
50             int a, b, c;
51             scanf("%d %d %d", &a, &b, &c);
52             edge[i].set_val(a, pre[b], c);
53             pre[b] = i++;
54             edge[i].set_val(b, pre[a], c);
55             pre[a] = i++;
56         }
57 
58         //執行Dij算法,使用最小堆進行優化
59         memset(done, false, sizeof(done));
60         memset(d, 0, sizeof(d));  //d數組的初始化方式是關鍵!
61         d[1] = INF;
62         priority_queue<Node> que;
63         que.push(Node(1, d[1]));  //源點入隊
64         done[1] = true;
65         while(!que.empty()){
66             Node cur = que.top();
67             que.pop();
68             for(int i = pre[cur.v]; i != -1; i = edge[i].link){
69                 int to = edge[i].to;
70                 if(!done[to] && d[to] < min(cur.w, edge[i].w)){
71                     d[to] = min(cur.w, edge[i].w);
72                     que.push(Node(to, d[to]));
73                 }
74             }
75         }
76 
77         //輸出結果
78         printf("Scenario #%d:\n%d\n\n", ca++, d[n]);
79     }
80     return 0;
81 }
相關文章
相關標籤/搜索