本题链接 : http://poj.org/problem?id=3268
题目大意:牛们要去聚会,输入N = 顶点数(牛场);M = 边(路)的数目; X = 终点 (聚会点)。问题:求来回时间的最大值。
Input:
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output:
Line 1: One integer: the maximum of time any one cow must walk.
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output:
10 解题思路:因为本题是单向的,所以在做的时候可以做一个正向图和一个反向图,分别求解来回的时间,然后找和的最大值。这里是我的代码:
1 #include <cstring> 2 #include <iostream> 3 #define INF 9999999 4 using namespace std; 5 6 bool used[100005]; 7 int V, E; 8 const int maxn = 1005; 9 10 void dijkstra (int s, int cost[][1005], int d[]) { 11 fill (d, d + V + 1, INF); 12 fill (used,used + V + 1,false); 13 d[s] = 0; 14 while (true) { 15 int v = -1; 16 for (int u = 1; u <= V; u++) { 17 if (!used[u] && (v == -1 || d[u] < d[v])) v = u; 18 } 19 if (v == -1) break; 20 used[v] = true; 21 for (int u = 1; u <= V; ++u) { 22 if (d[u] > d[v] + cost[v][u]) { 23 d[u] = d[v] + cost[v][u]; 24 } 25 } 26 } 27 } 28 29 int cost[maxn][maxn]; 30 int rcost[maxn][maxn]; 31 32 int main () { 33 int d[maxn]; 34 int rd[maxn]; 35 int x, y, w; 36 int sum[maxn]; 37 int S; 38 cin >> V >> E >> S; 39 40 for (int i = 1;i <= V; ++i) 41 for (int j = 1; j <= V; ++j) 42 rcost[i][j] = cost[i][j] = INF; 43 44 for (int i = 0; i < E; ++i) { 45 cin >> x >> y >> w; 46 rcost[y][x] = cost[x][y] = w; 47 } 48 49 dijkstra(S, cost, d);//分别计算最短路径 50 dijkstra(S, rcost, rd); 51 52 for (int i = 1; i <= V; ++i)//求和 53 sum[i] = d[i] + rd[i]; 54 55 int maxnum = sum[1]; 56 for (int i = 1; i <= V; ++i)///找最大值 57 if (sum[i] > maxnum) 58 maxnum = sum[i]; 59 60 cout << maxnum << endl; 61 62 return 0; 63 }
欢迎码友评论,一起成长。
时间: 2024-10-11 13:03:43