hdu 3499

>>>>>>>> >>>>>  原题链接

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. 
        

 小明拥有一次机票半价的机会,给出每个航班(有向图)的起始点和价格,最后给出起点和目的地,求到达目的地的最小花费。

开始感觉无从下手,难不成为了把一条边的权值变成半价要重新建一张图然后再用最短路?后来想起来可以从起点开始深搜,找到到达终点的每一条路径计算这条路径上在最长边时半价的花费,提交之后超时。后来发现算错了时间复杂度,然后我去问大佬怎么做,他只说了枚举每个边,我还是不会,那不就跟我的初次想法一样吗,过了几秒突然反应过来其实求两个最短路就可以了,一个是以起点开始求到每个点的最短路,然后再以终点开始求到每个点的最短路,这时枚举每条边,假设这条边从u到v权值是w,那么只需把起点到u的距离加上从终点到v的距离再加上w/2就完成了一次枚举,最后取一个最小值就是答案

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
const ll INF = 1e12;
const int M = 1e5 + 5;
ll dis1[M], dis2[M];
int n, m, cnt;
struct Edge
{
	int u, v;
	ll w;
};
struct Dij
{
	int num;
	ll dist;
	bool operator < (const Dij &a)const
	{
		return dist > a.dist;
	}
};
struct Adj
{
	int num;
	ll w;
};
vector<Edge> vt;
vector<Adj> adj[M];
vector<Adj> adj_f[M];
map<string, int> ma;
void Dijstra1(int st)
{
	Dij tmp;
	priority_queue<Dij> q;
	for(int i=1;i<=n;i++)
		dis1[i] = INF;
	dis1[st] = 0;
	tmp.num = st;
	tmp.dist = 0;
	q.push(tmp);
	while(!q.empty())
	{
		vector<Adj>::iterator it;
		for(it=adj[q.top().num].begin();it!=adj[q.top().num].end();it++)
		{
			if(dis1[it->num] > dis1[q.top().num] + it->w)
			{
				dis1[it->num] = dis1[q.top().num] + it->w;
				tmp.num = it->num;
				tmp.dist = dis1[it->num];
				q.push(tmp);
			}
		}
		q.pop();
	}
}
void Dijstra2(int st)
{
	Dij tmp;
	priority_queue<Dij> q;
	for(int i=1;i<=n;i++)
		dis2[i] = INF;
	dis2[st] = 0;
	tmp.num = st;
	tmp.dist = 0;
	q.push(tmp);
	while(!q.empty())
	{
		vector<Adj>::iterator it;
		for(it=adj_f[q.top().num].begin();it!=adj_f[q.top().num].end();it++)
		{
			if(dis2[it->num] > dis2[q.top().num] + it->w)
			{
				dis2[it->num] = dis2[q.top().num] + it->w;
				tmp.num = it->num;
				tmp.dist = dis2[it->num];
				q.push(tmp);
			}
		}
		q.pop();
	}
}
int main()
{
	ll w, tmp, ans;
	int t1, t2;
	Adj tmpa;
	Edge tmpe;
	char u[15], v[15];
	while(~scanf("%d%d", &n, &m))
	{
		ma.clear();
		vt.clear();
		for(int i=1;i<=n;i++)
		{
			adj[i].clear();
			adj_f[i].clear();
		}
		cnt = 1;
		for(int i=1;i<=m;i++)
		{
			scanf("%s %s %lld", u, v, &w);
			if(ma[u]==0)
				ma[u] = cnt++;
			if(ma[v]==0)
				ma[v] = cnt++;
			tmpa.num = ma[v];
			tmpa.w = w;
			adj[ma[u]].push_back(tmpa);
			tmpa.num = ma[u];
			adj_f[ma[v]].push_back(tmpa);
			tmpe.u = ma[u];
			tmpe.v = ma[v];
			tmpe.w = w;
			vt.push_back(tmpe);
		}
		scanf("%s %s", u, v);
		if(ma[u]==0 || ma[v]==0)
		{
			printf("-1\n");
			continue;
		}
		Dijstra1(ma[u]);
		Dijstra2(ma[v]);
		ans = INF;
		vector<Edge>::iterator it;
		for(it=vt.begin();it!=vt.end();it++)
		{
			t1 = it->u;
			t2 = it->v;
			tmp = dis1[t1] + dis2[t2] + it->w / 2;
			ans = min(ans, tmp);
		}
		if(ans>=INF)
			printf("-1\n");
		else
			printf("%lld\n", ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值