标签:
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
Output
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
Source
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 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5774651.html