UVA 10246 - Asterix and Obelix(最短路)

UVA 10246 - Asterix and Obelix

题目链接

题意:给定一个图,每个点有一个代价,边有一个代价,现在有q次询问,每次询问从u到v的最小花费,花费的计算方式为,路径代价加上路径上最大代价结点的代价

思路:枚举最大代价结点,然后做dijkstra,做的过程中忽略掉比枚举点更大代价的点,然后更新所有的答案,预处理完成后每次询问就可以在O(1)时间内完成了

代码:

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

const int MAXNODE = 85;
const int MAXEDGE = 10005;

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 u;
	HeapNode() {}
	HeapNode(Type d, int u) {
		this->d = d;
		this->u = u;
	}
	bool operator < (const HeapNode& c) const {
		return d > c.d;
	}
};

int n, m, q, cost[MAXNODE], ans[MAXNODE][MAXNODE];

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) {
		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++) 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 (cost[e.v] > cost[s]) continue;
				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));
				}
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				ans[i][j] = min(ans[i][j], d[i] + d[j] + cost[s]);
			}
		}
	}
} gao;

int main() {
	int cas = 0;
	int bo = 0;
	while (~scanf("%d%d%d", &n, &m, &q) && n) {
		if (bo) printf("\n");
		else bo = 1;
		gao.init(n);
		for (int i = 0; i < n; i++) scanf("%d", &cost[i]);
		int u, v, d;
		while (m--) {
			scanf("%d%d%d", &u, &v, &d);
			u--; v--;
			gao.add_Edge(u, v, d);
			gao.add_Edge(v, u, d);
		}
		memset(ans, INF, sizeof(ans));
		for (int i = 0; i < n; i++) gao.dijkstra(i);
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (ans[i][j] == INF) ans[i][j] = -1;
		printf("Case #%d\n", ++cas);
		while (q--) {
			scanf("%d%d", &u, &v);
			u--; v--;
			printf("%d\n", ans[u][v]);
		}
	}
	return 0;
}
时间: 2024-10-18 15:13:29

UVA 10246 - Asterix and Obelix(最短路)的相关文章

UVa 10246 Asterix and Obelix(变形的最短路径)

10246 - Asterix and Obelix Time limit: 3.000 seconds Problem A Asterix and Obelix Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB After winning a gruesome battle against the Romans in a far-away land, Asterix a

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

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