UVA 11374 - Airport Express(dijstra)

UVA 11374 - Airport Express

题目链接

题意:给定一些经济线,和一些商务线,商务线最多坐一次,每个线有一个时间,问最短时间

思路:从起点,终点各做一次dijstra,然后枚举商务线,就可以算出总时间,最后求出总时间最少

代码:

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

#define INF 0x3f3f3f3f

const int MAXNODE = 505;

struct Edge {
	int u, v, dist;
	Edge() {}
	Edge(int u, int v, int dist) {
		this->u = u;
		this->v = v;
		this->dist = dist;
	}
};

struct HeapNode {
	int d, u;
	HeapNode() {}
	HeapNode(int d, int u) {
		this->d = d;
		this->u = u;
	}
	bool operator < (const HeapNode& c) const {
		return d > c.d;
	}
};

struct Dijkstra {
	int n, m;
	vector<Edge> edges;
	vector<int> g[MAXNODE];
	bool done[MAXNODE];
	int d[MAXNODE], p[MAXNODE];

	void init(int tot) {
		n = tot;
		for (int i = 0; i < n; i++)
			g[i].clear();
		edges.clear();
	}

	void add_Edge(int u, int v, int dist) {
		edges.push_back(Edge(u, v, dist));
		m = edges.size();
		g[u].push_back(m - 1);
	}

	void print(int s, int e) {//shun xu
		if (s == e) {
			printf("%d", e + 1);
			return;
		}
		print(s, edges[p[e]].u);
		printf(" %d", e + 1);
	}

	void print2(int s, int e) {//ni xu
		if (s == e) {
			printf("%d\n", e + 1);
			return;
		}
		printf("%d ", e + 1);
		print2(s, edges[p[e]].u);
	}

	void dijkstra(int s) {
		priority_queue<HeapNode> Q;
		for (int i = 0; i < n; i++) d[i] = INF;
		d[s] = 0;
		memset(done, false, 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.v] > d[u] + e.dist) {
					d[e.v] = d[u] + e.dist;
					p[e.v] = g[u][i];
					Q.push(HeapNode(d[e.v], e.v));
				}
			}
		}
	}
} gao1, gao2;

const int M = 2005;

int n, s, e;
Edge em[M];

int main() {
	int bo = 0;
	while (~scanf("%d%d%d", &n, &s, &e)) {
		if (bo) printf("\n");
		else bo = 1;
		gao1.init(n);
		gao2.init(n);
		s--; e--;
		int m, x, y, z;
		scanf("%d", &m);
		while (m--) {
			scanf("%d%d%d", &x, &y, &z);
			x--; y--;
			gao1.add_Edge(x, y, z);
			gao1.add_Edge(y, x, z);
			gao2.add_Edge(x, y, z);
			gao2.add_Edge(y, x, z);
		}
		scanf("%d", &m);
		for (int i = 0; i < m; i++) {
			scanf("%d%d%d", &x, &y, &z);
			x--; y--;
			em[i * 2] = Edge(x, y, z);
			em[i * 2 + 1] = Edge(y, x, z);
		}
		gao1.dijkstra(s);
		gao2.dijkstra(e);
		int ans = gao1.d[e];
		int ss = e, ee = -1;
		for (int i = 0; i < 2 * m; i++) {
			int u = em[i].u, v = em[i].v, dist = em[i].dist;
			int sum = gao1.d[u] + gao2.d[v] + dist;
			if (sum < ans) {
				ans = sum;
				ss = u;
				ee = v;
			}
		}
		gao1.print(s, ss);
		if (ee != -1) {
			printf(" ");
			gao2.print2(e, ee);
		}
		else printf("\nTicket Not Used\n");
		if (ee != -1) printf("%d\n", ss + 1);
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-10-19 00:02:17

UVA 11374 - Airport Express(dijstra)的相关文章

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 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)

 题意:在Iokh市中,机场快线是市民从市内去机场的首选交通工具.机场快线分为经济线和商业线两种,线路,速度和价钱都不同.你有一张商业线车票,可以做一站商业线,而其他时候只能乘坐经济线.假设换乘时间忽略不计,你的任务是找一条去机场最快的路线.. 分析:枚举商业线T(a,b),则总时间为f(a)+T(a,b)+g(b);f和g用两次dijkstra来计算,以S为起点的dijkstra和以E为起点的dijkstra: 注意:有可能只做慢车到达不了终点,这时必须做某站快车,如果按照坐慢车一定能到达

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 - 11374 Airport Express(dijkstra)

题目大意:机场快线分为经济线和商业线两种,线路,速度和价钱都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能坐经济线 现在给你起点和终点,要求你找出一条最快的线路 解题思路:不得不说,这题还是有点恶心的 要进行两次的dijkstra,一次以起点为源点,得到的数组d1表示结点和起点最近距离 另一次以终点为源点,得到数组d2,表示结点和终点最近的距离 现在M张商业票,给出的地点为x,y,z 那么有两种选择方式,一种是从起点走到x,然后使用商业票,然后再从y走到终点,那么距离就为 d1[x]