UVA11374 Airport Express
题意:n个点,m条边,两种边,第二种边只能走其中的一条,问起始位置到终点的最短距离
题解:模板题,只要枚举每一条边,预处理两边的最短路,Dijkstra单源最短路
#include <bits/stdc++.h> #define ll long long #define maxn 100010 #define INF 1e9+7 using namespace std; struct edge{ int from,to,dist; }; struct node{ int d,u; bool operator<(const node& a) const{ return d>a.d; } }; struct dij{ int n,m; vector<int >G[maxn];//从0开始 vector<edge>edges; bool done[maxn]; int d[maxn]; //距离 int p[maxn]; //上一条边,端点判断 void init(int x){//边数,初始化 n = x; for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void add(int a,int b,int w){ edges.push_back((edge){a,b,w}); m = edges.size(); G[a].push_back(m-1); } void dijstra(int st){//计算最短路 for(int i=0;i<n;i++) d[i] = INF; memset(done, 0, sizeof(done)); d[st] = 0; priority_queue<node>q; q.push((node){0, st}); while(!q.empty()){ node fi = q.top();q.pop(); int u = fi.u; if(done[u]) continue; done[u] = 1; for(int i=0;i<G[u].size();i++){ edge& e = edges[G[u][i]]; if(e.dist+d[u]<d[e.to]){ d[e.to] = d[u]+e.dist; p[e.to] = G[u][i]; q.push((node){d[e.to], e.to}); } } } } }ds, de; int s,e; void dfs1(int a){ if(a == s){ cout<<a+1; return ; } edge e = ds.edges[ds.p[a]]; dfs1(e.from); cout<<" "<<a+1; } void dfs2(int a){ if(a == e){ cout<<" "<<a+1; return ; } edge e = de.edges[de.p[a]]; cout<<" "<<a+1; dfs2(e.from); } int main(){ int n, m, k, a, b, c, aaa=0; while(cin>>n>>s>>e){ if(aaa++) cout<<"\n"; s--,e--; ds.init(n);de.init(n); cin>>m; for(int i=0;i<m;i++){ cin>>a>>b>>c; a--,b--; ds.add(a, b, c);ds.add(b, a, c); de.add(a, b, c);de.add(b, a, c); } ds.dijstra(s); de.dijstra(e); cin>>k; int u=-1,v=-1, ans = ds.d[e]; for(int i=0;i<k;i++){ cin>>a>>b>>c; a--,b--; if(ds.d[a]+c+de.d[b] < ans){ ans = ds.d[a]+c+de.d[b]; u = a; v = b; } if(ds.d[b]+c+de.d[a] < ans){ ans = ds.d[b]+c+de.d[a]; u = b; v = a; } } if(u == -1){ dfs1(e);cout<<endl; cout<<"Ticket Not Used\n"; cout<<ans<<endl; } else{ dfs1(u);dfs2(v);cout<<endl; cout<<u+1<<"\n"<<ans<<endl; } } //fclose(stdout); return 0; }
posted on 2017-08-08 23:09 2855669158 阅读(100) 评论(0) 编辑 收藏 举报