标签:des style http color os io for ar
4 4 Harbin Beijing 500 Harbin Shanghai 1000 Beijing Chengdu 600 Shanghai Chengdu 400 Harbin Chengdu 4 0 Harbin Chengdu
800 -1HintIn the first sample, Shua Shua should use the card on the flight from Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the least total cost 800. In the second sample, there‘s no way for him to get to Chengdu from Harbin, so -1 is needed.
思路:分别以起点和终点为起点建立最短路,枚举打折的两个城市,求最短
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <map> #include <vector> #include <queue> #include <algorithm> using namespace std; const __int64 INF=1e18; const int maxn=100010; struct Edge{ int from,to,dist; Edge(int u,int v,int d):from(u),to(v),dist(d) {} }; struct HeapNode{ int d,u; bool operator < (const HeapNode& rhs) const{ return d > rhs.d; } }; struct Dijkstra{ int n,m; vector<Edge> edges; vector<int> G[maxn]; bool done[maxn]; __int64 d[maxn]; void init(int n) { this->n=n; for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void addEdges(int from,int to,int dist) { edges.push_back(Edge(from,to,dist)); m=edges.size(); G[from].push_back(m-1); } void dijkstra(int s) { priority_queue<HeapNode> Q; for(int i=0;i<n;i++) d[i]=INF; d[s]=0; memset(done,0,sizeof(done)); HeapNode tep; tep.d=0,tep.u=s; Q.push(tep); while (!Q.empty()) { HeapNode x=Q.top();Q.pop(); int u=x.u; if(done[u]) continue; done[u]=true; for(int i=0;i<G[u].size();i++) { Edge& e=edges[G[u][i]]; if(d[e.to]>d[u]+e.dist) { d[e.to]=d[u]+e.dist; tep.d=d[e.to],tep.u=e.to; Q.push(tep); } } } } }; string date; __int64 temp; map<string,int> data; Dijkstra dij1,dij2; int main () { __int64 ans; int tp,a,b,cnt,n,m; //freopen("data.in","r",stdin); while (~scanf("%d%d",&n,&m)) { data.clear(); dij1.init(n); dij2.init(n); cnt=-1; for (int i=1;i<=m;i++) { cin>>date; if(data.find(date)==data.end()) data[date]=++cnt; a=data[date]; cin>>date; if(data.find(date)==data.end()) data[date]=++cnt; b=data[date]; scanf("%d",&tp); dij1.addEdges(a,b,tp); dij2.addEdges(b,a,tp); } cin>>date; if(data.find(date)==data.end()) data[date]=++cnt; a=data[date]; cin>>date; if(data.find(date)==data.end()) data[date]=++cnt; b=data[date]; dij1.dijkstra(a); dij2.dijkstra(b); ans=INF; for(int i=0;i<dij1.edges.size();i++) { Edge tem=dij1.edges[i]; temp=dij1.d[tem.from]+dij2.d[tem.to]+tem.dist/2; ans=ans<temp?ans:temp; } if(ans>=INF) puts("-1"); else printf("%I64d\n",ans); } }
hdu 3499 Flight dijkstra 变形,布布扣,bubuko.com
标签:des style http color os io for ar
原文地址:http://blog.csdn.net/alpc_paul/article/details/38495959