UVA 1048 - Low Cost Air Travel(最短路)

UVA 1048 - Low Cost Air Travel

题目链接

题意:给定一些联票,在给定一些行程,要求这些行程的最小代价

思路:最短路,一张联票对应几个城市就拆成多少条边,结点表示的是当前完成形成i,在城市j的状态,这样去进行最短路,注意这题有坑点,就是城市编号可能很大,所以进行各种hash

代码:

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

const int MAXNODE = 50005;
const int MAXEDGE = 1000005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

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

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

struct Dijkstra {
	int n, m;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool done[MAXNODE];
	Type d[MAXNODE];
	int p[MAXNODE];

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}

	void add_Edge(int u, int v, Type dist, int id) {
		edges[m] = Edge(u, v, dist, id);
		next[m] = first[u];
		first[u] = m++;
	}

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

	void dijkstra(int s) {
		priority_queue<HeapNode> Q;
		for (int i = 0; i < n; i++) d[i] = INF;
		d[s] = 0;
		p[s] = -1;
		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 = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (d[e.v] > d[u] + e.dist) {
					d[e.v] = d[u] + e.dist;
					p[e.v] = i;
					Q.push(HeapNode(d[e.v], e.v));
				}
			}
		}
	}
} gao;

const int N = 25;

int n;
struct Ticket {
	int val, hn, h[N];
} ti[N], tour;

int id[15][5005];
map<int, int> hash;
int cn;

int get2(int city) {
	if (!hash.count(city)) hash[city] = cn++;
	return hash[city];
}

int get(int num, int city) {
	int tmp = get2(city);
	if (id[num][tmp] == -1) id[num][tmp] = gao.n++;
	return id[num][tmp];
}
void solve() {
	cn = 0;
	hash.clear();
	gao.init(0);
	memset(id, -1, sizeof(id));
	scanf("%d", &tour.hn);
	for (int i = 1; i <= tour.hn; i++)
		scanf("%d", &tour.h[i]);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < tour.hn; j++) {
			int u = j;
			for (int k = 0; k < ti[i].hn; k++) {
				if (ti[i].h[k] == tour.h[u + 1])
					u++;
	//			printf("%d %d %d %d %d\n", j, ti[i].h[0], u, ti[i].h[k], ti[i].val);
				gao.add_Edge(get(j, ti[i].h[0]), get(u, ti[i].h[k]), ti[i].val, i + 1);
				if (u == tour.hn) break;
			}
		}
	}
	//printf("%d %d %d\n", tour.h[1], tour.hn, tour.h[tour.hn]);
	gao.dijkstra(get(0, tour.h[1]));
	printf("Cost = %d\n", gao.d[get(tour.hn, tour.h[tour.hn])]);
	printf("  Tickets used:");
	gao.print(get(tour.hn, tour.h[tour.hn]));
	printf("\n");
}

int main() {
	int cas = 0;
	while (~scanf("%d", &n) && n) {
		cas++;
		for (int i = 0; i < n; i++) {
			scanf("%d", &ti[i].val);
			scanf("%d", &ti[i].hn);
			for (int j = 0; j < ti[i].hn; j++)
				scanf("%d", &ti[i].h[j]);
		}
		int q;
		scanf("%d", &q);
		for (int i = 1; i <= q; i++) {
			printf("Case %d, Trip %d: ", cas, i);
			solve();
		}
	}
	return 0;
}
时间: 2024-10-03 22:25:12

UVA 1048 - Low Cost Air Travel(最短路)的相关文章

UVa1048 Low Cost Air Travel--最短路

很好的一道题呀 思路 状态\(d(i,j)\)表示已经经过了行程单中的\(i\)个城市,目前在城市\(j\)的最小代价,直接建边跑最短路就行了 比如机票为\(ACBD\),行程单为\(CD\),那么对于\((0,A)\),连向\((1,C)\),\((1,B)\),\((2,D)\) 有两个需要注意的地方 1.起点为\((1,行程单的起点)\) 2.城市编号很大,要离散化 以下是代码,离散化用\(map\)完成 #include <algorithm> #include <iostrea

UVA - 10917 Walk Through the Forest (最短路+DP)

题意:Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所有从A出发回家的路径都短,你的任务是计算有多少条不同的路径 从后往前找最短路, 对于每一步要更新之后走的位置值: #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace s

On Imitation monkey the assignment is usually to carry a variety of major model low cost diesel-engined wristwatches

This wristwatches are made to glimpse very much like the genuine wristwatches in order to retain each of the efficiency. On Imitation monkey the assignment is usually to carry a variety of major model low cost diesel watches outlet within just one ro

HDU 4885 TIANKENG’s travel 最短路

判断是否共线用map记录下斜率: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include<algorithm> #include<math.h> #include<map> using namespace std; #define N 1022 const int INF = 1<<30-1;

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

Impublished using a low cost cheap ghd straighteners australia

Impublished using a low cost cheap ghd straighteners australia conventional typical standard traditional in wide quantities of prints at only one reach. Advise your kids to mix the newspaper publishing when that like. Plenty of are saying it was even

Low cost diesel-engined wristwatches despite the fact that invest in whatever you idea seemed to be a legitimate dealership inside of a shopping mall

This activities, even so, are for various generations witout a doubt since they are very appropriate. However the activities usually are mailed by means of Switzerland corporations ETA in addition to Valjoux, there're improved with Chronome trieworkh

UVA 12950 : Even Obsession(最短路Dijkstra)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4829 Patricia is an excellent software developer, but, as every brilliant person, she has some strange quirks.One of those is that everything she d

Travel(最短路)

Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected b