POJ1797 Heavy Transportation Dijkstra

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 44998 Accepted: 11768

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know. 

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

链接

一、题意

        一个运输系统中共有n个结点,m条路。对于每一条从结点1到结点n的路径,该路径的最大负荷为路径中每条路的负荷的最小值。如1-->B-->C-->n中各条路的负荷为2, 3, 4,则整条路的负荷为2。给定整个运输系统的状态,求1到n的最大负荷。

二、思路

        乍一看有点网络流的味道,但发现它并不能存在多条流。所以还是最短路的思想,用Dijkstra的变形算法,使用dis数组存储路径的最大负载,更新的条件从原来的取最短路径改为取最大负载,如由起点beg到终点v经过k,则有dis[v] = max(dis[v], min(dis[cur], mat[cur][i].c));其中min()求得先到k再到v的路径的负载,然后和原来的dis[v]比较。

三、代码

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 1100;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int INF = 2000000000;//0x7fffffff
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

struct Node {
	int v;
	int c;
	Node() {}
	Node(int vv, int cc) :v(vv), c(cc) {}
};

bool operator<(const Node &a, const Node &b) {
	return a.c < b.c;
}

vector<Node> mat[MAXN];
int dis[MAXN];
bool visited[MAXN];
priority_queue<Node> que;

void Dijkstra(int beg, int n) {
	while (!que.empty()) {
		que.pop();
	}

	memset(visited, false, sizeof(visited));
	memset(dis, 0, sizeof(dis));

	dis[beg] = INF;
	que.push(Node(beg, dis[beg]));
	while (!que.empty()) {
		int cur = que.top().v;
		que.pop();
		if (visited[cur]) {
			continue;
		}

		visited[cur] = true;
		for (int i = 0; i < mat[cur].size(); ++i) {
			if (!visited[mat[cur][i].v]) {
				int v = mat[cur][i].v;
				dis[v] = max(dis[v], min(dis[cur], mat[cur][i].c));
				que.push(Node(v, dis[v]));
			}
		}
	}
}

int main() {
	int t, n, m, u, v, c;
	scanf("%d", &t);
	for (int kase = 1; kase <= t; ++kase) {
		scanf("%d%d", &n, &m);

		for (int i = 0; i < n; ++i) {
			mat[i].clear();
		}

		for (int i = 0; i < m; ++i) {
			scanf("%d%d%d", &u, &v, &c);
			mat[u - 1].push_back(Node(v - 1, c));
			mat[v - 1].push_back(Node(u - 1, c));
		}

		Dijkstra(0, n);

		printf("Scenario #%d:\n", kase);
 		printf("%d\n\n", dis[n - 1]);
	}

	//system("pause");
	return 0;
}

POJ1797是一道经典的图论问题,主要考察的是最小生成树(MST)的应用。题目要求在一个无向图中找到一条路径,使得路径上的最大边权最小。这个问题可以通过Kruskal算法来解决。 ### 问题描述 给定一个无向图,图中有N个顶点和M条边。每条边有一个权值,要求找到一条路径,使得路径上的最大边权最小。 ### 解题思路 1. **Kruskal算法**:Kruskal算法是一种贪心算法,用于求解最小生成树。它通过不断选择最小权值的边来构建生成树,同时避免形成环。 2. **并查集(Union-Find)**:用于检测环的存在。每当加入一条边时,检查这条边连接的两个顶点是否已经在同一个连通分量中。如果在同一个连通分量中,则形成环;否则,将这两个顶点合并。 ### 代码实现 以下是使用Java实现的Kruskal算法来解决POJ1797问题的示例代码: ```java import java.util.Arrays; import java.util.Scanner; class Edge implements Comparable<Edge> { int u, v, weight; Edge(int u, int v, int weight) { this.u = u; this.v = v; this.weight = weight; } @Override public int compareTo(Edge other) { return this.weight - other.weight; } } public class Main { static int[] parent; static int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } static void union(int x, int y) { int xroot = find(x); int yroot = find(y); if (xroot != yroot) { parent[xroot] = yroot; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int t = 1; t <= T; t++) { int N = scanner.nextInt(); int M = scanner.nextInt(); Edge[] edges = new Edge[M]; for (int i = 0; i < M; i++) { int u = scanner.nextInt(); int v = scanner.nextInt(); int weight = scanner.nextInt(); edges[i] = new Edge(u, v, weight); } parent = new int[N + 1]; for (int i = 1; i <= N; i++) { parent[i] = i; } Arrays.sort(edges); int maxEdge = -1; for (Edge edge : edges) { if (find(edge.u) != find(edge.v)) { maxEdge = Math.max(maxEdge, edge.weight); union(edge.u, edge.v); } } System.out.println("Scenario #" + t + ":"); System.out.println(maxEdge); if (t < T) { System.out.println(); } } scanner.close(); } } ``` ### 代码解释 1. **Edge类**:表示图中的边,并实现了Comparable接口以便排序。 2. **find方法**:查找某个顶点的根节点。 3. **union方法**:合并两个顶点所在的集合。 4. **main方法**:读取输入数据,构建边列表,排序并应用Kruskal算法,最后输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值