题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962

Trucking

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1763    Accepted Submission(s):
618

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
 
 
这一题是和某大神一起a的,结果被他说成了是一个人写的代码,哼,不再和他愉快的玩耍了!!!
好,不多说了,我们讨论这一题。
题目大意:
(1)这一题的输入数据有很多,第一行是两个数,表示城市和路,从第二行开始就是城市与城市之间的限高和路长。最后一行输入的是起始和终止点,还有一个就是卡车所能承受的最大高度。
(2)所以这题想输出的是从起始到结束最大高度下的最短路~~~注意题目所给的限高哦。
(3)还有一个就是当限高为-1时,那么这条路就是没有高度的限制。
(4)还有在中间有一个放行,小心PE。
(5)对了,还有最最后一个注意的就是这里采取二分来找能满足条件的最大高度,能够节省很大的时间~~
 
详见代码。
 
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std; struct node
{
int h,len;
} map[][]; int start,end,height,c;
int node[];
const int inf=; int Spfa(int high)
{
for (int i=; i<=c; i++)
node[i]=inf;
queue<int>q;
int inq[]= {};
int tm=start;
node[tm]=;
inq[tm]=;
q.push(tm);
while (!q.empty())
{
int s=q.front();
q.pop();
for (int i=; 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]=;
}
}
}
inq[s]=; }
if (node[end]!=inf)
return node[end];
else
return -;
} int main ()
{
int r,maxx,minn,h,k=;
while (cin>>c>>r&&(c||r))
{
int ans=-,cmp=-;
for(int i=; i<=c; i++)
{
for(int j=; j<=c; j++)
{
map[i][j].len=inf;
map[i][j].h=;
}
}
maxx=,minn=inf;
for (int i=; i<=r; i++)
{
int a,b,len; cin>>a>>b>>h>>len;
if(h==-) 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)>>;
//cout<<l<<" "<<r<<" "<<mid<<endl;
int flag=Spfa(mid);
if (flag!=-)
{
l=mid+;
ans=mid;
cmp=flag;
}
else
r=mid-;
}
/*for (int i=minn;i<=maxx;i++)//这就是所谓的超时的方法。。。
{
int flag=Spfa(i);
if(flag!=-1)
{
ans=i;
cmp=flag;
}
}*/
if (k>)
printf ("\n");//注意这里的格式问题,小心PE哦~
printf("Case %d:\n",k++);
if(ans==-)
printf("cannot reach destination\n");
else
{
printf ("maximum height = %d\n",ans);
printf ("length of shortest route = %d\n",cmp);
}
//printf("\n");
}
return ;
}

hdu 2962 Trucking (二分+最短路Spfa)的更多相关文章

  1. HDU - 2962 Trucking SPFA+二分

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

  2. hdu 2962 Trucking (最短路径)

    Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 2962 Trucking

    题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...

  4. HDU 6071 同余最短路 spfa

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  5. [模板][HDU]P2544[单源最短路][SPFA]

    题目就不放了,主要是写一下SPFA,很少写,今天特别学了一个用STL的队列来做的. 代码: #include<iostream> #include<cstdio> #inclu ...

  6. BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ...

  7. 二分+最短路 UVALive - 4223

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

  8. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  9. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

随机推荐

  1. 【week2】Scrum中的站立会议

    Scrum站立会议    站立会议给我的第一印象就是站着开会,在经过我查阅资料之后,发现也是差不多的意思.学术一点的分析就是在Sprint开始后,团队将会在每个工作日特定时间举行一个简短会议,每次会议 ...

  2. AppScan工作原理&操作教程

    一.AppScan的工作原理 对一个综合性的大型网站来说,可能存在成千上万的页面.以登录界面为例,至少要输入用户名和密码,即该页面存在两个字段,当提交了用户名和密码等登录信息,网站需要检查是否正确,这 ...

  3. Kubernetes初探 :总体概述及使用示例

    Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...

  4. matlab 并行运算【转】

    一.Matlab并行计算原理梗概 Matlab的并行计算实质还是从主从结构的分布式计算.当你初始化Matlab并行计算环境时,你最初的Matlab进行自动成为主节点,同时初始化多个(具体个数手动设定, ...

  5. 【Luogu】P2901牛慢跑(K短路模板)

    题目链接 K短路居然用A*……奇妙. 先建反图从终点(1)跑一遍最短路,再A*,用堆存当前点到终点距离+从起点到当前点距离. 每次取出终点都可以视为发现了一个新的最短路. #include<cs ...

  6. mysql 迁移 mariadb

    背景: mysql5.7数据库安装在windows环境中,数据需要迁移到CentOS7.4的mariadb5.5中.web应用是采用springboot2.x开发的,迁移数据完成后,还需要简单修改一些 ...

  7. BZOJ1509 & 洛谷4408:[NOI2003]逃学的小孩——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1509 https://www.luogu.org/problemnew/show/P4408 sb ...

  8. windows下vue项目启动步骤

    原创:https://blog.csdn.net/qq_27680317/article/details/71123051?locationNum=10&fps=1 不是ngnix服务器是,忽 ...

  9. Hive架构及应用介绍【链接】

    原文链接:https://blog.csdn.net/a2011480169/article/details/51482799

  10. 负载均衡配置(基于Nginx)

    以下是基于nginx进行负载均衡配置的流程: 服务器配置如下: 1.  安装nginx的服务器:192.168.1.1 2.  nginx配置负载均衡位置及端口:192.168.1.1 80端口 3. ...