标签:mes head return lin express oid struct char har
走两次最短路然后枚举
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=10005; 4 const int maxm=500005; 5 const int INF=1000000000; 6 7 inline int read() 8 { 9 int x=0,k=1;char c=getchar(); 10 while(c<‘0‘||c>‘9‘) {if(c==‘-‘) k=-1;c=getchar();} 11 while(c>=‘0‘&&c<=‘9‘) x=(x<<3)+(x<<1)+(c^48),c=getchar(); 12 return x*k; 13 } 14 15 struct Edge{ 16 int u,v,w,next; 17 }e[maxm]; 18 19 int head[maxn],cnt,n,m,k,vis[maxn],dis[maxn],d,a[maxn],b[maxn],pre[maxn],apre[maxn],bpre[maxn]; 20 21 struct node{ 22 int w,now; 23 inline bool operator< (const node &x) const 24 { 25 return w>x.w; 26 } 27 }; 28 29 priority_queue<node>q; 30 31 inline void add(int u,int v,int w) 32 { 33 e[++cnt].u=u; 34 e[cnt].v=v; 35 e[cnt].w=w; 36 e[cnt].next=head[u]; 37 head[u]=cnt; 38 } 39 40 void dijikstra(int s) 41 { 42 for(int i=1;i<=n;++i) {dis[i]=INF;pre[i]=0;vis[i]=0;} 43 dis[s]=0; 44 q.push((node){0,s}); 45 while(!q.empty()) 46 { 47 node x=q.top(); 48 q.pop(); 49 int u=x.now; 50 if(vis[u]) continue; 51 vis[u]=1; 52 for(int i=head[u];i;i=e[i].next) 53 { 54 int v=e[i].v; 55 if(dis[v]>dis[u]+e[i].w) 56 { 57 dis[v]=dis[u]+e[i].w; 58 pre[v]=u; 59 q.push((node){dis[v],v}); 60 } 61 } 62 } 63 } 64 65 void init() 66 { 67 memset(head,0,sizeof(head)); 68 cnt=0; 69 } 70 71 void afs(int x) 72 { 73 if(apre[x]) 74 { 75 afs(apre[x]); 76 printf("%d ",x); 77 } 78 return; 79 } 80 81 void bfs(int x) 82 { 83 if(bpre[x]) 84 { 85 printf("%d ",x); 86 bfs(bpre[x]); 87 } 88 return; 89 } 90 91 int main() 92 { 93 int ans,Case=0; 94 while(scanf("%d%d%d",&n,&m,&k)==3) 95 { 96 if(Case) printf("\n"); 97 Case=1; 98 d=read(); 99 for(int i=1,x,y,z;i<=d;++i) 100 { 101 x=read(),y=read(),z=read(); 102 add(x,y,z); 103 add(y,x,z); 104 } 105 dijikstra(m); 106 for(int i=1;i<=n;++i) {a[i]=dis[i];apre[i]=pre[i];} 107 ans=a[k]; 108 dijikstra(k); 109 for(int i=1;i<=n;++i) {b[i]=dis[i];bpre[i]=pre[i];} 110 init(); 111 d=read(); 112 int qi,shi,mid=0; 113 for(int i=1,x,y,z;i<=d;++i) 114 { 115 x=read(),y=read(),z=read(); 116 if(a[x]+b[y]+z<ans) 117 { 118 qi=x;shi=y;mid=x; 119 ans=a[x]+b[y]+z; 120 } 121 if(a[y]+b[x]+z<ans) 122 { 123 qi=y;shi=x;mid=y; 124 ans=a[y]+b[x]+z; 125 } 126 } 127 if(mid) 128 { 129 printf("%d ",m); 130 afs(qi); 131 bfs(shi); 132 printf("%d\n",k); 133 printf("%d\n%d\n",mid,ans); 134 } 135 else 136 { 137 bfs(m); 138 printf("%d\n",k); 139 printf("Ticket Not Used\n%d\n",ans); 140 } 141 } 142 }
标签:mes head return lin express oid struct char har
原文地址:https://www.cnblogs.com/zytwan/p/9931904.html