POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
| Time Limit: 3000MS | Memory Limit: 30000K | |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() ![]() |
Description
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
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 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
其实这个题看了好久都没搞懂意思,然后找了找博客原来就是给定n个站点(就这样理解吧),m条路,两个站点之间有且只有一条路,每条路都有一个最大载重量,求整个路径组成的图中的最大的承重;比如,1到2之间的载重是3,超过3便不能从这条路上过,而1到3之间的载重是4,这时已经将图连接起来了,所以最大值是4,如果选1->2->3,则1、2之间的路就不能过重量为5的升降机;
这样,就相当于求一个最大生成树的权值最小的那条边,不过用生成树的方法不是超时就RE,只好用最短路中的一个算法解决了,我们看,既然dijkstra是求单源的最短路,d[i]存储的是从起始点到i点的最短路,那么我们就用它来存起始点到i点权值最小的那个;这样答案不就出来了;想想看,这道题逻辑性很强,求能承载的最大重量实际上是求整个联通图中权值最小的,就像短板原理--能装多少水取决于最短的那块木板;
Kruskal生成树:RE,数组开大了又会超时;
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1000+10;
struct node
{
int u,v,w;
}a[N];
int n,m,f[N];
int find(int x)
{
return f[x]==-1?x:f[x]=find(f[x]);
}
int cmp(node a,node b)
{
return a.w>b.w;
}
int ks(int n,int m)
{
for(int i=0;i<m;i++)
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
sort(a,a+m,cmp);
int minn=10000000;
memset(f,-1,sizeof(f));
for(int i=0;i<m;i++)
{
int x=find(a[i].u);
int y=find(a[i].v);
if(x!=y)
{
minn=min(minn,a[i].w);
f[x]=y;
}
if(find(1)==find(n))
break;
}
return minn;
}
int main()
{
int t;
scanf("%d",&t);
int t1=t;
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
int x=ks(n,m);
printf("Scenario #%d:\n%d\n",t1-t,x);
}
return 0;
}
Dijkstra最短路变形:AC
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
int w[N][N],v[N],d[N],n,m,t,t1;
void dijkstra()
{
int a,b,c,i,j;
memset(w,0,sizeof(w));
for(i=1; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
w[a][b]=w[b][a]=c;
}
memset(v,0,sizeof(v));
for(i=1; i<=n; i++)
d[i]=w[1][i];
for(i=1;i<=n;i++)
{
int x,m=-1;
for(j=1;j<=n;j++)
if(!v[j]&&d[j]>m)
m=d[x=j];
v[x]=1;
for(j=1;j<=n;j++)
if(!v[j] && d[j]<min(d[x],w[x][j]))
d[j]=min(d[x],w[x][j]);
}
printf("Scenario #%d:\n",t1-t);
printf("%d\n\n",d[n]);
}
int main()
{
scanf("%d",&t);
t1=t;
while(t--)
{
scanf("%d%d",&n,&m);
dijkstra();
}
return 0;
}
POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;的更多相关文章
- POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)
题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
- poj 1797Heavy Transportation(dijkstra变形)
题目链接:http://poj.org/problem?id=1797 题意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上 ...
- POJ1797 Heavy Transportation —— 最短路变形
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 2253 Frogger -- 最短路变形
这题的坑点在POJ输出double不能用%.lf而要用%.f...真是神坑. 题意:给出一个无向图,求节点1到2之间的最大边的边权的最小值. 算法:Dijkstra 题目每次选择权值最小的边进行延伸访 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ 2253 Frogger【最短路变形——路径上最小的最大权】
链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- javascript简单的表单验证
<html> <head> <title>用户登录</title> <script language="javascript" ...
- js调用本地程序
前几天,做项目时候用到js调用本地的程序,找了好多资料,一种是写入注册表,一种是写一个浏览器插件,相对来说,写一个注册表更简单一点,因为需求很紧.下面就是我的总结,希望可以对你们有所帮助,具体从哪里找 ...
- OpenGL 透视投影推导图解
有它足够了,转载自:http://blog.sina.com.cn/s/blog_73428e9a0102v920.html
- viewport实现html页面动态缩放/meta viewport/viewport
页面默认缩放比例为1,最小宽度为375px,在小于375px出现水平滚动条的时候重新计算显示比例缩小界面, <!DOCTYPE html> <html lang="en&q ...
- Es6里面的解析结构
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 洛谷 P2153 [SDOI2009]晨跑
题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...
- Hystrix 断路器
断路器: 当客户端访问服务端,发现服务端有异常不能进行访问时,就会执行一个fallback 方法.
- 事件冒泡 & 阻止事件冒泡
事件冒泡 : 当一个元素接收到事件的时候,会把他接收到的所有传播给他的父级,一直到顶层window.事件冒泡机制 阻止冒泡 : 当前要阻止冒泡的事件函数中调用 event.cancelBubble = ...
- env - 在重建的环境中运行程序
SYNOPSIS(总览) env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] DESCRIPTION(描述) 设置环境中的每个NAME为VAL ...
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...



