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 ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow‘s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

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.

Sample Input

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

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source

USACO 2007 February Silver

翻译时间:来看一哈我的英文水平张进点没有。  题目大意:

块田中(1≤N≤1000)都有1只牛去参加盛大的母牛聚会,这个聚会被安排在X号田(1≤X ≤N)。一共有M( 1≤M≤100,000)条单行道分别连接着两块田,且通过路i需要花Ti(1≤Ti≤100)的时间。每头母牛必需参加宴会并且在宴会结束时回到自己的田地,但是每头牛都很懒而喜欢选择化是最少的一个方案。来时的路和去时的可能不一样。求每头牛要来回的最短时间。

找出来回路程最长的。输出来回最长的距离。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 #define N 1010
 7 #define Max 10000000
 8 queue<int> q;
 9 bool exist[N];
10 int dis[N],map[N][N],num[N],n,m,x;
11 void init(){
12     memset(exist,false,sizeof(exist));
13     memset(dis,0x3f,sizeof(dis));
14 }
15 void SPFA(int x){
16     init();
17     exist[x]=true;q.push(x);dis[x]=0;
18     while(!q.empty()){
19         int p=q.front();q.pop();exist[p]=false;
20         for(int i=1;i<=n;i++)
21           if(dis[p]+map[p][i]<dis[i]){
22             dis[i]=dis[p]+map[p][i];
23             if(exist[i]==false) q.push(i),exist[i]=true;
24           }
25     }
26 }
27 void trave(){
28     for(int i=1;i<=n;i++)
29       for(int j=1,temp;j<i;j++){// 注意这里是从j到i-1 如果j从1到n 那么相当于没反
30       // 至于为什么 自己想去
31           temp=map[i][j];map[i][j]=map[j][i];map[j][i]=temp;
32       }
33 }
34 int main()
35 {
36     scanf("%d%d%d",&n,&m,&x);
37     for(int i=1;i<=n;i++)
38       for(int j=1;j<=n;j++)
39         map[i][j]=Max;// 这里表示很不解 循环赋值AC啦
40         // memset赋值 样例都过不了
41     for(int i=1,u,w,v;i<=m;i++){
42         scanf("%d%d%d",&u,&v,&w);
43         map[u][v]=w;
44     }
45     SPFA(x);
46     for(int i=1;i<=n;i++)
47       num[i]=dis[i];
48     trave();
49     SPFA(x);
50     int max=0;
51     for(int i=1; i<=n; i++)
52         if(dis[i]+num[i]>max)
53             max=dis[i]+num[i];
54     printf("%d\n",max);
55     return 0;
56 }

思路:先来一遍SPFA,这时相当于求出了返程的的距离,全部转存到num数组里,再来一次反边,再跑一边SPFA,即求出了去时的最短路径,相加求和。

时间: 2024-08-03 09:08:46

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

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace