标签:The flag 图结构 void BMI clu for wap more
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19314 | Accepted: 6777 |
Description
Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.
The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.
The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).
Input
Output
Sample Input
4 4 1 2 100 2 4 200 2 3 250 3 4 100
Sample Output
450
Hint
1 #include <cstdio> 2 #include <queue> 3 #include <vector> 4 #include <functional> 5 #include <cctype> 6 #include <algorithm> 7 #define num s-‘0‘ 8 9 using namespace std; 10 typedef pair<int, int> P; 11 12 struct edge 13 { 14 int to; 15 int cost; 16 }; 17 18 const int MAX_V=5000; 19 const int INF=0x3f3f3f3f; 20 int V,E,n; 21 vector<edge> G[MAX_V]; 22 int dist[MAX_V]; 23 int dist2[MAX_V]; 24 25 void solve(); 26 27 void swap(int &x, int &y) 28 { 29 int t=x; 30 x=y;y=t; 31 } 32 33 void read(int &x){ 34 char s; 35 x=0; 36 bool flag=0; 37 while(!isdigit(s=getchar())) 38 (s==‘-‘)&&(flag=true); 39 for(x=num;isdigit(s=getchar());x=x*10+num); 40 (flag)&&(x=-x); 41 } 42 43 void write(int x) 44 { 45 if(x<0) 46 { 47 putchar(‘-‘); 48 x=-x; 49 } 50 if(x>9) 51 write(x/10); 52 putchar(x%10+‘0‘); 53 } 54 55 int main() 56 { 57 read(V);read(E); 58 for (int i=0; i<E; i++) 59 { 60 edge e; 61 int s,t,c; 62 read(s);read(t);read(c); 63 e.to=t-1;e.cost=c; 64 G[s-1].push_back(e); 65 e.to=s-1;e.cost=c; 66 G[t-1].push_back(e); 67 } 68 solve(); 69 write(dist2[V-1]); 70 putchar(‘\n‘); 71 } 72 73 void solve() 74 { 75 priority_queue<P, vector<P>, greater<P> > que; 76 fill(dist, dist+V, INF); 77 fill(dist2, dist2+V, INF); 78 dist[0]=0; 79 que.push(P(0,0)); 80 while (!que.empty()) 81 { 82 P p=que.top(); que.pop(); 83 int v=p.second, d=p.first; 84 if (dist2[v]<d) continue; 85 for (int i=0; i<G[v].size(); i++) 86 { 87 edge &e=G[v][i]; 88 int d2=d+e.cost; 89 if (dist[e.to]>d2) 90 { 91 swap(dist[e.to],d2); 92 que.push(P(dist[e.to],e.to)); 93 } 94 if (dist2[e.to]>d2 && dist[e.to]<d2) 95 { 96 dist2[e.to]=d2; 97 que.push(P(dist2[e.to],e.to)); 98 } 99 } 100 } 101 }
标签:The flag 图结构 void BMI clu for wap more
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9461696.html