标签:really string iostream queue include front 就会 dom UI
Description
Input
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 #include<cstdio> 9 #define il inline 10 #define ll long long 11 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) 12 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--) 13 #define Max(a,b) ((a)>(b)?(a):(b)) 14 #define Min(a,b) ((a)>(b)?(b):(a)) 15 using namespace std; 16 const int N=200005,inf=233333333; 17 int s,t,n,m,k,w[N],W[N],dis[N],ans[N],tot; 18 int to[N],net[N],h[N],cnt1,To[N],Net[N],H[N],cnt2; 19 struct node{ 20 int f,g,id; 21 bool operator<(const node a)const{return f>a.f;} 22 }; 23 bool vis[N]; 24 priority_queue<node>Q; 25 26 il int gi(){ 27 int a=0;char x=getchar();bool f=0; 28 while((x<‘0‘||x>‘9‘)&&x!=‘-‘)x=getchar(); 29 if(x==‘-‘)x=getchar(),f=1; 30 while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar(); 31 return f?-a:a; 32 } 33 34 il void add(int u,int v,int c){ 35 to[++cnt1]=v,net[cnt1]=h[u],h[u]=cnt1,w[cnt1]=c; 36 To[++cnt2]=u,Net[cnt2]=H[v],H[v]=cnt2,W[cnt2]=c; 37 } 38 39 il void spfa(){ 40 queue<int>q; 41 For(i,1,n) dis[i]=inf; 42 dis[s]=0,vis[s]=1,q.push(s); 43 while(!q.empty()){ 44 int u=q.front();vis[u]=0;q.pop(); 45 for(int i=h[u];i;i=net[i]) 46 if(dis[to[i]]>dis[u]+w[i]){ 47 dis[to[i]]=dis[u]+w[i]; 48 if(!vis[to[i]])q.push(to[i]),vis[to[i]]=1; 49 } 50 } 51 } 52 53 il void Astar(){ 54 if(dis[t]==inf) return; 55 node tmp; 56 tmp.g=0,tmp.f=dis[t],tmp.id=t; 57 Q.push(tmp); 58 while(!Q.empty()){ 59 tmp=Q.top();Q.pop(); 60 if(tmp.id==s) {ans[++tot]=tmp.g;if(tot>=k)return;} 61 for(int i=H[tmp.id];i;i=Net[i]){ 62 node tp; 63 tp.g=tmp.g+W[i]; 64 tp.f=tp.g+dis[To[i]]; 65 tp.id=To[i]; 66 Q.push(tp); 67 } 68 } 69 } 70 71 int main(){ 72 while(scanf("%d%d",&n,&m)==2){ 73 memset(h,0,sizeof(h));cnt1=0; 74 memset(H,0,sizeof(H));cnt2=0; 75 int u,v,c; 76 For(i,1,m) u=gi(),v=gi(),c=gi(),add(u,v,c); 77 s=gi(),t=gi(),k=gi(); 78 if(s==t)k++; 79 spfa(); 80 Astar(); 81 if(tot<k)cout<<-1; 82 else cout<<ans[k]; 83 } 84 return 0; 85 }
标签:really string iostream queue include front 就会 dom UI
原文地址:https://www.cnblogs.com/five20/p/9242853.html