标签:style blog http color io os java ar for
2 2 1 2 5 2 1 4 1 2 2
14
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc{ 19 int to,w,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 w = y; 23 next = z; 24 } 25 }; 26 struct node{ 27 int g,h,to; 28 node(int x = 0,int y = 0,int z = 0){ 29 g = x; 30 h = y; 31 to = z; 32 } 33 bool operator < (const node &x) const{ 34 return x.g + x.h < g+h; 35 } 36 }; 37 arc e[200010]; 38 int head[maxn],tail[maxn],d[maxn],tot,n,m,s,t,kth; 39 int cnt[maxn]; 40 void add(int u,int v,int w){ 41 e[tot] = arc(v,w,head[u]); 42 head[u] = tot++; 43 e[tot] = arc(u,w,tail[v]); 44 tail[v] = tot++; 45 } 46 priority_queue< pii,vector< pii >,greater< pii > >q; 47 priority_queue<node>qq; 48 void dijkstra(){ 49 for(int i = 1; i <= n; i++) d[i] = INF; 50 while(!q.empty()) q.pop(); 51 d[t] = 0; 52 q.push(make_pair(d[t],t)); 53 while(!q.empty()){ 54 int u = q.top().second; 55 int w = q.top().first; 56 q.pop(); 57 if(w > d[u]) continue; 58 for(int i = tail[u]; ~i; i = e[i].next){ 59 if(d[e[i].to] > d[u] + e[i].w){ 60 d[e[i].to] = d[u] + e[i].w; 61 q.push(make_pair(d[e[i].to],e[i].to)); 62 } 63 } 64 } 65 } 66 int Astar(){ 67 while(!qq.empty()) qq.empty(); 68 memset(cnt,0,sizeof(cnt)); 69 qq.push(node(0,d[s],s)); 70 while(!qq.empty()){ 71 node now = qq.top(); 72 qq.pop(); 73 if(++cnt[now.to] > kth) continue; 74 if(cnt[t] == kth) return now.g; 75 for(int i = head[now.to]; ~i; i = e[i].next) 76 qq.push(node(now.g+e[i].w,d[e[i].to],e[i].to)); 77 78 } 79 return -1; 80 } 81 int main() { 82 int u,v,w; 83 while(~scanf("%d %d",&n,&m)){ 84 memset(head,-1,sizeof(head)); 85 memset(tail,-1,sizeof(tail)); 86 for(int i = 0; i < m; i++){ 87 scanf("%d %d %d",&u,&v,&w); 88 add(u,v,w); 89 } 90 scanf("%d %d %d",&s,&t,&kth); 91 dijkstra(); 92 if(s == t) ++kth; 93 printf("%d\n",Astar()); 94 } 95 return 0; 96 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3983907.html