HDU - 2962 Trucking SPFA+二分
Trucking
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+二分的更多相关文章
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
- hdu 2962 Trucking (最短路径)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2962 Trucking
题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...
- hdu 2962 最短路+二分
题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...
- hdu 2962 题解
题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot reach destination $ 跟前面 $ ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)
Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65 ...
- Day4 - I - Trucking HDU - 2962
A certain local trucking company would like to transport some goods on a cargo truck from one place ...
- Trucking(HDU 2962 最短路+二分搜索)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 远程服务器上的weblogic项目管理(三)常用指令及常见错误
weblogic的管理流程已在前两节整理完毕,接下来汇总一下linux环境下的weblogic管理常用指令及常见错误: 常用指令: ./startWebLogic.sh 启动weblogic ./st ...
- Grunt学习笔记【7】---- grunt-contrib-less插件详解
本文主要讲如何使用Grunt实现less文件压缩. 一 说明 less是非常常用的样式框架之一,Grunt也提供了可以编译和打包less样式文件的插件:grunt-contrib-less. 实现原理 ...
- (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题
1.安装 npm i react-native-splash-screen --save or yarn add react-native-splash-screen --save 2.自动配置 re ...
- log4j 2 入门实例(1)
本文介绍log4j的基本概念和将日志输出到控制台的例子. 参考文章: http://www.jianshu.com/p/464058bdbc76 http://www.hankcs.com/progr ...
- logback 配置详解(上)
logback 配置详解(一)<configuration> and <logger> 一:根节点<configuration>包含的属性: scan: 当此属性设 ...
- smokeping高级配置
摘自: http://mayulin.blog.51cto.com/1628315/514367 自定义报警 http://www.cnblogs.com/thatsit/p/6395506.html
- flash滑杆控制图片横向滚动
flash滑杆控制图片横向滚动是一款FLASH动画图片左右滚动素材,滑杆控制滚动,效果很酷,带FLASH源文件. 下载:http://www.huiyi8.com/sc/9452.html
- html5--2.3新的布局元素(2)-article
html5--2.3新的布局元素(2)-article 学习要点 了解article元素的语义和用法 完成一个简单的实例 article元素(标签) 用于定义一个独立的内容区块,比如一篇文章,一篇博客 ...
- Smack编程库进行代码功能调试
http://www.tuicool.com/articles/U3Afiy 使用Smack编程库进行代码功能调试 关于Smack编程库,前面我们提到,它是面向Java端的api,主要在PC上使用,利 ...
- Java_HTTP_01_HttpClient
一. 二.参考文档 1. HttpClient官方文档 HttpClient官方文档中文翻译 1.HttpClient 4 实现文件下载 2.httpclient 上传文件.下载文件 3.httpcl ...