题目链接: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. Windows资源监控工具大全

    在利用LoadRunner进行性能测试的时候,Windows服务器的资源是经常需要监控的对象.其实除了LoadRunner提供的计数器,似乎Window服务器并不像Unix或者Linux提供众多的性能 ...

  2. 发送缓冲区sk_wmem_queued

    sk_wmem_queued是目前发送缓冲区的量 tcp_trim_head 把这快内存给去掉, 什么时候会加入到内存里呢?__tcp_add_write_queue_tail, skb里的内存是啥? ...

  3. Chrome Extensions API & options

    Chrome Extensions API options https://developer.chrome.com/extensions https://developer.chrome.com/e ...

  4. KMP算法字符串查找子串

    题目: 经典的KMP算法 分析: 和KMP算法对应的是BF算法,其中BF算法时间复杂度,最坏情况下可以达到O(n*m),而KMP算法的时间复杂度是O(n + m),所以,KMP算法效率高很多. 但是K ...

  5. [C/C++] C++抽象类

    转自:http://www.cnblogs.com/dongsheng/p/3343939.html 一.纯虚函数定义 纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己 ...

  6. Spring MVC实践

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...

  7. Oracle 获取 某个表的建表SQL

    获取A表的创表SQL select dbms_metadata.get_ddl('TABLE','A') from dual

  8. bzoj3709: [PA2014]Bohater 贪心

    ~~~题面~~~ 题解: 首先有一个比较明显的策略,肯定先要把能带给自己受益的先选完,然后再以最佳状态去打那些会给自己带来损失的怪. 对于前一部分(可以带来受益的怪),显然我们需要先从代价小的打起,因 ...

  9. MySQL - General error: 1390 Prepared statement contains too many placeholders

    报错原因:预处理 SQL语句时使用的占位符数量超过了最大限制(默认65535). 解决方案:拆分查询语句,每次使用的占位符低于限制即可.

  10. 虚拟机网络连接模式中桥接模式和NAT模式的区别

    1.桥接模式:当虚拟机系统的网络连接模式为桥接模式时,相当于在主机系统和虚拟机系统之间连接了一个网桥,而网桥两端的网络都属于同一网络,主机和虚拟机是处于同一网络中的对等主机. 实例,在使用Xshell ...