Trucking

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

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
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2722 2433 2923 2482 2377 
 

一口血喷在了屏幕上...

题目不难而且我的初始思路是对的,开始觉得数据不大没想用二分求高度而已,而且没用二分时间差不大,一倍多。

最后检查了几次,wa了好几次,才发现是没有跳出 0 0!!还以为二分又写错了= =

用C写的邻接表比vector快点。

最短路+枚举高度:

 //140MS    600K    1953 B    C++
#include<iostream>
#include<queue>
#define N 1005
#define inf 0x3fffffff
using namespace std;
struct node{
int v,h,d;
int next;
}edge[*N];
vector<node>V[N];
int d[N];
int vis[N];
int n,s,e;
int head[N],edgenum;
void addedge(int u,int v,int h,int d)
{
edge[edgenum].v=v;
edge[edgenum].h=h;
edge[edgenum].d=d;
edge[edgenum].next=head[u];
head[u]=edgenum++;
}
void spfa(int s,int maxh)
{
for(int i=;i<=n;i++)
d[i]=inf;
memset(vis,,sizeof(vis));
queue<int>Q;
d[s]=;
Q.push(s);
vis[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
int h=edge[i].h;
int w=edge[i].d;
if(d[v]>d[u]+w && h>=maxh){
d[v]=d[u]+w;
if(!vis[v]){
Q.push(v);
vis[v]=;
}
}
}
}
}
int main(void)
{
int m,a,b,h,c,k=;
while(scanf("%d%d",&n,&m)!=EOF && (n+m))
{
if(k!=) printf("\n");
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
V[i].clear();
edgenum=;
for(int i=;i<m;i++){
scanf("%d%d%d%d",&a,&b,&h,&c);
if(h==-) h=inf;
addedge(a,b,h,c);
addedge(b,a,h,c);
}
scanf("%d%d%d",&s,&e,&h);
printf("Case %d:\n",k++);
int ans=inf;
int l=,r=h;
while(l<r){
int mid=(l+r+)>>;
spfa(s,mid);
if(d[e]!=inf){
l=mid;ans=d[e];
}else r=mid-;
}
if(ans!=inf){
printf("maximum height = %d\n",r);
printf("length of shortest route = %d\n",ans);
}else printf("cannot reach destination\n");
}
return ;
}

hdu 2962 Trucking (最短路径)的更多相关文章

  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 SPFA+二分

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

  3. HDU 2962 Trucking

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

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

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

  5. Day4 - I - Trucking HDU - 2962

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

  6. HDU ACM 3790 最短路径问题

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. hdu 3790 (最短路径问题dijkstra)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  8. HDU 2112 HDU Today(最短路径+map)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. hdu 1688 Sightseeing (最短路径)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. web之前端获取上传图片并展示

    1.html中经常存在图片上传的问题,但是后续的展示基本上是通过后台输出流的方式来呈现的.但是这样耗费的资源比较多.所以这里学习了一种前端直接展示图片的方式(供参考). 2.html的编写方式比较简单 ...

  2. 利尔达CC3200模块第一篇之-wlan_ap例程测试

    1. 本次采用利尔达的CC3200模块,CC3200主时钟80M,内部没有flash,必须外接SPI Flash.本次测试采用利尔达科技的CC3200的底板和模块(左边).烧写连接VCC, GND, ...

  3. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  4. 使用materialization

    explain select `countries`.`id` AS `id`,`countries`.`sortname` AS `sortname`,`countries`.`name` AS ` ...

  5. 欧陆词典PEST2词库

    欧陆词典PEST2单词列表,其中大概1900+单词,可能有少数几个没有录入,但不影响使用!

  6. Objective-C 类和对象

    面向对象 面向对象(Object-Oriented)是基于面向过程(procedure-oriented)而言的 面向对象 强调对象<指挥者> OC, Java语言就是面向对象 面向过程 ...

  7. Python安装教程最新版

    Python安装教程最新版 目前Python官网已经更新到了最新版Python 3.7.1, 相比Python 2系列,它的兼容性不是太好, 不过应该会在不久的将来会全面解决.它的安装比较容易,具体步 ...

  8. 孤荷凌寒自学python第七十八天开始写Python的第一个爬虫8

    孤荷凌寒自学python第七十八天开始写Python的第一个爬虫8 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 到今天止基本完成了对docx模块针 ...

  9. adb 在windows7中的使用

    我的系统环境是win7 x64 首先放上资源链接:https://pan.baidu.com/s/1eTV5qX8 密码:2ejw 第一步: 配置环境变量,将adb.exe的路径添加到PATH里面去: ...

  10. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...