【最短路】ACdream 1198 - Transformers' Mission

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 }

【最短路】ACdream 1198 - Transformers' Mission

时间: 2024-12-06 04:07:25

【最短路】ACdream 1198 - Transformers' Mission的相关文章

ACdream 1198 Transformers&#39; Mission(最短路)

题目地址:http://acdream.info/problem?pid=1198 比赛的时候做出的人很少...所以我也没看....其实就是一道简单的最短路...要使时间最短,那么对于每一个点来说都要最短的时间从起点走到该点,然后再用最短的时间从该点到终点,那么只要求两次最短路就行了.然后最后求两个最短路的和的最大值,即最晚到达的时间. 代码如下: #include <iostream> #include <cstdio> #include <string> #incl

ACdream 1415 最短路+桥

点击打开链接 题意:给个图,问你从1到n的最短路的路径上,有多少桥 思路:先是要满足条件最短路,然后判断每条边是不是最短路里的边,怎么判断也很简单,先从1开始求最短路和从n开始求最短路,对于边U到V来说,若1到U的最短路加上n到V的最短路在加上这条边的权值若等于1到n的最短路,那么这条边就是我们要的,就是这个条件if(dis1[U[i]]+COST[i]+dis2[V[i]]==maxdis||dis1[V[i]]+COST[i]+dis2[U[i]]==maxdis)maxdis是1到n的最短

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj2449:第k短路问题

Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One

ACDream - Chasing Girl

先上题目: Chasing Girl Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description YYS 是SCAU_ACM里相当有名气的高富帅, 又因为他曾代表SCAU参加 World Final 而被各路亲朋好友仰慕.但YYS 又有另外一个嗜好,就是泡妞.在大学的时候,他经常去找各个女友玩耍, 但在路上却又整天遇到膜拜他的渣渣

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

UVA - 1416 Warfare And Logistics (最短路)

Description The army of United Nations launched a new wave of air strikes on terroristforces. The objective of the mission is to reduce enemy's logistical mobility. Each airstrike will destroy a path and therefore increase the shipping cost of the sh

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

HDU 4063 线段与圆相交+最短路

Aircraft Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 228 Problem Description You are playing a flying game. In the game, player controls an aircraft in a 2D-