题意:给你n个奶牛,m个农场,在x农场开派对,问你所有奶牛中必须花费最大的时间
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 using namespace std; 8 #define M 100005 9 #define N 1005 10 #define inf 9999999 11 int size1,size2,n,m,x; 12 int dis[N],vis[N],head1[N],head2[N]; 13 int sum[N]; 14 struct Edge 15 { 16 int v,w,next; 17 Edge(){} 18 Edge(int V,int W,int NEXT):v(V),w(W),next(NEXT){} 19 }edge1[M],edge2[M]; 20 21 void Init() 22 { 23 size1 = 0; 24 size2 = 0; 25 memset(head1,-1,sizeof(head1)); 26 memset(head2,-1,sizeof(head2)); 27 } 28 29 void InsertEdge1(int u,int v,int w) 30 { 31 edge1[size1] = Edge(v,w,head1[u]); 32 head1[u] = size1++; 33 } 34 void InsertEdge2(int u,int v,int w) 35 { 36 edge2[size2] = Edge(v,w,head2[u]); 37 head2[u] = size2++; 38 } 39 40 void spfa(int st,Edge edge[],int head[]) 41 { 42 for(int i=1; i<=n; i++) 43 { 44 vis[i] = 0; 45 dis[i] = inf; 46 } 47 dis[st] = 0; vis[st] = 1; 48 queue<int> Q; 49 Q.push(st); 50 while(!Q.empty()) 51 { 52 int u = Q.front(); 53 Q.pop(); 54 vis[u] = 0; 55 for(int i=head[u]; i!=-1; i = edge[i].next) 56 { 57 int v = edge[i].v; 58 if(dis[v] > dis[u] + edge[i].w) 59 { 60 dis[v] = dis[u] + edge[i].w ; 61 if(!vis[v]) 62 { 63 vis[v] = 1; 64 Q.push(v); 65 } 66 } 67 } 68 } 69 } 70 71 int main() 72 { 73 while(scanf("%d%d%d",&n,&m,&x)!=EOF) 74 { 75 Init(); 76 int u,v,w; 77 for(int i=0; i<m; i++) 78 { 79 scanf("%d%d%d",&u,&v,&w); 80 InsertEdge1(u,v,w); 81 InsertEdge2(v,u,w); 82 } 83 memset(sum,0,sizeof(sum)); 84 spfa(x,edge1,head1); 85 for(int i=1; i<=n; i++) 86 sum[i] += dis[i]; 87 spfa(x,edge2,head2); 88 for(int i=1; i<=n; i++) 89 sum[i] += dis[i]; 90 int ans = -1; 91 for(int i=1; i<=n; i++) 92 ans = max(ans,sum[i]); 93 printf("%d ",ans); 94 } 95 return 0; 96 }