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 ...
随机推荐
- cocos2d-js添加百度MSSP插屏(通过jsb反射机制)
1.导入jar包.... 2.修改AndroidManifest.xml文件 添加: <meta-data android:name="BaiduMobAd_APP_ID" ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- ubuntu在vim里搜索关键字
在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然后输入你要查找的关键字敲回车就可以了. 如果你要继续查找此关键字,敲字符 n 就可以继续查找了.
- IOS AFNetWorking 设置超时时间
(原创经验总结) 1.关于AF 超时的说法 系统默认的timeInterval 是60s ASI默认是10s 但是有一个说法是 AF “AFN在GET条件下设置的NSURLRequest能起作用, ...
- ActiveMQ之点对点使用
package com.toov5.producer; import javax.jms.Connection; import javax.jms.JMSException; import javax ...
- 算法(Algorithms)第4版 练习 1.3.9
主要思路: 用Dijkstra的双栈算法. 遇到数字则压入数字栈中(String). 遇到运算符则压入运算符栈中(String). 遇到右括号时,从数字栈和运算法栈中弹出相应的元素,生成相应的运算表达 ...
- python正则表达提取文本好文
摘自: http://www.cnblogs.com/rj81/p/5933838.html
- java进程分析
1. 找出 java进程pid,比如 11327 2. 使用jstack 看下 锁持有情况 /usr/java/latest/bin/jstack -l 11327 3. 输出java堆栈信息,以及 ...
- python处理txt文件的一种情况
在txt文本中,以换行符作为标记分段处理txt文件中的内容的方法: with open(path, 'r', encoding='utf-8') as f: for line in f: if lin ...
- block implicitly retains self to indicate this is 警告消除
Build Settings 输入CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF 设置为No