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.
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;
}