标签:des com http blog style class div img code c java
给出1~N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了
题目:
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 <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 5000+10 8 9 10 int N,R; 11 12 #define MP(a,b) make_pair(a,b) 13 #define INF 100000000 14 15 #define pii pair<int,int> 16 vector<pii> G[MAXN]; 17 struct pri 18 { 19 bool operator () (const pair<int,int> &p1,const pair<int,int>&p2) 20 { 21 return p1.second< p2.second; 22 } 23 }; 24 25 int d[MAXN]; 26 void dj(int s) 27 { 28 for(int i = 1;i<=N;i++) 29 { 30 d[i]= INF; 31 d[i] = INF; 32 } 33 d[s]=0; 34 d[s]=0; 35 36 priority_queue<pii,vector<pii>,pri> q; 37 q.push( MP(s,d[s]) ); 38 while(!q.empty()) 39 { 40 int u = q.top().first; 41 q.pop(); 42 43 int size = G[u].size(); 44 45 for(int i=0;i<size;i++) 46 { 47 int v,w; 48 v = G[u][i].first; 49 w = G[u][i].second; 50 51 if( d[v] > d[u]+w ) 52 { 53 d[v]=d[u]+w; 54 q.push(MP(v,d[v])); 55 56 } 57 } 58 } 59 60 } 61 /* 62 void spfa(int src) 63 { 64 65 for(int i=1;i<=N;i++) 66 { 67 d[i] = INF; 68 } 69 d[src]=0; 70 queue<int> q; 71 q.push(src); 72 int a,next; 73 int vis[MAXN]={0}; 74 while(!q.empty()) 75 { 76 a = q.front(); 77 q.pop(); 78 vis[a] = 0; 79 int len = G[a].size(); 80 for(int i=0;i<len;i++) 81 { 82 next = G[a][i].first; 83 if( d[next]> d[a]+G[a][i].second ) 84 { 85 d[next] = d[a]+G[a][i].second; 86 if( !vis[next] ) 87 { 88 q.push(next); 89 vis[next]=1; 90 } 91 } 92 } 93 } 94 } 95 */ 96 struct node{ 97 98 int now,g,h,f; 99 bool operator < (const node& x) const 100 { 101 if( f == x.f ) 102 { 103 return x.g<g; 104 } 105 else 106 { 107 return x.f<f; 108 } 109 } 110 }; 111 void astar(int src,int to) 112 { 113 priority_queue<node> q; 114 node a, next; 115 116 a.now = src; 117 a.g = 0; 118 a.f = d[src]+a.g; 119 q.push(a); 120 int cnt = 0; 121 while(!q.empty()) 122 { 123 a = q.top(); 124 q.pop(); 125 if( a.now == to ) 126 { 127 cnt++; 128 if( cnt == 2 ) 129 { 130 cout<<a.g<<endl; 131 return; 132 } 133 } 134 int size = G[a.now].size(); 135 for(int i=0;i<size;i++) 136 { 137 next.now = G[a.now][i].first; 138 next.g = a.g + G[a.now][i].second; 139 next.f = next.g+d[next.now]; 140 q.push(next); 141 } 142 } 143 } 144 145 int main() 146 { 147 148 cin>>N>>R; 149 150 for(int i=0;i<R;i++) 151 { 152 int f,t,w; 153 cin>>f>>t>>w; 154 155 G[f].push_back( MP(t,w) ); 156 G[t].push_back( MP(f,w) ); 157 } 158 159 dj(N); 160 // spfa(N); 161 astar(1,N); 162 return 0; 163 }
POJ3255:Roadblocks(次短路 SPFA+A星),码迷,mamicode.com
POJ3255:Roadblocks(次短路 SPFA+A星)
标签:des com http blog style class div img code c java
原文地址:http://www.cnblogs.com/doubleshik/p/3702138.html