标签:cin maximum direct short color max cow span time
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<cmath> 8 9 using namespace std; 10 11 const int MAX_V=1001; 12 const int MAX_E=100001; 13 const int INF=99999999; 14 15 typedef pair<int,int> P; 16 17 struct edge{ 18 int from,to,cost; 19 }; 20 21 int d1[MAX_V];//从起点出发到任意一点的最短路 22 int d2[MAX_V];//从任一点出发到起点的最短路 23 24 vector<edge>es1[MAX_E]; 25 vector<edge>es2[MAX_E]; 26 27 //就是求单源最短路的 28 void dijkstra(int s,int* d,vector<edge> es[]){ 29 fill(d,d+MAX_V,INF); 30 d[s]=0; 31 priority_queue<P,vector<P>,greater<P> > que; 32 que.push(P(0,s)); 33 while(!que.empty()){ 34 P p=que.top();que.pop(); 35 int v=p.second; 36 if(d[v]<p.first)continue; 37 for(int i=0;i<es[v].size();i++){ 38 edge e=es[v][i]; 39 if(d[e.to]>d[e.from]+e.cost){ 40 d[e.to]=d[e.from]+e.cost; 41 que.push(P(d[e.to],e.to)); 42 } 43 } 44 } 45 46 } 47 48 int main() { 49 int n,m,x; 50 cin>>n>>m>>x; 51 while(m--){ 52 edge temp; 53 scanf("%d%d%d",&temp.from,&temp.to,&temp.cost); 54 es1[temp.from].push_back(temp); 55 swap(temp.from,temp.to); 56 es2[temp.from].push_back(temp); 57 } 58 int ans=-1; 59 for(int i=1;i<=n;i++){ 60 dijkstra(i,d1,es1);//从起点出发到任意一点的最短路 61 dijkstra(i,d2,es2);//从任一点出发到起点的最短路 62 ans=max(ans,d1[x]+d2[x]); 63 } 64 cout<<ans<<endl; 65 return 0; 66 }
标签:cin maximum direct short color max cow span time
原文地址:https://www.cnblogs.com/OldAtaraxi/p/12234490.html