POJ1797 Heavy Transportation —— 最短路变形
2024-08-30 12:33:09
题目链接:http://poj.org/problem?id=1797
Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 39999 | Accepted: 10515 |
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.
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.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4
Source
TUD Programming Contest 2004, Darmstadt, Germany
题解:
1.最短路径的变形:把dis[]从原来的记录最短距离 变为 记录不同路径上最小边权中的最大值。
2.利用dijkstra算法时,每次松弛都是选取dis的最大值。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n, m; struct edge
{
int to, w, next;
}edge[MAXN*MAXN];
int cnt, head[MAXN]; void addedge(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} int dis[MAXN];
bool vis[MAXN];
void dijkstra(int st)
{
memset(vis, , sizeof(vis));
for(int i = ; i<=n; i++)
dis[i] = (i==st?INF:); for(int i = ; i<=n; i++)
{
int k, maxx = ;
for(int j = ; j<=n; j++)
if(!vis[j] && dis[j]>maxx)
maxx = dis[k=j]; vis[k] = ;
for(int j = head[k]; j!=-; j = edge[j].next)
if(!vis[edge[j].to])
dis[edge[j].to] = max(dis[edge[j].to], min(dis[k], edge[j].w) );
}
} int x[MAXN], y[MAXN];
int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &m);
init();
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addedge(u,v,w);
addedge(v,u,w);
} dijkstra();
printf("Scenario #%d:\n",kase);
printf("%d\n\n", dis[n]);
}
}
最新文章
- 玩转Vim 编辑器
- Entity Framework 6 Recipes 2nd Edition(10-2)译 ->; 返回输出参数
- golang---文件读写
- typeof,GetType
- C++中的文件读取结束
- 改进网站设计的免费jQuery插件Top 7
- zabbix脚本报警
- 程序中条用其他程序中已经存在的PERFORM
- HDU 1394
- 【转】iOS自动布局进阶用法
- php 获取图片、swf的尺寸大小
- IDEA启动自动进入最后一个项目
- sql server 2012 数据库还原方法
- dedecms 织梦显示时间格式
- laravel 500错误的一个解决办法
- ajax 上传文件,监听进度(progress)
- c++中被忽视的隐藏
- Spark性能优化(基于Spark 1.x)
- LeetCode算法题详解之两个数组的交集
- SQL经常使用的一些词