标签:air vector 导航 ret ems const mem col cto
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pii pair<int,int> 4 #define mp make_pair 5 #define inf 0x3f3f3f3f 6 #define piii pair<int,pii> 7 const int maxn=505; 8 9 vector<pii>e1[maxn],e2[maxn]; 10 int dis1[maxn][maxn],dis2[maxn][maxn]; 11 int N,M; 12 bool vis[maxn]; 13 int d[maxn]; 14 void dij(int s,vector<pii> (&e)[maxn]){//值传递会怎样 15 memset(vis,0,sizeof(vis)); 16 memset(d,inf,sizeof(d)); 17 priority_queue<pii,vector<pii>,greater<pii> >q; 18 q.push(mp(0,s)); 19 d[s]=0; 20 while(!q.empty()){ 21 int u=q.top().second;q.pop(); 22 if(vis[u])continue; 23 vis[u]=1; 24 for(pii p:e[u]){ 25 int v=p.first,w=p.second; 26 if(d[v]>d[u]+w){ 27 d[v]=d[u]+w; 28 q.push(mp(d[v],v)); 29 } 30 } 31 } 32 } 33 int d2[maxn][2]; 34 bool vis2[maxn][2]; 35 void dij2(){ 36 memset(vis2,0,sizeof(vis2)); 37 memset(d2,inf,sizeof(d2)); 38 d2[1][0]=0; 39 priority_queue<piii,vector<piii>,greater<piii> >q; 40 q.push(mp(0,mp(1,0))); 41 while(!q.empty()){ 42 piii o=q.top();q.pop(); 43 int u=o.second.first; 44 int type=o.second.second; 45 if(vis2[u][type])continue; 46 vis2[u][type]=1; 47 for(pii p:e1[u]){ 48 int v=p.first,w=p.second; 49 if(w>=0){ 50 if(d2[v][0]>d2[u][type]+w){ 51 d2[v][0]=d2[u][type]+w; 52 q.push(mp(d2[v][0],mp(v,0))); 53 } 54 }else if(type==0){ 55 w*=-1; 56 if(d2[v][1]>d2[u][type]+w){ 57 d2[v][1]=d2[u][type]+w; 58 q.push(mp(d2[v][1],mp(v,1))); 59 } 60 } 61 } 62 } 63 } 64 void spfa(){} 65 int main(){ 66 int a,b,c,t; 67 ios::sync_with_stdio(false); 68 cin>>N>>M; 69 for(int i=1;i<=N;++i){ 70 for(int j=i;j<=N;++j){ 71 if(i==j)dis1[i][j]=dis2[i][j]=0; 72 else{ 73 dis1[i][j]=dis1[j][i]=inf; 74 dis2[i][j]=dis2[j][i]=inf; 75 } 76 } 77 } 78 while(M--){ 79 cin>>t>>a>>b>>c; 80 if(t==0){ 81 e1[a].push_back(mp(b,c)); 82 e1[b].push_back(mp(a,c)); 83 }else if(t==1){ 84 if(dis2[a][b]>c)dis2[a][b]=dis2[b][a]=c; 85 e2[a].push_back(mp(b,c)); 86 e2[b].push_back(mp(a,c)); 87 } 88 } 89 for(int i=1;i<=N;++i){ 90 dij(i,e2); 91 for(int j=1;j<=N;++j){ 92 if(i!=j && d[j]!=inf && d[j]<=1005){ 93 e1[i].push_back(mp(j,d[j]*d[j]*-1)); 94 } 95 } 96 } 97 dij2(); 98 cout<<min(d2[N][0],d2[N][1])<<endl; 99 return 0; 100 }
标签:air vector 导航 ret ems const mem col cto
原文地址:https://www.cnblogs.com/zzqc/p/12384928.html