hdu 3499 Flight (最短路径)
2013-11-19 11:03
204 查看
Flight
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2014 Accepted Submission(s): 428
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu
4 0
Harbin Chengdu
[align=left]Sample Output[/align]
800
-1
Hint
In 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.
[align=left]Author[/align]
Edelweiss
[align=left]Source[/align]
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
[align=left]Recommend[/align]
zhouzeyong | We have carefully selected several similar problems for you: 3501 3502 3503 3504 3505
题意:
在N个点,M条带权边的图上,查询从点s到点e的最短路径,不过,可以有一次机会可以把一条边的权值变成原来的一半。
小菜代码(双向求解,G++不能过...):
//3765MS 28756K 2269 B G++ //转载: http://blog.csdn.net/shoutmon/article/details/8583984 /* 思路: 1.先正向建图,以a为源点跑Dijkstra 2.再反向建图,以b为源点跑Dijkstra 3.枚举边(作为花费变为一半的边),从a到这条边的起点u使用正向建图的结果,从这条边的终点v使用反向建图的结果,然后再加上这条边边权的一半,就得到这条边花费变为一半时候的总花费。 4.将枚举结果取最小值即为最小花费 5.注意输入是字符串,可以用map */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<map> using namespace std; typedef __int64 ll; const int N=100010; const int M=500010; const ll inf=1LL<<60; struct node { int to; ll dis; node *next; }E[M<<1],*G1 ,*G2 ,*head; int n,m,num; ll d1 ,d2 ; bool inq ; map<string,int> dict; inline void add(int a,int b,ll c,node *G[]) { head->to=b; head->dis=c; head->next=G[a]; G[a]=head++; } inline int change(char *s) { if(dict.count(s)) return dict[s]; else return dict[s]=num++; } void SPFA(int s,ll d[],node *G[]) { deque<int> Q; Q.push_back(s); memset(inq,false,sizeof(inq)); fill(d,d+N,inf); d[s]=0; int to; ll dis; while(!Q.empty()) { int u=Q.front(); Q.pop_front(); inq[u]=false; for(node *p=G[u];p;p=p->next) { to=p->to; dis=p->dis; if(d[to]>d[u]+dis) { d[to]=d[u]+dis; if(!Q.empty()) { if(d[to]>d[Q.front()]) Q.push_back(to); else Q.push_front(to); } else Q.push_back(to); } } } } int main() { char s1[20],s2[20]; while(~scanf("%d%d",&n,&m)) { num=0; dict.clear(); memset(G1,NULL,sizeof(G1)); memset(G2,NULL,sizeof(G2)); head=E; int s,t; ll dis; for(int i=0;i<m;i++) { scanf("%s %s %I64d",s1,s2,&dis); s=change(s1),t=change(s2); add(s,t,dis,G1); add(t,s,dis,G2); } scanf("%s %s",s1,s2); s=dict[s1],t=dict[s2]; SPFA(s,d1,G1); SPFA(t,d2,G2); ll ans=inf; for(int i=0;i<n;i++) { for(node *p=G1[i];p;p=p->next) { int j=p->to; if(d1[i]<inf && d2[j]<inf) ans=min(ans,d1[i]+d2[j]+(p->dis)/2); } } if(ans==inf) printf("-1\n"); else printf("%I64d\n",ans); } return 0; }
View Code
相关文章推荐
- hdu1596 find the safest road(最短路径floyd)
- hdu2544——最短路径
- HDU 3790 最短路径问题
- 【最短路径dijkstra算法】HDU 1874---畅通工程续
- HDU-3790-最短路径问题
- HDU 3339 In Action (最短路径+DP(01背包))
- hdu(1874)最短路径问题
- hdu 1385 spfa和floyd,dijkstra记录最短最小字典序的路径
- hdu 2544最短路径
- HDU-3790-最短路径
- (step6.2.4)hdu 2680(Choose the best route——最短路径)
- HDU-2544最短路径
- 【最短路径-Floyd/Bellman_Ford】hdu 1217 Arbitrage
- HDU 1874 畅通工程续(Dijkstra算法求最短路径)
- hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)
- hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)
- hdu 3790 最短路径问题 (Djikstra)
- hdu 2066 一个人的旅行 最短路径dijkstra算法
- (阶段三 dijkstra算法温习1.1)HDU 2544 最短路(利用dijkstra算法计算两个节点间的最短路径)
- 【最短路径-Dis】hdu 1311 Relative Relatives