UVA 11374 Airport Express(二分+Dijkstra算法)

In a small city called Iokh, a train service, Airport-Express,takes residents to the airport more quickly than other transports.There are two types of trains in Airport-Express,the Economy-Xpress and the Commercial-Xpress. Theytravel at different speeds, take different routes and have differentcosts.Jason is going to the airport to meet his friend. He wantsto take the Commercial-Xpress which is supposed to be faster,but he doesn’t have enough money. Luckily he has a ticketfor the Commercial-Xpress which can take him one stationforward. If he used the ticket wisely, he might end up savinga lot of time. However, choosing the best time to use theticket is not easy for him.Jason now seeks your help. The routes of the two typesof trains are given. Please write a program to find the bestroute to the destination. The program should also tell when the ticket should be used.

Input

The input consists of several test cases. Consecutive cases are separated by a blank line.The first line of each case contains 3 integers, namely N, S and E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ N),which represent the number of stations, the starting point and where the airport is located respectively.There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stationsof the Economy-Xpress. The next M lines give the information of the routes of the Economy-Xpress.Each consists of three integers X, Y and Z (X, Y ≤ N, 1 ≤ Z ≤ 100). This means X and Y areconnected and it takes Z minutes to travel between these two stations.The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections betweenthe stations of the Commercial-Xpress. The next K lines contain the information of the CommercialXpressin the same format as that of the Economy-Xpress.All connections are bi-directional. You may assume that there is exactly one optimal route to theairport. There might be cases where you MUST use your ticket in order to reach the airport.

Output

For each case, you should first list the number of stations which Jason would visit in order. On thenext line, output ‘Ticket Not Used’ if you decided NOT to use the ticket; otherwise, state the stationwhere Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journeyon the last line. Consecutive sets of output must be separated by a blank line.

Sample Input

4 1 4

4

1 2 2

1 3 3

2 4 4

3 4 5

1

2 4 3

Sample Output

1 2 4

2

5


【思路】

题目允许取至多一张商务票用最短时间从起点坐车到终点,当然也可以不用商务票。

把整个图分成两层,一层是经济票,一层是商务票。对经济票生成的图的起点和终点各做一次Dijkstra算法,我们就可以得到所有点到起点和终点的经济票最短路,然后枚举商务票对这些最短路作连接,我们就可以知道总体最优的情况了,跟经济票的起点、终点直接最短路作比较,就能知道是否需要用这张票了。


【代码】

//************************************************************************
// File Name: main.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com 
// Created Time: 2018年03月27日 星期二 20时55分29秒
//************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int MAXN = 505, INF = 0x3f3f3f3f;

struct edge {
	int from, to, dist;

	edge() {}
	edge (int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist) {}
};

struct node {
	int u, d;

	node() {}
	node(int _u, int _d) : u(_u), d(_d) {}

	bool operator<(const node &another) const
	{
		return d > another.d;
	}
};

int n, origin, terminal, m, k;
vector<edge> e;
vector<int> g[MAXN];
int d1[MAXN], d2[MAXN], last1[MAXN], last2[MAXN];
bool done[MAXN];
priority_queue<node> q;
int com[MAXN][MAXN];

void add_edge(int from, int to, int dist)
{
	e.push_back(edge(from, to, dist));
	g[from].push_back(e.size() - 1);
}

void dijkstra(int x)
{
	memset(done, false, sizeof(done));
	if (x == origin){
		memset(d1, 0x3f, sizeof(d1));
		d1[x] = 0;
	}
	else {
		memset(d2, 0x3f, sizeof(d2));
		d2[x] = 0;
	}
	q.push(node(x, 0));
	while (!q.empty()) {
		int u = q.top().u; q.pop();
		if (done[u]) continue;
		done[u] = true;
		for (int i = 0; i < g[u].size(); i++) {
			edge &now = e[g[u][i]];
			if (x == origin && d1[now.to] > d1[u] + now.dist) {
				d1[now.to] = d1[u] + now.dist;
				last1[now.to] = g[u][i];	
				q.push(node(now.to, d1[now.to]));
			}
			else
			if (x == terminal && d2[now.to] > d2[u] + now.dist) {
				d2[now.to] = d2[u] + now.dist;
				last2[now.to] = g[u][i];
				q.push(node(now.to, d2[now.to]));
			}
		}
	}
}

void init()
{
	e.clear();
	for (int i = 1; i <= n; i++) g[i].clear();
	memset(com, 0x3f, sizeof(com));
}

int main()
{
	bool flag = false;
	while (scanf("%d %d %d", &n, &origin, &terminal) == 3) {
		if (!flag)
			flag = true;
		else
			printf("\n");
		init();
		scanf("%d", &m);
		int a, b, c;
		for (int i = 1; i <= m; i++) {
			scanf("%d %d %d", &a, &b, &c);
			add_edge(a, b, c);
			add_edge(b, a, c);
		}
		scanf("%d", &k);
		for (int i = 1; i <= k; i++) {
			scanf("%d %d %d", &a, &b, &c);
			com[a][b] = min(com[a][b], c);
			com[b][a] = com[a][b];
		}
		dijkstra(origin);
		dijkstra(terminal);
		int bg, ed, ans = d1[terminal];
		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++) {
				if (com[i][j] == INF) continue;
				if (ans > d1[i] + d2[j] + com[i][j]) {
					ans = d1[i] + d2[j] + com[i][j];
					bg = i;
					ed = j;
				}
				if (ans > d1[j] + d2[i] + com[i][j]) {
					ans = d1[j] + d2[i] + com[i][j];
					bg = j;
					ed = i;
				}
			}
		if (ans == d1[terminal]) {
			int now = origin;
			while (now != terminal) {
				printf("%d ", now);
				now = e[last2[now]].from;
			}
			printf("%d\n", terminal);
			printf("Ticket Not Used\n%d\n", ans);
		}
		else {
			int cnt = 0, now = bg;
			int go[MAXN];
			while (now != origin) {
				go[++cnt] = now;
				now = e[last1[now]].from;
			}
			go[++cnt] = origin;
			for (int i = cnt; i >= 1; i--) printf("%d ", go[i]);
			now = ed;
			while (now != terminal) {
				printf("%d ", now);
				now = e[last2[now]].from;
			}
			printf("%d\n%d\n%d\n", terminal, bg, ans);
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值