hdu 3499 Flight dijkstra 变形

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. 

Author

Edelweiss

Source

2010
ACM-ICPC Multi-University Training Contest(7)——Host by HIT

思路:分别以起点和终点为起点建立最短路,枚举打折的两个城市,求最短

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const __int64 INF=1e18;
const int maxn=100010;
struct Edge{
	int from,to,dist;
	Edge(int u,int v,int d):from(u),to(v),dist(d) {}
};

struct HeapNode{
	int d,u;
	bool operator < (const HeapNode& rhs) const{
		return d > rhs.d;
	}
};
struct Dijkstra{
	int n,m;
	vector<Edge> edges;
	vector<int> G[maxn];
	bool done[maxn];
	__int64 d[maxn];

	void init(int n)
	{
		this->n=n;
		for(int i=0;i<n;i++) G[i].clear();
		edges.clear();
	}

	void addEdges(int from,int to,int dist)
	{
		edges.push_back(Edge(from,to,dist));
		m=edges.size();
		G[from].push_back(m-1);
	}

	void dijkstra(int s)
	{
		priority_queue<HeapNode> Q;
		for(int i=0;i<n;i++) d[i]=INF;
		d[s]=0;
		memset(done,0,sizeof(done));
		HeapNode tep;
		tep.d=0,tep.u=s;
		Q.push(tep);
		while (!Q.empty())
		{
			HeapNode x=Q.top();Q.pop();
			int u=x.u;
			if(done[u]) continue;
			done[u]=true;
			for(int i=0;i<G[u].size();i++)
			{
				Edge& e=edges[G[u][i]];
				if(d[e.to]>d[u]+e.dist)
				{
					d[e.to]=d[u]+e.dist;
					tep.d=d[e.to],tep.u=e.to;
					Q.push(tep);
				}
			}
		}
	}

};

string date;

__int64 temp;
map<string,int> data;
Dijkstra dij1,dij2;
int main ()
{
	__int64 ans;
	int tp,a,b,cnt,n,m;
	//freopen("data.in","r",stdin);
    while (~scanf("%d%d",&n,&m))
    {
		data.clear();
		dij1.init(n);
		dij2.init(n);
		cnt=-1;
        for (int i=1;i<=m;i++)
        {

            cin>>date;
			if(data.find(date)==data.end())
            	data[date]=++cnt;
			a=data[date];
            cin>>date;
			if(data.find(date)==data.end())
            	data[date]=++cnt;
			b=data[date];
            scanf("%d",&tp);
			dij1.addEdges(a,b,tp);
			dij2.addEdges(b,a,tp);
        }
        cin>>date;
		if(data.find(date)==data.end())
			data[date]=++cnt;
        a=data[date];
		cin>>date;
		if(data.find(date)==data.end())
			data[date]=++cnt;
        b=data[date];
		dij1.dijkstra(a);
		dij2.dijkstra(b);
        ans=INF;
		for(int i=0;i<dij1.edges.size();i++)
		{
			Edge tem=dij1.edges[i];
			temp=dij1.d[tem.from]+dij2.d[tem.to]+tem.dist/2;
			ans=ans<temp?ans:temp;
		}
        if(ans>=INF) puts("-1");
        else printf("%I64d\n",ans);
    }
}

hdu 3499 Flight dijkstra 变形

时间: 2024-07-29 19:56:45

hdu 3499 Flight dijkstra 变形的相关文章

HDU 3499 Flight spfa+dp

Flight Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Recently, Shua Shua had a big

HDU - 3499 Flight 双向SPFA+枚举中间边

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

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

hdu 1596(Floyd 变形)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6911    Accepted Submission(s): 2450 Problem Description XX星球有很多城市,每个城市之间有一条或

hdu 1217 (Floyd变形)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4430    Accepted Submission(s): 2013 Problem Description Arbitrage is the use of discr

hdu 4381(背包变形)

题意: 给定n个块,编号从1到n,以及m个操作,初始时n个块是白色. 操作有2种形式: 1 ai xi : 从[1,ai]选xi个块,将这些块涂白. 2 ai xi:从[ai,n]选xi个块,将这些块涂白. 可以忽略某些操作且如果区间内没有足够的黑块(黑块用于涂白),则不能进行这个操作. 分析: 写写画画一看就知道这道题是一个背包问题. “恰好装满背包”. 以下摘自题解: 本题难点在于正确处理两种操作,不妨假设只有一种操作,那么这种操作如果是1的话那么就把操作按照a从小到大排序,每次都尽量往最左

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

【lightoj-1002】Country Roads(dijkstra变形)

light1002:传送门 [题目大意] n个点m条边,给一个源点,找出源点到其他点的‘最短路’ 定义:找出每条通路中最大的cost,这些最大的cost中找出一个最小的即为‘最短路’,dijkstra变形.dis[i]为s->i的‘最短路’ 1 #include<bits/stdc++.h> 2 int mp[505][505],dis[505],vis[505]; 3 using namespace std; 4 int n; 5 void dij(int s) 6 { 7 int i

HDU1596 find the safest road---(最短路径dijkstra,#变形#)

http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析: 题目要找一条安全度最高的路,安全度计算方法    Safe(P) = s(e1)*s(e2)-*s(ek) e1,e2,ek是P 上的边 在Dijkstra算法的基础上稍加改动 #include "cstdio" #include "cstring" #include "algorithm" using namespace std; #defin