Silver cow party
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:12674 |
|
Accepted:5651 |
Description
One cow from eachNFarms (1 ≤N≤ 1000) conveniently numbered 1 ..NIs going to attend the big cow party to be held at farm #X(1 ≤X≤N). A totalM(1 ≤MLess than or equal to 100,000) unidirectional (one-way roads connects pairs of farms; roadIRequiresTi(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:
N,
M, And
X
Lines 2 ..
M+ 1: Line
I+ 1 describes Road
IWith three space-separated integers:
AI,
Bi, And
Ti. The described road runs from farm
AITo farm
Bi, Requiring
TiTime units to traverse.
Output
Line 1: One INTEGER: the maximum of time any one cow must walk.
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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
Mean:
There are n farms on the pasture, and there are some paths between the farms. Each farm has a cow. Now, the ox on farm X has a birthday party, the ox from other farms will go to the farm to attend the party. Now let you choose one of the most time-consuming cows to output the time.
Now, we want you to find a directed graph with n nodes and m edges. We want you to find the longest route from n-1 nodes to a specified node.
Analyze:
This idea is clever. When we store images, we use chained forward stars to store them. When we store images, we create two sides, one forward and one reverse, and one flag to mark them. Then, we use spfa twice and the first time to find the forward graph starting from X to the shortest path of each node, and the second time to find the reverse graph starting from X to the shortest path of each node, finally, the two shortest paths are added and the maximum value is the final answer.
Time Complexity:O (N * K)
Source code:
//Memory Time// 3521K 241MS//by : Snarl_jsb#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<iomanip>#include<string>#include<climits>#include<cmath>#define MAXV 1010#define MAXE 100010#define LL long longusing namespace std;namespace Adj{ struct Node { int to,next,val; bool flag; } edge[MAXE<<1]; int top,head[MAXV]; void init() { top=1; memset(head,0,sizeof(head)); } void addEdge(int u,int v,int val) { edge[top].to=v; edge[top].val=val; edge[top].flag=1; edge[top].next=head[u]; head[u]=top++; edge[top].to=u; edge[top].val=val; edge[top].flag=0; edge[top].next=head[v]; head[v]=top++; }}using namespace Adj;int n,m,x,ans;bool vis[MAXV];int dis[MAXV];int dis1[MAXV];void spfa(bool flag){ memset(vis,0,sizeof(vis)); for(int i=0;i<=n;i++) dis[i]=INT_MAX; queue<int>Q; Q.push(x); vis[x]=1; dis[x]=0; while(!Q.empty()) { int now=Q.front(); Q.pop(); vis[now]=0; for(int i=head[now];i;i=edge[i].next) { if(flag==1) { if(edge[i].flag==0) continue; } else { if(edge[i].flag==1) continue; } int son=edge[i].to; int val=edge[i].val; if(dis[now]+val<dis[son]) { dis[son]=dis[now]+val; if(!vis[son]) { vis[son]=1; Q.push(son); } } } } if(flag==1) { for(int i=1;i<=n;i++) dis1[i]=dis[i]; } else { int Max=INT_MIN; for(int i=1;i<=n;i++) { dis[i]+=dis1[i]; if(dis[i]>Max) Max=dis[i]; } ans=Max; }}int main(){// freopen("cin.txt","r",stdin);// freopen("cout.txt","w",stdout); while(~scanf("%d %d %d",&n,&m,&x)) { init(); int u,v,w; while(m--) { scanf("%d %d %d",&u,&v,&w); addEdge(u,v,w); } spfa(1); spfa(0); printf("%d\n",ans); } return 0;}