HDU 3499 Flight

Flight

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3219    Accepted Submission(s): 676


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. 
 

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.
 题意: n个城市,m个路线,
接下来是m行  每一行有两个城市表示从a飞到b城市票价为x元, Shua Shua有一张优惠券,可以使票价打五折,问他要从起始点到目的地最少
花多少银子。
思路就是正向建图,反向建图,然后枚举每一个票价,假设这个机票是由A城市飞到B城市,那么你就需要计算从起始点START城市到A城市的花费
加上B城市到END城市的花费,再加上这个机票打完折后的价格。
这里我用dis数组记录START城市到其他城市的最小花费,用dist城市记录从 END城市到其他城市的最小花费(其实就是其他城市到END城市的最小花费)
另外这里如果用s[][]数组存城市的话,好像会超时,,,所以用map映射一下。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#define N 100005
#define M 500005
const long long inf=1LL<<60;
using namespace std;

struct node
{
	int val;
	int to;
	int next;
}edge[M],edge1[M];

map<string,int> s;


int cnt,st,en,cout1,cout2;
int n,m;
int vis[N];
long long dis[N],dist[N];
int head[N],head1[N];

inline int getnum(char *ss)
{
    if(s.count(ss)) return s[ss];
    else return s[ss]=cnt++;
}

void add(int u,int v,int w)
{
	edge[++cout1].to=v;
	edge[cout1].val=w;
	edge[cout1].next=head[u];
	head[u]=cout1;
	return ;
}

void add1(int u,int v,int w)
{
	edge1[++cout2].to=v;
	edge1[cout2].val=w;
	edge1[cout2].next=head1[u];
	head1[u]=cout2;
	return ;
}


/*
void spfa(int x)
{
	memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
    	dis[i]=inf;
	}
    vis[x]=1;
    dis[x]=0;
    int cur=x;
    queue< int >q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        vis[cur]=0;
        for(int i=head[cur];i!=-1;i=edge[i].next)
        {
            int id=edge[i].to;
            int ww=edge[i].val;
            if(dis[id]>ww+dis[cur])
            {
                dis[id]=ww+dis[cur];
                if(!vis[id])
                {
                    vis[id]=1;
                    q.push(id);

                }
            }
        }
    }
    return ;
}

void spfa1(int x)
{
	int cur;
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
	{
		dist[i]=inf;
	}
	cur=x;
	vis[cur]=1;
	dist[cur]=0;
	queue< int >q;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		//printf("cur : %d\n",cur);
		vis[cur]=0;
		q.pop();
		for(int i=head1[cur];i!=-1;i=edge1[i].next)
		{
			int id=edge1[i].to;
            int ww=edge1[i].val;
            if(dist[id]>ww+dist[cur])
            {
                dist[id]=ww+dist[cur];
                if(!vis[id])
                {
                    vis[id]=1;
                    q.push(id);
                    
                }
            }
		}
	}
}

*/

void spfa(int x)
{
	int i,j;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++)
	{
		dis[i]=inf;
	}
	vis[x]=1;
	dis[x]=0;
	queue< int >q;
	q.push(x);
	int cur;
	while(!q.empty())
	{
		cur=q.front();
		vis[cur]=0;
		q.pop();
		for(i=head[cur];i!=-1;i=edge[i].next)
		{
			int vv=edge[i].to;
			if(dis[vv]>dis[cur]+edge[i].val)
			{
				dis[vv]=dis[cur]+edge[i].val;
				if(!vis[vv])
				{
					vis[vv]=1;
					q.push(vv);
				}
			}
		}
	}
	return ;
}

void spfa1(int x)
{
	int i,j;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++)
	{
		dist[i]=inf;
	}
	vis[x]=1;
	dist[x]=0;
	queue< int >q;
	q.push(x);
	int cur;
	while(!q.empty())
	{
		cur=q.front();
		vis[cur]=0;
		q.pop();
		for(i=head1[cur];i!=-1;i=edge1[i].next)
		{
			int vv=edge1[i].to;
			if(dist[vv]>dist[cur]+edge1[i].val)
			{
				dist[vv]=dist[cur]+edge1[i].val;
				if(!vis[vv])
				{
					vis[vv]=1;
					q.push(vv);
				}
			}
		}
	}
	return ;
}



int main()
{
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		s.clear();
		memset(head,-1,sizeof(head));
	memset(head1,-1,sizeof(head1));
	char s1[55],s2[55];
	int i,j;
	int uu,vv,w;
	cnt=0;
	cout1=0;
	cout2=0;
	for(i=1;i<=m;i++)
	{
		scanf("%s%s%d",s1,s2,&w);
		uu=getnum(s1);
		vv=getnum(s2);
		add(uu,vv,w);
		add1(vv,uu,w);
	}
	scanf("%s%s",s1,s2);
	st=getnum(s1);
	en=getnum(s2);
	spfa(st);
	spfa1(en);
	long long  min=inf;
	for(i=1;i<=n;i++)
	{
		int k=head[i];
		while(k!=-1)
		{
			uu=i;
			vv=edge[k].to;
			w=edge[k].val;
			long long num=dis[uu]+dist[vv]+w/2;
			if(num<min) min=num;
			k=edge[k].next;
		}
	}
	if(min==inf) printf("-1\n");
	else printf("%lld\n",min);
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值