【最短路】ACdream 1198 - Transformers' Mission

Posted on 2015-03-31 23:45  LLGemini  阅读(151)  评论(0编辑  收藏  举报

Problem Description

A group of transformers whose leader is Optimus Prime(擎天柱) were assigned a mission: to destroy all Decepticon's(霸天虎) bases.

The bases are connected by roads. They must visit each base and place a bomb there. They start their mission at a particular base and from there they disseminate to reach each base.

The transformers must use the available roads to travel between bases. Any of them can visit one base after another, but they must all gather at a common base when their task in done because Optimus Prime(擎天柱) doesn't allow his teammate to run into any trouble.

Your job is to determine the minimum time needed to complete the mission.

You may assume that the time required to place a bomb is negligible.

Each transformer can carry unlimited number of bombs and there is an unlimited supply of transformers for this mission.

Input

Input starts with an integer T(T ≤ 50), denoting the number of test cases.

The first line of each case starts with a positive integer n(1 ≤ n ≤ 1000), where n denotes the number of Decepticon's(霸天虎) bases.

The next line contains a positive integer m(0 ≤ m ≤ 100000), where m is the number of roads connecting two bases.

Each of the next m lines contain two distinct numbers u, v, w(0 ≤ u, v, w < n, u != v), this means there is a road from base u to base v which costs w units of time. The bases are numbered from 0 to n-1.

The last line of each case contains two integers s, e(0 ≤ s, e < n).

Where s denotes the base from where the mission starts and e denotes the base where they must meet.

You may assume that two bases will be directly connected by at most one road.

The input will be given such that, it will be possible to go from any base to another by using one or more roads.

Output

For each case, output one line containing "Case #x: " followed by the minimum time.

Sample Input

2
5 6
0 1 2
1 2 2
3 1 2
4 0 3
3 2 3
3 4 1
4 3
5 5
1 0 1
1 2 3
1 3 3
4 2 2
3 4 1
4 2

Sample Output

Case #1: 7
Case #2: 9

【题意】简单说就是派好几个人去炸n个地方,问至少需要花多长时间。
【分析】任取一点分别求到起点和重点的最短路,所得之和就是炸这个地方时所需要花费的最少时间,然后取n个地方花费时间的最大值即可;
【技巧】两次BFS求最短路;
【代码】
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<vector>
 5 #include<cstring>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn = 1010;
 9 vector<int> G[maxn];
10 int Map[maxn][maxn], d1[maxn], d2[maxn], vis[maxn];
11 int m, n;
12 void BFS(int s, int* d)
13 {
14     memset(d, -1, sizeof(d));
15     memset(vis, 0, sizeof(vis));
16 
17     int u = s;
18     d[u] = 0; vis[u] = 1;
19     queue<int> q;
20     q.push(u);
21     while(!q.empty())
22     {
23 
24         int v = q.front(); q.pop();
25         //cout << "v: " << v << endl;
26         int sz = G[v].size();
27         for(int i = 0; i < sz; i++)
28         {
29             int w = G[v][i];
30             //cout << "w: " << w << endl;
31             if(!vis[w])
32             {
33                 vis[w] = 1;
34                 d[w] = d[v]+Map[v][w];
35                 q.push(w);
36             }
37             else
38             {
39                 if(d[v]+Map[v][w] < d[w])
40                 {
41                     d[w] = d[v]+Map[v][w];
42                     q.push(w);
43                 }
44             }
45         }
46 
47     }
48 }
49 
50 int main()
51 {
52     int T; scanf("%d", &T);
53     for(int kase = 1; kase <= T; kase++)
54     {
55         memset(Map, 0, sizeof(Map));
56         for(int i = 0; i < n; i++) G[i].clear();
57         scanf("%d%d", &n, &m);
58         while(m--)
59         {
60             int u, v, t;
61             scanf("%d%d%d", &u, &v, &t);
62             Map[u][v] = Map[v][u] = t;
63             G[u].push_back(v); G[v].push_back(u);
64         }
65         int st, en; scanf("%d%d", &st, &en);
66         BFS(st, d1);
67         BFS(en, d2);
68         int ans = 0;
69         for(int i = 0; i < n; i++)
70             ans = max(ans, d1[i]+d2[i]);
71         printf("Case #%d: %d\n", kase, ans);
72 //        for(int i = 0; i < n; i++) printf("%d ", d2[i]);
73 //        printf("\n");
74     }
75     return 0;
76 }
View Code