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 ...
随机推荐
- mvn deploy
mvn:deploy在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享. 以将ojdbc14传到nexus中的thirdparty为例 一 配置 ...
- 限制线程数 Limit the number of threads started by colly 随机延迟
random delay | Colly http://go-colly.org/docs/examples/random_delay/
- Bootstrap aggregating Bagging 合奏 Ensemble Neural Network
zh.wikipedia.org/wiki/Bagging算法 Bagging算法 (英语:Bootstrap aggregating,引导聚集算法),又称装袋算法,是机器学习领域的一种团体学习算法. ...
- mysql系列之4.mysql字符集
针对mysql语句的临时办法: 先查看下mysql的各种编码设置情况, 如果结果显示有几项的编码不一致, 请先调整linux的系统编码 mysql> show variables like 'c ...
- Python爬虫-- Scrapy框架
Scrapy框架 Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码.对于会阻塞线程的操作包含访问文件.数据库或者Web.产生新的进程并需要 ...
- u盘安装debian 7(Wheezy) stabe
将一个闲置u盘插入电脑usb口.从http://mirrors.sohu.com/debian-cd/7.4.0/amd64/iso-dvd/debian-7.4.0-amd64-DVD-1.iso ...
- Android4.4 GPS框架分析【转】
本文转载自:http://blog.csdn.net/junzhang1122/article/details/46674569 GPS HAL层代码在目录trunk/Android/hardware ...
- 素数筛总结篇___Eratosthenes筛法和欧拉筛法(*【模板】使用 )
求素数 题目描述 求小于n的所有素数的数量. 输入 多组输入,输入整数n(n<1000000),以0结束. 输出 输出n以内所有素数的个数. 示例输入 10 0 示例输出 4 提示 以这道题目为 ...
- HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...
- __declspec(dllimport)的小秘密(转)
昨天和同事使用一个dll(lib+dll)的时候,发现他在引用头文件是,并没有使用__declspec(dllimport),但是程序完全运行正常,不明觉厉下,去网上翻了下资料,原来是链接器的原因,这 ...