首页 > 代码库 > hdu 3499 Flight dijkstra 变形
hdu 3499 Flight dijkstra 变形
Problem Description
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There‘s a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?
Input
There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
Output
One line for each test case the least money Shua Shua have to pay. If it‘s impossible for him to finish the trip, just output -1.
Sample Input
4 4 Harbin Beijing 500 Harbin Shanghai 1000 Beijing Chengdu 600 Shanghai Chengdu 400 Harbin Chengdu 4 0 Harbin Chengdu
Sample Output
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.
Author
Edelweiss
Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
思路:分别以起点和终点为起点建立最短路,枚举打折的两个城市,求最短
#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); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。