hdu 2962 Trucking (二分+最短路Spfa)
题目链接: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
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.
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.
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.
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
#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)的更多相关文章
- HDU - 2962 Trucking SPFA+二分
Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...
- hdu 2962 Trucking (最短路径)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2962 Trucking
题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...
- HDU 6071 同余最短路 spfa
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- [模板][HDU]P2544[单源最短路][SPFA]
题目就不放了,主要是写一下SPFA,很少写,今天特别学了一个用STL的队列来做的. 代码: #include<iostream> #include<cstdio> #inclu ...
- BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ...
- 二分+最短路 UVALive - 4223
题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
随机推荐
- NeoLoad系列- 快速上手教程
1.新建工程 2.点击录制脚本按钮 3.在弹出的开始录制对话框中,填写虚拟用户信息. Record in下拉框,用来填写用户路径,一般有三个容器组成: Init, Actions, and End.当 ...
- django为model设置表名
class redis_data(models.Model): class Meta: db_table='redis_data' key=models.CharFie ...
- 窗口中各模块的切换效果,使用jquery实现
用到了两个js库,请自行下载,用到的背景图片可任意图片都可以,主要是看效果 <!DOCTYPE html> <html> <head> <script src ...
- 【刷题】BZOJ 3998 [TJOI2015]弦论
Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...
- hdu1950 Bridging signals
LIS nlogn的时间复杂度,之前没有写过. 思路是d[i]保存长度为i的单调不下降子序列末尾的最小值. 更新时候,如果a[i]>d[len],(len为目前最长的单调不下降子序列) d[++ ...
- BZOJ1027 [HNOI2004]打鼹鼠 【dp】
1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3647 Solved: 1746 [Submit][Sta ...
- 洛谷 P2730 魔板 Magic Squares 解题报告
P2730 魔板 Magic Squares 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 ...
- Ark组件[转]
Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...
- 2016多校联合训练1 B题Chess (博弈论 SG函数)
题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外 ...
- 探索CAS无锁技术
前言:关于同步,很多人都知道synchronized,Reentrantlock等加锁技术,这种方式也很好理解,是在线程访问的临界区资源上建立一个阻塞机制,需要线程等待 其它线程释放了锁,它才能运行. ...