Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
两次spfa,334K+16ms第一次理解错成每只母牛往返的必须时间,所以求最小,事实上并不是。 还有看了讨论,居然有矩阵转置的解法,学习一下拓展思维。 #include <stdio.h> #include <memory.h> #include <queue> #define MAXN 1001 #define INF 0x3f3f3f3f using namespace std; typedef struct { int start,end,time; int next1,next2; }Edge; Edge de[100001]; int headlist1[MAXN],headlist2[MAXN]; int dis1[MAXN],dis2[MAXN],visited[MAXN]; void spfa1(int k) { queue<int>q; int i,temp,x,y; memset(visited,0,sizeof(visited)); memset(dis1,0x3f,sizeof(dis1)); q.push(k),visited[k]=1,dis1[k]=0; while(!q.empty()) { temp=q.front(); q.pop(); visited[temp]=0; for(i=headlist1[temp];i!=-1;i=de[i].next1) { x=de[i].start,y=de[i].end; if(dis1[y]>dis1[x]+de[i].time) { dis1[y]=dis1[x]+de[i].time; if(!visited[y]) visited[y]=1,q.push(y); } } } } void spfa2(int k) { queue<int>q; int i,temp,x,y; memset(visited,0,sizeof(visited)); memset(dis2,0x3f,sizeof(dis2)); q.push(k),visited[k]=1,dis2[k]=0; while(!q.empty()) { temp=q.front(); q.pop(); visited[temp]=0; for(i=headlist2[temp];i!=-1;i=de[i].next2) { x=de[i].end,y=de[i].start; if(dis2[y]>dis2[x]+de[i].time) { dis2[y]=dis2[x]+de[i].time; if(!visited[y]) visited[y]=1,q.push(y); } } } } int main() { int N,M,X,i; while(scanf("%d %d %d",&N,&M,&X)!=EOF) { memset(headlist1,-1,sizeof(headlist1)); memset(headlist2,-1,sizeof(headlist2)); for(i=0;i<M;++i) { scanf("%d %d %d",&de[i].start,&de[i].end,&de[i].time); de[i].next1=headlist1[de[i].start]; headlist1[de[i].start]=i; de[i].next2=headlist2[de[i].end]; headlist2[de[i].end]=i; } spfa1(X); spfa2(X); int max=-1; for(i=1;i<=N;++i) if(max<dis1[i]+dis2[i]&&i!=X) max=dis1[i]+dis2[i]; printf("%d\n",max); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator