HDU 2962 Trucking
题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路。
题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可。
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
using namespace std;
const int N=20005;
const int INF=9999999;
typedef pair<int,int>seg;
priority_queue<seg,vector<seg>,greater<seg> >q;
int l,r,mid,begin,end,d[N],head[N],u[N],v[N],w[N],next[N],le[N],n,m,a,b,c;
int height,route;
bool vis[N];
void build(){
memset(head,-1,sizeof(head));
for(int e=1;e<=m;e++){
scanf("%d%d%d%d",&u[e],&v[e],&le[e],&w[e]);
if(le[e]==-1)le[e]=INF;
u[e+m]=v[e]; v[e+m]=u[e]; w[e+m]=w[e]; le[e+m]=le[e];
next[e]=head[u[e]]; head[u[e]]=e;
next[e+m]=head[u[e+m]]; head[u[e+m]]=e+m;
}
}
void Dijkstra(int src,int limit){
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++) d[i]=INF;
d[src]=0;
q.push(make_pair(d[src],src));
while(!q.empty()){
seg now=q.top(); q.pop();
int x=now.second;
if(vis[x]) continue; vis[x]=true;
for(int e=head[x];e!=-1;e=next[e])
if(d[v[e]]>d[x]+w[e]&&le[e]>=limit){
d[v[e]]=d[x]+w[e];
q.push(make_pair(d[v[e]],v[e]));
}
}
}
int main(){
int cnt=1;
while(~scanf("%d%d",&n,&m)){
height=route=0;
if(m==0&&n==0)break;
if(cnt!=1) puts("");
printf("Case %d:\n",cnt++);
build();
scanf("%d%d%d",&begin,&end,&r);
l=1;
while(l<=r){
mid=(l+r)>>1;
Dijkstra(begin,mid);
if(d[end]!=INF){height=mid;route=d[end];l=mid+1;}
else r=mid-1;
}
if(height==0)puts("cannot reach destination");
else{
printf("maximum height = %d\n",height);
printf("length of shortest route = %d\n",route);
}
}
return 0;
}
HDU 2962 Trucking的更多相关文章
- 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 SPFA+二分
Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...
- Trucking(HDU 2962 最短路+二分搜索)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Day4 - I - Trucking HDU - 2962
A certain local trucking company would like to transport some goods on a cargo truck from one place ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- hdu 2962 最短路+二分
题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...
- hdu 2962 题解
题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot reach destination $ 跟前面 $ ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
随机推荐
- Unity StrangeIoc框架 (三)signal信号方式
先创建TestRoot using UnityEngine; using System.Collections; using strange.extensions.context.impl; publ ...
- C/C++中volatile关键字详解 (转)
1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...
- 浅谈Spring(二)
一.AOP编程(面向切面编程) AOP的本质是代理. 1.静态代理设计模式 概念:通过代理类为原始类增加额外功能. 代理类 = 原始类 + 额外功能 +实现原始类的相同接口. 优点:避免原始类因为额外 ...
- Scala io操作
1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...
- BZOJ 1406: [AHOI2007]密码箱( 数论 )
(x+1)(x-1) mod N = 0, 枚举N的>N^0.5的约数当作x+1或者x-1... ------------------------------------------------ ...
- Scala学习之for 循环和 yield 的例子
for循环中的 yield 会把当前的元素记下来,保存在集合中,循环结束后将返回该集合.Scala中for循环是有返回值的.如果被循环的是Map,返回的就是Map,被循环的是List,返回的就是Lis ...
- C学习之指针强化
char *p = (char *)malloc(100); malloc是用于分配内存的函数,它的参数为int型,表示分配多少个字节长度,其返回类型为void*,在这里用char*就是强制转化,指定 ...
- hadoop搭建杂记:Linux下hostname的更改办法
VirtualBox搭建hadoop伪分布式模式:更改hostname VirtualBox搭建hadoop伪分布式模式:更改hostname master: ip:192.168.56.120 机器 ...
- php数组使用技巧及操作总结
数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是一些小结,借此记之,便于以后鉴之. 1. 数组定义 数组的定义使用 array()方式定义,可以定义空数组:<?ph ...
- CSS 基础总结
CSS基础 Doctype 声明位于文档中的最前面,处于 标签之前.告知浏览器的解析器,用什么文档类型 规范来解析这个文档. 在标准模式中,浏览器根据规范呈现页面: 在混杂模式中,页面以一种比较宽松的 ...