POJ 1797 Heavy Transportation(dijkstra算法+优先队列优化)

题目就是找出从1到n的一条路径,使得这条路径的最小边权最大。

dijkstra算法的变形,状态为终点V和到该点的最小边权的最大值M。

#pragma warning(disable:4996)
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1005;
const int M = N*N;
const int inf = 0x6fffffff;
vector<int>g[N], e[N];
bool vis[N];
int ans[N];
int n, m;

struct node {
	int v, M;//从源点到点v能通过的重量的最大值
	node() {}
	node(int v, int M) :v(v), M(M) {}
	bool operator<(const node&op)const {
		return M < op.M;
	}
};

void add(int u, int v, int c) {
	g[u].push_back(v);
	e[u].push_back(c);
}

void dijkstra() {
	memset(vis, false, sizeof vis);
	memset(ans, 0, sizeof ans);
	priority_queue<node>q;
	q.push(node(1, inf));
	ans[1] = inf;
	while (!q.empty()) {
		node now = q.top(); q.pop();
		int u = now.v;
		if (vis[u])continue;
		vis[u] = true;
		if (u == n)break;
		for (int i = 0; i < (int)g[u].size(); i++) {
			int v = g[u][i], c = e[u][i];
			if (vis[v])continue;
			int tmp = min(ans[u], c);
			if (ans[v] < tmp) {
				ans[v] = tmp;
				q.push(node(v, ans[v]));
			}
		}

	}
}

int main() {
	int t; scanf("%d", &t);
	for (int kase = 1; kase <= t; kase++) {
		for (int i = 1; i < N; i++) {
			g[i].clear();
			e[i].clear();
		}
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= m; i++) {
			int u, v, c; scanf("%d %d %d", &u, &v, &c);
			add(u, v, c);
			add(v, u, c);
		}
		dijkstra();
		printf("Scenario #%d:\n", kase);
		printf("%d\n\n", ans[n]);
	}
	return 0;
}


你可能感兴趣的:(POJ 1797 Heavy Transportation(dijkstra算法+优先队列优化))