Trucking

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.

InputThe 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.OutputFor 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 题意:每条路都有最大限重和长度,有一些货物,求卡车在保证能到达且不超载的前提下最多能拉多少货物,和通过的最短路径。
思路:货物最多的基础上路径最短。枚举货物的重量,求在限重内通过的最短路。普通枚举O(n)*SPFA O(kE)可能会超时,这里用到二分枚举O(logn)来优化时间,然后SPFA求最短路。
#include<stdio.h>
#include<string.h>
#include<deque>
#include<vector>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std; struct Node{
int v,h,w;
}node;
vector<Node> edge[MAX];
int dis[MAX],b[MAX];
int n,mid;
void spfa(int k)
{
int i;
deque<int> q;
for(i=;i<=n;i++){
dis[i]=INF;
}
memset(b,,sizeof(b));
b[k]=;
dis[k]=;
q.push_back(k);
while(q.size()){
int u=q.front();
for(i=;i<edge[u].size();i++){
int v=edge[u][i].v;
int h=edge[u][i].h;
int w=edge[u][i].w;
if(dis[v]>dis[u]+w&&(h>=mid||h==-)){
dis[v]=dis[u]+w;
if(b[v]==){
b[v]=;
if(dis[v]>dis[u]) q.push_back(v);
else q.push_front(v);
}
}
}
b[u]=;
q.pop_front();
}
}
int main()
{
int m,u,v,h,w,bg,ed,hi,l,r,f,i,j;
f=;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
f++;
for(i=;i<=n;i++){
edge[i].clear();
}
for(i=;i<=m;i++){
scanf("%d%d%d%d",&u,&v,&h,&w);
node.v=v;
node.h=h;
node.w=w;
edge[u].push_back(node);
node.v=u;
edge[v].push_back(node);
}
scanf("%d%d%d",&bg,&ed,&hi);
l=;r=hi;mid=;
int ans1=,ans2=-;
while(l<=r){
mid=(l+r)/; //二分
spfa(bg);
if(dis[ed]==INF) r=mid-;
else{
ans1=mid;
ans2=dis[ed];
l=mid+;
}
}
if(f!=) printf("\n");
if(ans2==-) printf("Case %d:\ncannot reach destination\n",f);
else printf("Case %d:\nmaximum height = %d\nlength of shortest route = %d\n",f,ans1,ans2);
}
return ;
}

HDU - 2962 Trucking SPFA+二分的更多相关文章

  1. hdu 2962 Trucking (二分+最短路Spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...

  2. hdu 2962 Trucking (最短路径)

    Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2962 Trucking

    题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...

  4. hdu 2962 最短路+二分

    题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...

  5. hdu 2962 题解

    题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot  reach  destination $ 跟前面 $ ...

  6. UVALive 4223 / HDU 2962 spfa + 二分

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

  7. hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)

    Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65 ...

  8. Day4 - I - Trucking HDU - 2962

    A certain local trucking company would like to transport some goods on a cargo truck from one place ...

  9. Trucking(HDU 2962 最短路+二分搜索)

    Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. git config --system --unset credential.helper 重新输入账号密码

    检查本地配置$ git config --local -lcore.repositoryformatversion=0core.filemode=falsecore.bare=falsecore.lo ...

  2. 我的Java开发学习之旅------>求N内所有的素数

    一.素数的概念 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数 ...

  3. [2018-10-17]宁波dotnet社区(NBDNC)第一次问卷关于dotnet技术栈的小调查

    最近(2018年10月7日至10月17日),为配合确定下一次社区线下活动主题,做了一次宁波dotnet社区(NBDNC)的本地dotnet技术栈调研,设计了一份问卷,在此做一次记录. 导出的问卷统计结 ...

  4. Quartz Job scheduling 基础实现代码

    Quartz 集成在 SpringBoot 中分为 config.task.utils.controller 和 MVC 的三层即 controller.service.dao 和 entity. c ...

  5. win 10 安装.msi 程序出现the error code is 2503

    解决方法: C:\Windows\temp文件夹的权限不够,需要给其更高权限 右键temp文件夹 点击属性进入属性对话框 组或用户名的里面的All APPLICATION PACKAGES和所有受限制 ...

  6. win32com操作word(3):导入VBA常量

    导入VBA常量方法:http://blog.sina.com.cn/s/blog_a73687bc0101k8x8.html 我们之前说过,win32com组件为python提供处理COM组件(.dl ...

  7. RabbitMQ消息队列随笔

    本文权当各位看官对RabbitMQ的基本概念以及使用场景有了一定的了解,如果你还对它所知甚少或者只是停留在仅仅是听说过,建议你先看看这篇文章,在对RabbitMQ有了基本认识后,我们正式开启我们的Ra ...

  8. Python: scikit-image Blob detection

    这个用例主要介绍利用三种算法对含有blob的图像进行检测,blob 或者叫斑点,就是在一幅图像上,暗背景上的亮区域,或者亮背景上的暗区域,都可以称为blob.主要利用blob与背景之间的对比度来进行检 ...

  9. Can't load AMD 64-bit .dll on a IA 32-bit platform错误

    将tomcat的bin目录下的tcnative-1.dll文件删除.就可以了.

  10. BZOJ_2259_ [Oibh]新型计算机 _最短路

    Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计算机的输入问题.新型计算机的输入也很独特,假 ...