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
 
 
这个题花了我和我老婆一下午的时间 本来以为很简单 后来越想越复杂 改了好多遍 最后还PE了一遍T^T,唉 。。。。结果还被我老婆说了
 
题目大意是有n个点m条路 每条路都有一个限高,最后给出一个起点 一个终点和车所能装货物的最大高度,求在车装货物高度尽量高的情况下的最短路
 
先用二分列举货物的高度 再用SPFA找最短路就OK0.0,,,噢噢噢噢 最后要注意下输出格式 是在测试数据之间有空行
 
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std; struct node
{
int h,len;
} map[1010][1010]; int start,end,height,c;
int node[1010];
const int inf=9999999; int Spfa(int high)
{
for (int i=1;i<=c;i++)
node[i]=inf;
queue<int>q;
int inq[1010]= {0};
int tm=start;
node[tm]=0;
inq[tm]=1;
q.push(tm);
while (!q.empty())
{
int s=q.front();
q.pop();
for (int i=1; i<=c; i++)
{
//cout<<s<<i<<" "<<node[i]<<" "<<map[s][i].len<<endl;
if (map[s][i].h>=high&&node[i]>map[s][i].len+node[s])
{
node[i]=map[s][i].len+node[s];
//cout<<" "<<i<<" "<<node[i]<<endl;
if (!inq[i])
{
q.push(i);
inq[i]=1;
}
}
}
inq[s]=0; }
if (node[end]!=inf)
return node[end];
else
return -1;
} int main ()
{
int r,maxx,minn,h,k=1;
while (cin>>c>>r&&(c||r))
{
int ans=-1,cmp=-1;
for(int i=1;i<=c;i++)
{
for(int j=1;j<=c;j++)
{
map[i][j].len=inf;
map[i][j].h=0;
}
}
maxx=0,minn=inf;
for (int i=1; i<=r; i++)
{
int a,b,len; cin>>a>>b>>h>>len;
if(h==-1) h=inf;
if (minn>h) minn=h;
if (maxx<h) maxx=h;
//cout<<minn<<" "<<maxx<<endl;
if (map[a][b].len>len)
map[a][b].len=map[b][a].len=len;
if (map[a][b].h<h)
map[a][b].h=map[b][a].h=h;
}
cin>>start>>end>>height;
maxx=height>maxx?maxx:height;
int l=minn,r=maxx;
while (l<=r)
{
int mid=(r+l)>>1;
//cout<<l<<" "<<r<<" "<<mid<<endl;
int flag=Spfa(mid);
if (flag!=-1)
{
l=mid+1;
ans=mid;
cmp=flag;
}
else
r=mid-1;
}
if(k>1)
printf("\n");
printf("Case %d:\n",k++);
if(ans==-1)
printf("cannot reach destination\n");
else
{
printf ("maximum height = %d\n",ans);
printf ("length of shortest route = %d\n",cmp);
}
}
return 0;
}

  

hdu2962 Trucking (最短路+二分查找)的更多相关文章

  1. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  2. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  3. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  6. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  7. 【Python】二分查找算法

    二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

随机推荐

  1. bzoj 1911 [Apio2010]特别行动队(斜率优化+DP)

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3191  Solved: 1450[Submit][Statu ...

  2. Bzoj 2748: [HAOI2012]音量调节 动态规划

    2748: [HAOI2012]音量调节 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1234  Solved: 777[Submit][Status ...

  3. n皇后问题leetcode-51. N-Queens

    n皇后问题是应用回溯法的经典问题.任一行.列.对角线不能有两皇后并存,因此在判断是否合法时,可以将某一行是否有皇后.某一列是否有皇后分别用数组存起来.注意到,对于往左下右上的对角线,每个点的行号(i) ...

  4. 《A First Course in Probability》-chaper7-极限定理-强大数定理

    在现实问题中我们对于一个实验往往会重复成千上万次,那么我们就需要关注在实验次数趋于无穷之后,整个实验的期望会趋于怎样一个结果.其实这一章“极限定理”都是在处理这个问题. 强大数定理: 这里的证明过程给 ...

  5. 《算法问题实战策略》——chaper9——动态规划法技巧

    Q1: 数字游戏: 两个人(A.B)用n个整数排成的一排棋盘玩游戏,游戏从A开始,每个人有如下操作: (1)    拿走棋盘最右侧或者最左侧的棋子,被拿走的数字从棋盘中抹掉. (2)    棋盘中还剩 ...

  6. poj 3169 Layout

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8610   Accepted: 4147 Descriptio ...

  7. hdoj 1050 Moving Tables【贪心区间覆盖】

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. SQL-LINQ-Lambda语法对照

    SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Employees .Select ...

  9. Linux 下挂载硬盘的 方法

    1. 添加磁盘,查看磁盘状况 [root@db1 /]# fdisk -l Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sector ...

  10. 使用FileSystemWatcher监视文件变化

    本文转载:http://www.cnblogs.com/zanxiaofeng/archive/2011/01/08/1930583.html FileSystemWatcher基础 属性: Path ...