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. python中将datetime对象转化为时间戳

    从mongodb中读取出来的记录中,时间存储在datetime对象里,返回给客户端的却要求是时间戳格式,因此需要将对应的datetime时间转化为时间戳,从stackoverflow上找到同样的问题和 ...

  2. day 10 形态学处理 膨胀

    #-*- coding:utf-8 -*- #1.导入包 import cv2 import numpy as np #2.导入图片 img = cv2.imread('home.jpg',0) #3 ...

  3. selenium自动化测试资源整理

    1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是原版的就不得而知了. http://www.slimjet.com/ch ...

  4. 三个线程ABC,交替打印ABC

    转载与:https://www.cnblogs.com/x_wukong/p/4009709.html 创建3个线程,让其交替打印ABC . 输出如下:  ABCABCABCABC. 方法:使用syn ...

  5. leetcode-最长无重复字符的子串

    参考他的人代码:https://blog.csdn.net/littlebai07/article/details/79100081 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: ...

  6. 学习笔记,99乘法表,嵌套while循环

    line = 0 #定义外循环初变量 while line < 9: #外循环判断语句 line += 1 #改变外循环初变量,避免陷入死循环 row = 0 #定义内循环初变量 while r ...

  7. 初涉 JavaScript

    网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...

  8. PAT-甲级解题目录

    PAT甲级题目:点这里 pat解题列表 题号 标题 题目类型  10001 1001 A+B Format (20 分)  字符串处理  1003 1003 Emergency (25 分) 最短路径 ...

  9. 小猫爬山:dfs

    题目描述: 翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). 翰翰和达达只好花钱让它们坐索道下山. ...

  10. SGU 176 Flow construction(有源汇上下界最小流)

    Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...