UVALive 4223 / HDU 2962 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.
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
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 + 二分的更多相关文章
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
- UVALive - 4223(hdu 2926)
---恢复内容开始--- 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS ...
- hdu 2962 题解
题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot reach destination $ 跟前面 $ ...
- UVALive 4223 Trucking 二分+spfa
Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- HDU - 2962 Trucking SPFA+二分
Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...
- 二分+最短路 UVALive - 4223
题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...
- hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)
Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65 ...
- hdu 2962 最短路+二分
题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...
- 【HDOJ1529】【差分约束+SPFA+二分】
http://acm.hdu.edu.cn/showproblem.php?pid=1529 Cashier Employment Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- 【Android 系统开发】使用 Source InSight 阅读 Android 源代码
1. 安装 Source Insight (1) Source Insight 相关资源 安装相关资源 : -- 下载地址 : http://www.sourceinsight.com/down35. ...
- C# 监控Windows睡眠与恢复
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; private void SystemEvents_PowerModeC ...
- 【POJ 1741】 Tree
[题目链接] http://poj.org/problem?id=1741 [算法] 点分治 要求距离不超过k的点对个数,不妨将路径分成两类 : 1. 经过根节点 2. 不经过根节点 考虑第1类路径, ...
- linux安装lua
1,下载lua源码wget http://www.lua.org/ftp/lua-5.3.3.tar.gz或curl -R -O http://www.lua.org/ftp/lua-5.3.3.ta ...
- C#基础篇之语言和框架介绍
1.如何描述C#和.NET的关系? .Net的是平台,C#是为了微软公司为了.NET平台开发的面向对象语言. 2.C#能做什么? (1)C#.NET做窗体应用开发,Web开发中可以通过WCF编写Web ...
- 纯css实现宽度自适应,高度与宽度成比例
html: <div></div> css div{ width: 33.33%; box-sizing: border-box; float: left; position: ...
- JS自定义功能函数实现动态添加网址参数修改网址参数值
无论是前端开发还是后台设计,很多时候开发人员都需要获取当前或目标网址的相关信息.这个已有现成的内置对象属性可以直接调用了(下面是获取当前页面的参考代码) 复制代码 代码如下: <script t ...
- JVM 原理
0 引言 JVM一直是java知识里面进阶阶段的重要部分,如果希望在java领域研究的更深入,则JVM则是如论如何也避开不了的话题,本系列试图通过简洁易读的方式,讲解JVM必要的知识点. 1 运行流 ...
- 进程线程之pid,tid
Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其i ...
- vue调试工具vue-devtools的安装
一.可以在chrome商店中下载安装,当然需要FQ哈,你懂得~: 二.手动安装: 1.将github上项目文件克隆到本地,https://github.com/vuejs/vue-devtools: ...