标签:source welcome ftl bsp ota iss cap comm using
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30754 | Accepted: 8394 |
Description
Input
Output
Sample Input
2 2 1 2 5 2 1 4 1 2 2
Sample Output
14
Source
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <queue> 6 7 using namespace std; 8 9 const int INF(0x3f3f3f3f); 10 const int M(100005); 11 const int N(1005); 12 int n,m,s,t,k; 13 int sumedge,had[N],hed[N]; 14 struct Edge 15 { 16 int v,next,w; 17 }edge1[M<<1],edge2[M<<1]; 18 inline void ins(int u,int v,int w) 19 { 20 edge1[++sumedge].v=v; 21 edge1[sumedge].next=hed[u]; 22 edge1[sumedge].w=w; 23 hed[u]=sumedge; 24 edge2[sumedge].v=u; 25 edge2[sumedge].next=had[v]; 26 edge2[sumedge].w=w; 27 had[v]=sumedge; 28 29 } 30 31 int dis[N]; 32 bool inq[N]; 33 void SPFA(int s) 34 { 35 for(int i=1;i<=n;i++) dis[i]=INF; 36 queue<int>que; que.push(s); 37 inq[s]=true; dis[s]=0; 38 for(;!que.empty();) 39 { 40 int u=que.front(); 41 que.pop(); inq[u]=0; 42 for(int v,i=had[u];i;i=edge2[i].next) 43 { 44 v=edge2[i].v; 45 if(dis[v]>dis[u]+edge2[i].w) 46 { 47 dis[v]=dis[u]+edge2[i].w; 48 if(!inq[v]) que.push(v),inq[v]=1; 49 } 50 } 51 } 52 } 53 54 struct Node 55 { 56 int g,f,to; 57 bool operator < (const Node x) const 58 { 59 if(f==x.f) return g>x.g; 60 return f>x.f; 61 } 62 }; 63 int Astar() 64 { 65 priority_queue<Node>que; 66 if(dis[s]==INF) return -1; 67 int cnt=0; Node now,v; 68 now.f=dis[s];now.g=0;now.to=s; 69 que.push(now); 70 for(;!que.empty();) 71 { 72 now=que.top(); que.pop(); 73 if(now.to==t) cnt++; 74 if(cnt==k) return now.g; 75 for(int i=hed[now.to];i;i=edge1[i].next) 76 { 77 v.to=edge1[i].v; 78 v.g=now.g+edge1[i].w; 79 v.f=v.g+dis[edge1[i].v]; 80 que.push(v); 81 } 82 } 83 return -1; 84 } 85 86 int main() 87 { 88 scanf("%d%d",&n,&m); 89 for(int u,v,w;m--;) 90 { 91 scanf("%d%d%d",&u,&v,&w); 92 ins(u,v,w); 93 } 94 scanf("%d%d%d",&s,&t,&k); 95 if(s==t) k++; SPFA(t); 96 printf("%d\n",Astar()); 97 return 0; 98 }
标签:source welcome ftl bsp ota iss cap comm using
原文地址:http://www.cnblogs.com/Shy-key/p/7416331.html