HDU - 3499 Flight 最短路

HDU - 3499 Flight 最短路

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. 
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
-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. 
题意:一个人坐飞机从起点飞往终点,他有一张优惠卡,可以使某一段行程的机票减半,求使用这张优惠卡的前提下花费的最少钱是多少
一开始我也掉入那个错误的误区里了,先求花费最少的,也就是最短路,然后把这条路上最大的那个减半,这是错误的想法,很容易就找到反例。
正解:先从起点求最短路,然后反向建图,再从终点求最短路。最后遍历每一条边,找起点到 i 的距离加 j 到终点的距离加这条边的一半的最小值,注意输入的是字符串,用个map处理一下。
因为要用两次dij求最短路,有些细节很难处理,我写的代码很low
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=100010;
const int maxm=1000100;
const ll INF=0x3f3f3f3f3f3f3f3f;;
struct edge{
	int u;
	int v;
	int next;
	int w;
}e1[maxm],e2[maxm];
struct node{
	int id;
	ll w;
	node(){};
	node(int _id,ll _w){id=_id,w=_w;};
	bool operator<(const node&other) const
	{
		return w>other.w;
	}
};
ll dis[maxn];
int head1[maxn],vis[maxn],head2[maxn];
int n,m,cnt1,cnt2;
void add_Edge1(int u,int v,int w)
{
	e1[cnt1].u=u;
	e1[cnt1].v=v;
	e1[cnt1].w=w;
	e1[cnt1].next=head1[u];
	head1[u]=cnt1++;
}
void add_Edge2(int u,int v,int w)
{
	e2[cnt2].u=u;
	e2[cnt2].v=v;
	e2[cnt2].w=w;
	e2[cnt2].next=head2[u];
	head2[u]=cnt2++;
}
void Dijsktra(int s,struct edge *e,int *head)
{
    for(int i=1;i<=n;i++)
	{
		vis[i]=false;
		dis[i]=INF;
	} 
	
	priority_queue<node> q;
	dis[s]=0;
	q.push(node(s,dis[s]));
	
	while(!q.empty())
	{
		struct node temp=q.top();
		q.pop();
		if(vis[temp.id]) continue;
		vis[temp.id]=true;
		
		for(int i=head[temp.id];i!=-1;i=e[i].next)
		{
			int to=e[i].v;
			if(dis[to]>dis[temp.id]+e[i].w)
			{
				dis[to]=dis[temp.id]+e[i].w;
				q.push(node(to,dis[to]));
			}
		}
		
	}
}
int main(void)
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		ll diss[maxn];
		getchar();
		int flag=1;
		char a[15],b[15];
		int c;
		cnt1=1;
		cnt2=1;
		memset(head1,-1,sizeof(head1));
		memset(head2,-1,sizeof(head2));
		map<string,int> addr;
		for(int i=1;i<=m;i++)
		{
			scanf("%s%s%d",a,b,&c);
			if(addr[a]==0) addr[a]=flag++;
			if(addr[b]==0) addr[b]=flag++;
			add_Edge1(addr[a],addr[b],c); 
			add_Edge2(addr[b],addr[a],c);
		}
		scanf("%s%s",a,b);
		if(addr[a]==0||addr[b]==0){
			printf("-1\n");
			continue;
		} 
		if(addr[a]==addr[b]){
			printf("\n");
			continue;
		}
		int s=addr[a],end=addr[b];
		Dijsktra(s,e1,head1);
		if(dis[end]==INF){
			printf("-1\n");
			continue;
		}
		for(int i=1;i<=n;i++)
		   diss[i]=dis[i];
		Dijsktra(end,e2,head2);
		ll ans=INF;
		for(int i=1;i<=m;i++)
		{
			int u=e1[i].u;
			int v=e1[i].v;
			ll w=e1[i].w;	
			if(ans>diss[u]+dis[v]+w/2)
			   ans=diss[u]+dis[v]+w/2;
		}
		printf("%I64d\n",ans);	    
	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值