UVA 11280 - Flying to Fredericton(最短路)

UVA 11280 - Flying to Fredericton

题目链接

题意:给定一些国家,和两个国家间的花费,现在有一些询问,询问每次最多转k次飞机,最小花费

思路:dijkstra变形,多开一维表示转机次数即可

代码:

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

const int MAXNODE = 505;
const int MAXEDGE = 5005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

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

struct HeapNode {
	Type d;
	int ti;
	int u;
	HeapNode() {}
	HeapNode(Type d, int u, int ti) {
		this->d = d;
		this->ti = ti;
		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][MAXNODE];
	Type d[MAXNODE][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) {
		edges[m] = Edge(u, v, dist);
		next[m] = first[u];
		first[u] = m++;
	}

	void dijkstra(int s) {
		priority_queue<HeapNode> Q;
		for (int i = 0; i < n; i++)
			for (int j = 0; j <= 100; j++)
				d[i][j] = INF;
		d[s][0] = 0;
		memset(done, false, sizeof(done));
		Q.push(HeapNode(0, s, 0));
		while (!Q.empty()) {
			HeapNode x = Q.top(); Q.pop();
			int u = x.u;
			int ti = x.ti;
			if (done[u][ti]) continue;
			done[u][ti] = true;
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (d[e.v][ti + 1] > d[u][ti] + e.dist) {
					d[e.v][ti + 1] = d[u][ti] + e.dist;
					Q.push(HeapNode(d[e.v][ti + 1], e.v, ti + 1));
				}
			}
		}
	}
} gao;

int t, n;
map<string, int> hash;
string name;

int main() {
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		gao.init(n);
		hash.clear();
		for (int i = 0; i < n; i++) {
			cin >> name;
			hash[name] = i;
		}
		int m;
		scanf("%d", &m);
		string u, v;
		int d;
		while (m--) {
			cin >> u >> v >> d;
			gao.add_Edge(hash[u], hash[v], d);
		}
		gao.dijkstra(0);
		printf("Scenario #%d\n", ++cas);
		int q, num;
		scanf("%d", &q);
		while (q--) {
			scanf("%d", &num);
			num++;
			if (num > n - 1) num = n - 1;
			int ans = INF;
			for (int i = 0; i <= num; i++)
				ans = min(ans, gao.d[n - 1][i]);
			if (ans == INF) printf("No satisfactory flights\n");
			else printf("Total cost of flight(s) is $%d\n", ans);
		}
		if (t) printf("\n");
	}
	return 0;
}
时间: 2024-09-30 04:34:28

UVA 11280 - Flying to Fredericton(最短路)的相关文章

UVA - 11280 Flying to Fredericton(SPFA)

题目大意:给出N个点,M条线,Q个询问,问从起点到终点,最多经过k个点的最短线路 解题思路:spfa直接跑,然后纪录最短路的数组加一个维度,纪录经过几个点就可以了,还是挺水的 #include <cstdio> #include <cstring> #include <map> #include <iostream> #include <queue> using namespace std; #define N 110 #define M 101

uva 11280 状态压缩+最短路

题意:坐飞机从 a 地到 b 地 ,在最多停留s次时 , 最小花费是多少? 在题目给出的地点 , 是按从远到近给出的 , 并且给出的航班中 , 不会有从远地点到近地点的航班. 因此从这可以看出 , 题目给的图是一个DAG图 , 那么我们就能用toposort来找最短路. 注意: 会有重边 解法: 构造一个数组 d[i][j]  , 表示从开始点 s  到点 i , 在停留 j 次时的最小花费. 然后我们再求出这个图的toposort , 再求这个每一个点和其相邻点的距离. 代码: #includ

UVA - 1416 Warfare And Logistics (最短路)

Description The army of United Nations launched a new wave of air strikes on terroristforces. The objective of the mission is to reduce enemy's logistical mobility. Each airstrike will destroy a path and therefore increase the shipping cost of the sh

UVA 116 Unidirectional TSP(DP最短路字典序)

Description  Unidirectional TSP  Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Travel

UVA - 10537 The Toll! Revisited (最短路变形逆推)

Description Problem G Toll! Revisited Input: Standard Input Output: Standard Output Time Limit: 1 Second Sindbad the Sailor sold 66 silver spoons to the Sultan of Samarkand. The selling was quite easy; but delivering was complicated. The items were t

UVA 1078 - Steam Roller(最短路)

UVA 1078 - Steam Roller 题目链接 题意:给定一个地图,要求起点走到终点需要的时间,如果进入一个转弯位置,则进入和出去的时候时间要加倍 思路:最短路,关键在于如何建模,每个结点d[x][y][d][flag]表示在x, y结点,方向为d,是否加倍过了,这样就可以把每个结点之间对应的关系建边,做最短路即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <q

UVA 10801 Lift Hopping (最短路)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1742 Problem ? Lift Hopping Time Limit: 1 second Ted the bellhop: "I'm coming up and if there isn't a dead body by the time I get there, I'll m

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 1600 Patrol Robot (BFS 最短路/DFS剪枝)

这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #include <bits/stdc++.h> using namespace std; struct Node{ int r; int c; int g; int cnt; Node(int r,int c,int g,int cnt):r(r),c(c),g(g),cnt(cnt){} }; int v