最短路 错题集
1.
?Silver Cow Party
?
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:?N,?M, and?X?
Lines 2..?M+1: Line?i+1 describes road?i?with three space-separated integers:?Ai,Bi, 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 3Sample Output
10Hint
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.
?
代碼
#include <iostream> #include<algorithm> #include<stdio.h> #include<string> #include<string.h> #include<math.h> #include<vector> #include<queue> using namespace std; const int maxn=1e9+7,maxx=1011; int n,m,x,cnt=0; struct studen {int v,d; };vector<studen> a[1200]; int visit[maxx];bool operator < (const studen &a,const studen &b) {return a.d>b.d; } priority_queue<studen> que; int go[1200],come[1200];int main() {int i,j,ax,bx,c,y;studen p;scanf("%d%d%d",&n,&m,&x);for(i=0; i<m; i++){scanf("%d%d%d",&ax,&bx,&c);p.v=bx;p.d=c;a[ax].push_back( p);}p.v=x;p.d=0;que.push(p);while(!que.empty()){p=que.top();que.pop();if(visit[p.v])continue;visit[p.v]=1;//printf("p=%d ",p.v);for(i=0,j=a[p.v].size(); i<j; i++){studen stu;stu.v=a[p.v][i].v;if(visit[stu.v])continue;stu.d=p.d+a[p.v][i].d;come[stu.v]=stu.d;// printf("v=%d s=%d\n",stu.d,stu.v);que.push(stu);}}//for(i=0;i<=n;i++)//printf("i=%d come=%d\n",i,come[i]);//printf("yesn=%d\n",n);for(y=1; y<=n; y++){// printf("i=%d \n",y);while(!que.empty())que.pop();memset(visit,0,sizeof(visit));p.v=y;p.d=0;que.push(p);while(!que.empty()){p=que.top();que.pop();if(visit[p.v])continue;visit[p.v]=1;//printf("p=%d ",p.v);if(p.v==x)break;for(i=0,j=a[p.v].size(); i<j; i++){studen stu;stu.v=a[p.v][i].v;if(visit[stu.v])continue;stu.d=p.d+a[p.v][i].d;que.push(stu);}}// printf("p=%d come=%d\n",p.d,come[y]);if(p.d+come[y]>cnt)cnt=p.d+come[y];}printf("%d\n",cnt);return 0; }?
總結(jié)
- 上一篇: Django自定义模板标签的使用详解
- 下一篇: 解决报错:Unable to proce