Trucking

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11419    Accepted Submission(s): 1101

Problem Description
A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the roads in the shortest route: some roads may have obstacles (e.g. bridge overpass, tunnels) which limit heights of the goods transported. Therefore, the company would like to transport as much as possible each trip, and then choose the shortest route that can be used to transport that amount.

For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded.

 
Input
The input consists of a number of cases. Each case starts with two integers, separated by a space, on a line. These two integers are the number of cities (C) and the number of roads (R). There are at most 1000 cities, numbered from 1. This is followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allowed on that road, and the length of that road. The maximum height for each road is a positive integer, except that a height of -1 indicates that there is no height limit on that road. The length of each road is a positive integer at most 1000. Every road can be travelled in both directions, and there is at most one road connecting each distinct pair of cities. Finally, the last line of each case consists of the start and end city numbers, as well as the height limit (a positive integer) of the cargo truck. The input terminates when C = R = 0.
 
Output
For each case, print the case number followed by the maximum height of the cargo truck allowed and the length of the shortest route. Use the format as shown in the sample output. If it is not possible to reach the end city from the start city, print "cannot reach destination" after the case number. Print a blank line between the output of the cases.
 
Sample Input
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 10
5 6
1 2 7 5
1 3 4 2
2 4 -1 10
2 5 2 4
3 4 10 1
4 5 8 5
1 5 4
3 1
1 2 -1 100
1 3 10
0 0
 
Sample Output
Case 1:
maximum height = 7
length of shortest route = 20

Case 2:
maximum height = 4
length of shortest route = 8

Case 3:
cannot reach destination

 
Source

题意:有c个城市,r条连接两个不同城市的道路,每条道路都有长度和限制货物的最大高度,货物的高度越高

则能运输的货物越多,所以商人老板想从城市a到城市能运输货物的高度最大可以是多少,当高度最大时,要走的路程总长最短是多少。

输入给出 c 和 r 的大小,接下来有 r 行代表着每条道路的信息,每一行给出相连的城市a,b,道路限制的最大高度(-1代表高度不受限制),道路的长度,道路是双向的。

最后一行给出出发城市,目的城市,货物能到达的初始最大高度。

思路:简单的dijkstra的运用,先用此算法求出能运输的最大高度,再以求出的最大高度为限制求出要走的最短路程。但是要注意不能同时求最大高度和最段路径,

因为当你求出一个点的最大高度和最短路程时,你不能以该点的最段路程来退出它后面点的最短路程,因为加设你当前求出的最大高度为h,最短路径为l,那么如果它连接下一个点的道路的限制小于你当前的最大高度时,你就不能保证下一个点的最短路径是最短的

看下图就知道了

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0x3fffffff
using namespace std;
struct st{
int h,l;
}mp[1010][1010];
st dis[1010];
int vt[1010];
int n,m,nb;
int a2,b2,h2;
int dij(){
int i,j,k;
int mx,mn;
int h;
int c,d,a1;
for(i=1;i<=n;i++){//初始化
dis[i].l=INF;
dis[i].h=-1;
}
dis[a2].l=0;//初始化开始点
dis[a2].h=h2;
k=a2;
while(k!=-1){//求高度
vt[k]=1;
mx=0;//高度
for(i=1;i<=n;i++){
if(!vt[i]&&mp[k][i].l!=-1){
if(dis[i].h<(min(mp[k][i].h,dis[k].h))){//更新每个点的最大高度
dis[i].h=min(mp[k][i].h,dis[k].h); }
}
}
k=-1;
for(i=1;i<=n;i++){
if(!vt[i]){//找出当前高度最大的点 重复操作
if(dis[i].h>mx){
mx=dis[i].h;
k=i;
}
}
} }
h=dis[b2].h;//最大高度
k=a2;
//printf("ww%d\n",h);
memset(vt,0,sizeof(vt));
while(k!=-1){//求最短路径
vt[k]=1;
mn=INF;//长度
for(i=1;i<=n;i++){
if(!vt[i]&&mp[k][i].l!=-1&&mp[k][i].h>=h){//限制条件
dis[i].l=min(dis[i].l,mp[k][i].l+dis[k].l);//更新路径长度
}
}
k=-1;
for(i=1;i<=n;i++){//找出当前路径最小的点
if(!vt[i]){
if(mn>dis[i].l){
mn=dis[i].l;
k=i;
}
}
}
} if(nb>1)
printf("\n");
printf("Case %d:\n",nb);
if(dis[b2].l==INF)//如果不能走到目的城市
printf("cannot reach destination\n");
else
printf("maximum height = %d\nlength of shortest route = %d\n",dis[b2].h,dis[b2].l);
return 0;
}
int main(){
int i,j,l;
int lg=0;
nb=0;
while(scanf("%d%d",&n,&m)&&n){
memset(mp,-1,sizeof(mp));//初始化
memset(vt,0,sizeof(vt));
for(i=0;i<m;i++){
scanf("%d%d%d%d",&a2,&b2,&h2,&l);//输入这条道路相连的两个城市,道路的限制高度,道路的长度
if(h2==-1)
h2=INF;
mp[a2][b2].h=h2;
mp[a2][b2].l=l;
mp[b2][a2]=mp[a2][b2];
}
scanf("%d%d%d",&a2,&b2,&h2);//输入出发城市,目的城市,限制的最大高度
nb++;
dij();
}
return 0;
}

  

A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the roads in the shortest route: some roads may have obstacles (e.g. bridge overpass, tunnels) which limit heights of the goods transported. Therefore, the company would like to transport as much as possible each trip, and then choose the shortest route that can be used to transport that amount. For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded. Input The input consists of a number of cases. Each case starts with two integers, separated by a space, on a line. These two integers are the number of cities (C) and the number of roads (R). There are at most 1000 cities, numbered from 1. This is followed by R lines each containing the city numbers of the cities connected by that road, the maximum height allowed on that road, and the length of that road. The maximum height for each road is a positive integer, except that a height of ‘-1’ indicates that there is no height limit on that road. The length of each road is a positive integer at most 1000. Every road can be travelled in both directions, and there is at most one road connecting each distinct pair of cities. Finally, the last line of each case consists of the start and end city numbers, as well as the height limit (a positive integer) of the cargo truck. The input terminates when C = R = 0. Output For each case, print the case number followed by the maximum height of the cargo truck allowed and the length of the shortest route. Use the format as shown in the sample output. If it is not possible to reach the end city from the start city, print ‘cannot reach destination’ after the case number. Print a blank line between the output of the cases. Sample Input 5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 10 5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 4 3 1 1 2 -1 100 1 3 10 0 0 Sample Output Case 1: maximum height = 7 length of shortest route = 20 Case 2: maximum height = 4 length of shortest route = 8 Case 3: cannot reach destination

UVALive - 4223,hdu2962(简单dijkstra)的更多相关文章

  1. 二分+最短路 UVALive - 4223

    题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...

  2. UVALive 4223 Trucking 二分+spfa

    Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  3. UVALive - 4223(hdu 2926)

    ---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS ...

  4. Fight Against Traffic -简单dijkstra算法使用

    题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...

  5. UVaLive 4256 Salesmen (简单DP)

    题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻. 析:dp[i][j] 表示第 i 个数改成 j 时满足条件.然后就很容易了. 代码如下: #pragma com ...

  6. UVALive 4223 / HDU 2962 spfa + 二分

    Trucking Problem Description A certain local trucking company would like to transport some goods on ...

  7. UVALive 6486 Skyscrapers 简单动态规划

    题意: 有N个格子排成一排,在每个格子里填上1到N的数(每个只能填一次),分别代表每个格子的高度.现在给你两个数left和right,分别表示从左往右看,和从右往左看,能看到的格子数.问有多少种情况. ...

  8. 图论基础之Dijkstra算法的初探

         图论,顾名思义就是有图有论.        图:由点"Vertex"和边"Edge "组成,且图分为有向图和无向图(本文讨论有向图),之前做毕业设计的 ...

  9. 一些简单二分题,简单的hash,H(i),字符串题

    说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...

随机推荐

  1. JavaScricp(总回顾)

    知识点导图 1:基础知识 (1)JavaScript是脚本语言,弱类型,执行非常非常快 (2)它与java有什么关系?没有任何关系 (3)js能做什么事情?1控制浏览器 BOM ,2控制元素 DOM ...

  2. Oarcle 入门之注释与关键字

    --1.--单行注释 *输入法应定要为英文 --2./*多行注释 *与java相似*/   ------------------------------------------------------ ...

  3. 9个顶级开发IoT项目的开源物联网平台

    https://blog.csdn.net/shnbiot/article/details/80432017 物联网(IoT)是帮助人工智能(AI)以更好的方式控制和理解事物的未来技术. 我们收集了一 ...

  4. zabbix 配置维护

    Centos 6.5, Zabbix 3.0.4 在配置了邮件报警后,如果正常的软硬件变更(比如发版)也不停的发邮件肯定很烦,这个时候就需要在操作前挂上维护: 浏览器登录zabbix后台,Config ...

  5. Bootstrap3基础 warning/active... 表格的状态类(不同的背景色)

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  6. vue搭建前端相关命令

    Vue搭建.新建工程并打开浏览器调试的指令: 这四行命令就是我们接下来工作了. 1.npm install –global vue-cli 我们在安装好nodejs后就可以用到“npm”这个前缀指令, ...

  7. jackson中@JsonProperty、@JsonIgnore等常用注解总结

    本文为博主原创,未经允许不得转载: 最近用的比较多,把json相关的知识点都总结一下,jackjson的注解使用比较频繁, jackson的maven依赖 <dependency> < ...

  8. C# 绘图

    e.Graphics.DrawLine (绘制一条连接由坐标对指定的两个点的线条) e.Graphics.DrawString (绘制指定位置的文本字符串) e.Graphics.DrawRectan ...

  9. pictureBox绑定Base64字符串

    if (!string.IsNullOrEmpty(imageCode)) { byte[] bytes = Convert.FromBase64String(imageCode); MemorySt ...

  10. 【Nodejs】【node.js 安装 和 配置Sublime Text的Node.js】

    [一] [安装nodejs] 第一步:下载安装文件: https://nodejs.org/en/download/ 第二步:安装nodejs 下载完成之后,双击"node-v6.10.1- ...