uva 11374 Airport Express (Dijkstra)



题意:在Iokh市中,机场快线是市民从市内去机场的首选交通工具。机场快线分为经济线和商业线两种,线路,速度和价钱都不同。你有一张商业线车票,可以做一站商业线,而其他时候只能乘坐经济线。假设换乘时间忽略不计,你的任务是找一条去机场最快的路线。。

分析:枚举商业线T(a,b),则总时间为f(a)+T(a,b)+g(b);f和g用两次dijkstra来计算,以S为起点的dijkstra和以E为起点的dijkstra;

注意:有可能只做慢车到达不了终点,这时必须做某站快车,如果按照坐慢车一定能到达终点然后从起点打印路径可能会出错,因为此时没有一条完整路径,这时从换到的站到终点应从另一侧打印

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#define eps 1e-6
#define LL long long
using namespace std;
const int maxn = 550;
const int INF = 100000000;
int N, S, E, M, K;
vector<pair<int, int> > kuai[maxn];
int kase;
//Dijkstra
struct Edge {
	int from, to, dist;
	Edge(int u = 0, int v = 0, int d = 0) : 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];            //是否已经永久编号
	int d[maxn];                //s到各个点的距离
	int p[maxn];                //最短路中的上一条边

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

	void AddEdge(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) {            //求s到所有点的距离,0表示起点,1表示终点
		priority_queue<HeapNode> Q;
		for(int i = 0; i < n; i++) d[i] = INF;
		d[s] = 0;
		memset(done, 0, sizeof(done));
		Q.push((HeapNode){0, s});
		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;
					p[e.to] = G[u][i];
					Q.push((HeapNode){d[e.to], e.to});
				}
			}
		}
	} 

	void dfs(int S, int E) {
		if(S == E) {
			printf("%d", E+1);
			return;
		}
		int u = p[E];
		int v = edges[u].from;
		dfs(S, v);
		printf(" %d", E+1);
	}
} Dij[2];

void init() {
	S--; E--;
	cin >> M;
	int u, v, dist;
	Dij[0].init(N);
	Dij[1].init(N);
	for(int i = 0; i < N; i++) kuai[i].clear();
	for(int i = 0; i < M; i++) {
		scanf("%d%d%d", &u, &v, &dist);
		u--; v--;
		Dij[0].AddEdge(u, v, dist); Dij[1].AddEdge(u, v, dist);
		Dij[0].AddEdge(v, u, dist); Dij[1].AddEdge(v, u, dist);
	}
	Dij[0].dijkstra(S);
	Dij[1].dijkstra(E);
	cin >> K;
	for(int i = 0; i < K; i++) {
		scanf("%d%d%d", &u, &v, &dist);
		u--; v--;
		kuai[u].push_back(make_pair(v, dist));
		kuai[v].push_back(make_pair(u, dist));
	}
}

void solve() {
	if(kase) cout << endl;
	kase++;
	int ans = INF, huancheng, huandao;
	for(int i = 0; i < N; i++) {
		int sz = kuai[i].size();
		for(int j = 0; j < sz; j++) {
			if(ans > Dij[0].d[i]+Dij[1].d[kuai[i][j].first]+kuai[i][j].second) {
				huancheng = i; huandao = kuai[i][j].first;
				ans = Dij[0].d[i]+Dij[1].d[kuai[i][j].first]+kuai[i][j].second;
			}
		}
	}
	if(ans > Dij[0].d[E]) {
		Dij[0].dfs(S, E);
		cout << endl;
		cout << "Ticket Not Used" << endl;
		cout << Dij[0].d[E] << endl;
	}
	else {
		Dij[0].dfs(S, huancheng);
		int pos = huandao;
		while(pos != E) {
			printf(" %d", pos+1);
			pos = Dij[1].edges[Dij[1].p[pos]].from;
		}
		printf(" %d\n", E+1);
		cout << huancheng+1 << endl;
		cout << ans << endl;
	}
}

int main() {
	//freopen("input.txt", "r", stdin);
	while(scanf("%d%d%d", &N, &S, &E) == 3) {
		init();
		solve();
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-19 00:02:18

uva 11374 Airport Express (Dijkstra)的相关文章

uva 11374 Airport Express(最短路)

uva 11374 Airport Express 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

UVA 11374 - Airport Express(dijstra)

UVA 11374 - Airport Express 题目链接 题意:给定一些经济线,和一些商务线,商务线最多坐一次,每个线有一个时间,问最短时间 思路:从起点,终点各做一次dijstra,然后枚举商务线,就可以算出总时间,最后求出总时间最少 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; #define INF

UVA 11374 Airport Express(优先队列优化dijstra + 枚举)

UVA Airport Express 题意:在Iokh市机场快线分为经济线和商业线.线路和速度价格都不同.你只有一张商业线车票,即最多只能坐一站商业线,其他时候只能坐经济线.找出一条去机场最快的线路. 思路:因为商业线只能坐一站,假如乘坐一条商业线(a,b),那么起点到a,b到终点都必须是最短路.所以先预处理起点和终点到其他所有点的最短路,分别记为f()和g(),两次dijstra即可.那么我们只需枚举每条商业线求出所有的f(a)+T(a,b)+g(b)然后求最小即可. 1w是TLE,改成了优

uva 11374 Airport Express(spfa 邻接表+队列)

Problem D: Airport Express 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 theCommercial-Xpress

UVA 11374 Airport Express (最短路dijkstra+枚举+边的输出)

题意:给你一个数n,s,e,n为有多少个车站,s,e是起点和终点,接下来有m条经济路线,再接下来有k条商业线,你最多只能座一条商业线,现在要你求出从s到e的最短路,并输出它所经过的节点还有座商业线的车站. 思路:实际上这道题就是考你对dijkstra的理解了,其中d数组的含意是起点到第i个点的最短距离,那么每次寻找商业路线的时候,是不是可以比较d[e]跟从起点到点u的最小值+从终点到v的最小值+w[u][v].最后我们可以根据递归输出路径了. AC代码: #include<cstdio> #i

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express 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 Comm

UVA 11374 Airport Express 机场快线 Dijistra+路径

题目链接:UVA 11374 Airport Express Airport Express Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Expr

UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

题意:给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: (1)简单易操作方法:既然第二个集合的边只能有1条,就穷举下这些边,可能的边集进行求最短路,同时记录3个答案.复杂度是O(m*k). (2)时间复杂度低:不妨先求从s到每个其他点的距离d1[i],再求e到其他每个点的距离d2[i],接下来穷举第二个集合中的每条边u-v,那么最短距离为d1[u]+di

uva 10986 Sending email (dijkstra)

uva 10986 Sending email "A new internet watchdog is creating a stir in Springfield. Mr. X, if that is his real name, has come up with a sensational scoop."Kent Brockman There are n SMTP servers connected by network cables. Each of the m cables c