题目链接: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. 转 linux安装swoole扩展

    linux安装swoole扩展 发表于2年前(2014-09-03 14:05)   阅读(4404) | 评论(3) 7人收藏此文章, 我要收藏 赞2 上海源创会5月15日与你相约[玫瑰里],赶快来 ...

  2. VMbox复制虚拟机后网卡问题-bring up interface eth0:Device eth0 does not seem to be present

    1.使用 ifconfig -a 查看mac地址 eg:HWaddr:08:00:29:B2:2B   2.vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ...

  3. BZOJ 1010 玩具装箱(斜率优化DP)

    dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...

  4. 【bzoj4229】选择 离线+LCT

    题目描述 现在,我想知道自己是否还有选择. 给定n个点m条边的无向图以及顺序发生的q个事件. 每个事件都属于下面两种之一: 1.删除某一条图上仍存在的边 2.询问是否存在两条边不相交的路径可以从点u出 ...

  5. (一)MySQL学习笔记

    1.MySQL安装 下载地址:https://dev.mysql.com/downloads/mysql/ 启动mysql服务 net start mysql 停止mysql服务 net stop m ...

  6. Creator开源游戏、插件、教程、视频汇总

    Creator开源游戏.插件.教程.视频汇总 来源 http://forum.cocos.com/t/creator/44782 王哲首席客服   17-03-17    4   史上最全,没有之一. ...

  7. [洛谷P4111][HEOI2015]小Z的房间

    题目大意:有一个$n\times m$的房间,一些位置是房间,另一些位置是柱子,相邻两个房间之间有墙,问有多少种方案可以打通一些墙把所有房间连成一棵树,柱子不可以打通 题解:矩阵树定理,把房间当点,墙 ...

  8. bzoj1208: [HNOI2004]宠物收养所 (sbt)

    切傻逼题还能wa那么多次我也是醉了 好啦其实是sbt都不会敲了(一直用神器treap) 重点是研究了下陈大神的删除,以前treap的删除都是直接旋转去删的…… 还是treap大法好&…… 题解 ...

  9. POJ 3621 Sightseeing Cows | 01分数规划

    题目: http://poj.org/problem?id=3621 题解: 二分答案,检查有没有负环 #include<cstdio> #include<algorithm> ...

  10. cloneNode与事件拷贝

    用法: var newNode = oldNode.cloneNode(deep); //deep,布尔值,若为true,则克隆oldNode及其子节点,否则只克隆oldNode本身 关于复制事件 1 ...