POJ3268 Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18713   Accepted: 8561

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

二话不说就是一个DFS砸上去,敲一半才看到这图不一定是树……默默删了代码写SPFA。每头牛都要走到X点再走回去,那么就在正向边和反向边上各跑一次SPFA,枚举每个点看,找最长的来回距离和就是答案。

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<queue>
 8 using namespace std;
 9 const int mxm=300010;
10 const int mxn=2000;
11 struct edge{
12     int v,dis;
13     int next;
14     bool b;//判断正向反向
15 }e[mxm];
16 int hd[mxm],cnt;
17 int n,m,x;
18 int dis[mxn];
19 int ans[mxn];
20 void add_edge(int u,int v,int dis){
21     e[++cnt].v=v;e[cnt].next=hd[u];e[cnt].dis=dis;hd[u]=cnt;
22     e[cnt].b=1;//正向
23     e[++cnt].v=u;e[cnt].next=hd[v];e[cnt].dis=dis;hd[v]=cnt;
24     e[cnt].b=0;//反向
25 }
26 int inq[mxn];
27 void SPFA(bool dir){
28     memset(dis,32,sizeof dis);
29     queue<int>q;
30     inq[x]=1;
31     dis[x]=0;
32     q.push(x);
33     while(!q.empty()){
34         int u=q.front();q.pop();inq[u]=0;
35         for(int i=hd[u];i;i=e[i].next){
36             if(e[i].b!=dir)continue;
37             int v=e[i].v;
38             if(dis[u]+e[i].dis<dis[v]){
39                 dis[v]=dis[u]+e[i].dis;
40                 if(!inq[v]){
41                     inq[v]=1;
42                     q.push(v);
43                 }
44             }
45         }
46     }
47     return;
48 }
49 int main(){
50     scanf("%d%d%d",&n,&m,&x);
51     int i,j;
52     int u,v,d;
53     for(i=1;i<=m;i++){
54         scanf("%d%d%d",&u,&v,&d);
55         add_edge(u,v,d);
56     }
57     SPFA(1);//正向
58     memcpy(ans,dis,sizeof dis);
59     SPFA(0);
60     int mxans=0;
61     for(i=1;i<=n;i++){
62         if(dis[i]+ans[i]>mxans)mxans=dis[i]+ans[i];
63     }
64     printf("%d\n",mxans);
65     return 0;
66 }
时间: 2024-10-10 05:35:19

POJ3268 Silver Cow Party的相关文章

POJ3268 Silver Cow Party 【Dijkstra】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13183   Accepted: 5932 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 

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 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

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co

POJ-3268 Silver Cow Party( 最短路 )

题目链接:http://poj.org/problem?id=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 road

poj3268——Silver Cow Party(最短路+godv之力)

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

POJ-3268 Silver Cow Party---正向+反向Dijkstra

题目链接: https://vjudge.net/problem/POJ-3268 题目大意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号X,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求所有牛从开始到回家的时间是多少? 思路: 所有牛都回到了家所花费的时间就是这些牛中花费时间的最大者,可以正向的Dijkstra求出从X到每个点的最短时间,然后一个骚操作:将所有边反向(swap(Map[i][j], Map[j][i])),再从x用dijkstra求出X到每个点的最

poj3268 Silver Cow Party (SPFA求最短路)

其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #

poj3268 Silver Cow Party(最短路)

非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了. 题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int

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