hdu3499(分层图最短路)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yu121380/article/details/82384025

Flight

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

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.

解析:这里我用不上dp思想的分层图做,比较容易理解。分层图最短路不懂得可以看我写的博客,有两种做法,读者可以自行选择了解。

#include<bits/stdc++.h>
using namespace std;
 
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=2e5+5;

int n,m;
struct node{
	int v,w;
	node(int v,int w):v(v),w(w) {}
};

struct qnode{
	int to,dis;
	qnode(int to,int dis):to(to),dis(dis) {}
	bool operator <(const qnode &X)const
	{
		return dis>X.dis;
	}
};
ll d[maxn];
bool vis[maxn];
vector<node> ve[maxn];
void dijkstra(int s)
{
	priority_queue<qnode> q;
	mem(d,inf);
	mem(vis,false);

	d[s]=0;
	
	q.push(qnode(s,0));
	while(!q.empty())
	{
		qnode tmp=q.top();q.pop();
		int u=tmp.to;
		if(vis[u])continue;
		vis[u]=true;
		
		for(int i=0; i<ve[u].size(); i++)
		{
			int v=ve[u][i].v;
			int w=ve[u][i].w;
			if(d[v]>d[u]+w)
			{
				d[v]=d[u]+w;
				q.push(qnode(v,d[v]));
			}
		}
	}
}

int main()
{
	ll ans;
	map<string,int> mp;
	string s1,s2;int w;
	int id=1;
	while(cin>>n>>m)
	{
		mp.clear();
		for(int i=0; i<=2*n; i++)ve[i].clear();
		id=1;
		
		for(int i=0; i<m; i++)
		{
			cin>>s1>>s2>>w;
			if(!mp[s1])mp[s1]=id++;
			if(!mp[s2])mp[s2]=id++;
			
			ve[mp[s1]].push_back(node(mp[s2],w));//第一层 
			ve[mp[s1]].push_back(node(mp[s2]+n,w/2));//第一层到第二层 
			ve[mp[s1]+n].push_back(node(mp[s2]+n,w));//第二层 
			
		}
		cin>>s1>>s2;
		if(!mp[s1])mp[s1]=id++;
		if(!mp[s2])mp[s2]=id++;
		
		int s=mp[s1];int t=mp[s2];
		dijkstra(s);
		ans=min(d[t],d[t+n]);

		if(ans>1e17)ans=-1;
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/82384025
今日推荐