Trucking

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

 
题意:
   给出一无向图 每条路对卡车的高度都有限制 求从起点到终点 卡车最高的高度及行进的最短路 
 
题解:
    我们二分高度,  
  在这个高度下进行一次最短路,解决是否能到达 终点,能的话记录 路径长度
  更新答案
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<queue>
using namespace std ;
typedef long long ll; const int N = + ;
const int inf = 1e9 + ; int dis[N],head[N],vis[N],t,n,m,T;
struct ss{
int to,h,v,next;
}e[N];
void add(int u,int v,int h,int w) {
e[t].to = v;
e[t].next = head[u];
e[t].v = w;
e[t].h = h;
head[u] = t++;
}
int spfa(int x,int limt) {
queue<int >q;
for(int i = ; i <= n; i++) dis[i] = inf, vis[i] = ;
dis[x] = ;
q.push(x);
vis[x] = ;
while(!q.empty()) {
int k = q.front();
q.pop();vis[k] = ;
for(int i = head[k]; i; i = e[i].next) {
if(e[i].h < limt) continue;
if(dis[e[i].to] > dis[k] + e[i].v) {
dis[e[i].to] = dis[k] + e[i].v;
if(!vis[e[i].to]) {
vis[e[i].to] = ;
q.push(e[i].to);
}
}
}
}
return dis[T];
}
int main() {
int a,b,h,v,S,cas = ;
while(~scanf("%d%d",&n,&m)) {
if(!n || !m) break;
if (cas > ) printf ("\n");
t = ; memset(head,,sizeof(head));
for(int i = ; i <= m; i++) {
scanf("%d%d%d%d",&a,&b,&h,&v);
if(h == -) h = inf;
add(a,b,h,v);
add(b,a,h,v);
}
scanf("%d%d%d",&S,&T,&h);
int l = , r = h, ans = inf;
while(l < r) {
int mid = (l + r + ) >> ;
if(spfa(S,mid) != inf) l = mid, ans = dis[T];
else r = mid - ;
}
printf ("Case %d:\n", cas++);
if(ans != inf) printf ("maximum height = %d\nlength of shortest route = %d\n", l, ans);
else {
printf("cannot reach destination\n");
}
}
return ;
}

UVALive 4223 / HDU 2962 spfa + 二分的更多相关文章

  1. hdu 2962 Trucking (二分+最短路Spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...

  2. UVALive - 4223(hdu 2926)

    ---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS ...

  3. hdu 2962 题解

    题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot  reach  destination $ 跟前面 $ ...

  4. UVALive 4223 Trucking 二分+spfa

    Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  5. HDU - 2962 Trucking SPFA+二分

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

  6. 二分+最短路 UVALive - 4223

    题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...

  7. hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)

    Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65 ...

  8. hdu 2962 最短路+二分

    题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...

  9. 【HDOJ1529】【差分约束+SPFA+二分】

    http://acm.hdu.edu.cn/showproblem.php?pid=1529 Cashier Employment Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. vue 父子组件通信props/emit

    props 1.父组件传递数据给子组件 父组件: <parent> <child :childMsg="msg"></child>//这里必须要 ...

  2. 分享一个完美的新闻客户端(酷商城)Android源码

    分享一个完美的新闻客户端(酷商城)Android源码,这个源码项目是从安卓教程网转载过来的,项目主要是解析html,fragment,异步缓存图片加载,webview加载网页等.可以正常的运行的,我已 ...

  3. Python更换pip源,更换conda源

    更换pip源: 1.在windows文件管理器中,输入 %APPDATA% 2.在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件 3.在新建的pip.ini文件中输入以下内 ...

  4. Qwiklab'实验-API Gateway, AWS Lambda'

    title: AWS之Qwiklab subtitle: 2. Qwiklab'实验-API Gateway, AWS Lambda' date: 2018-09-20 17:29:20 --- In ...

  5. Pyhton学习——Day9(阶段性练习)

    # 1.文件内容如下,标题为:姓名,性别,年纪,薪资## egon male 18 3000# alex male 38 30000# wupeiqi female 28 20000# yuanhao ...

  6. 【WPF】这可能是全网最全的拖拽实现方法的总结

    原文地址 https://www.cnblogs.com/younShieh/p/10811456.html 前文 本文只对笔者学习掌握的一般的拖动问题的实现方法进行整理和讨论,包括窗口.控件等内容的 ...

  7. Multipartfile与File类型相互转换

    特殊情况下需要做转换 1.M转F File file = new File(path); FileUtils.copyInputStreamToFile(multipartFile.getInputS ...

  8. (2016北京集训十三)【xsy1531】魔法游戏 - Nim游戏

    题解: 好题!我的结论很接近正解了... 把一个数化成二进制,每次至少要拿走一位,最多全拿走,不能不拿.那么这就是一个经典的Nim问题了,子树异或起来就是根节点的答案,随便递推一下就行了. 代码: # ...

  9. HDU 2048 神、上帝以及老天爷( 错排 )

    链接:传送门 思路:错排模板,典型错排问题,n个人所有人都不会抽到自己的方案数为 Dn = (n-1) * (Dn-1 + Dn-2) /******************************* ...

  10. [BZOJ3438][洛谷P1361]小M的作物

    题目大意:有A.B两个集合和n个物品,每个物品只能放在一个集合里.每个物品放在不同集合内能获得不同价值.有一些物品,如果它们同时放在一个集合内,则会产生新的价值(A和B中都有且不一定相同(c1和c2) ...