<poj - 3268> Silver Cow Party 最短路径问题

  本题链接 : http://poj.org/problem?id=3268

  题目大意:牛们要去聚会,输入N = 顶点数(牛场);M = 边(路)的数目; X = 终点 (聚会点)。问题:求来回时间的最大值。

 Input:

  Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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

<poj - 3268> Silver Cow Party 最短路径问题的相关文章

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤X ≤

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 Description One cow from each of \(N\) farms \((1 ≤ N ≤ 1000)\) conveniently numbered \(1..N\) is going to attend the big cow party to be held at farm #\(X (1 ≤ X ≤ N)\). A total of \(M (1 ≤ M ≤ 100,000)\) unidirectional (one-way roads

POJ 3268 Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20274   Accepted: 9278 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X