Trucking(HDU 2962 最短路+二分搜索)

时间:2022-03-13 14:00:22

Trucking

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

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
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

题意:给定一个无向图,每条边有长度,通过最大高度两个权值,求解从起点到终点的能通过的最大高度以及在此高度上的最短路径长度

思路:二分搜索,每次进行一次最短路算法Dijkstra,路径更新公式需要添加   map[i][v].h>=p||map[i][v].h==-1

需要注意最后一行不能输出\n  PE了两次

 #include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Max 1005
#define INF 9999999
struct node
{
int len,h;
}map[Max][Max];
bool vis[Max];
int dis[Max];
int c,r;
int s,e,height; bool Dijkstra(int p)
{
int i,j;
memset(vis,,sizeof(vis));
for(i=;i<=c;i++)
dis[i]=INF;
dis[s]=;
while(true)
{
int v=;
for(i=;i<=c;i++)
{
if(!vis[i]&&(v==||dis[i]<dis[v]))
v=i;
}
if(v==)
break;
vis[v]=;
//cout<<v<<endl;
// cout<<"3 "<<dis[3]<<endl;
for(i=;i<=c;i++)
{
if(vis[i]==&&(map[i][v].h>=p||map[i][v].h==-)&&(dis[v]+map[i][v].len<dis[i]))
dis[i]=dis[v]+map[i][v].len;
}
}
return dis[e]!=INF;
} int main()
{
int i,j;
int a,b;
int t=;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&c,&r))
{
if(c==&&r==)
break;
for(i=;i<=c;i++)
for(j=;j<=c;j++)
{
map[i][j].h=;
map[i][j].len=INF;
}
for(i=;i<r;i++)
{
scanf("%d%d",&a,&b);
scanf("%d%d",&map[a][b].h,&map[a][b].len);
map[b][a].h=map[a][b].h;
map[b][a].len=map[a][b].len;
}
scanf("%d%d%d",&s,&e,&height);
int lb=,rb=height+,mid,ans,length=INF;
while(rb-lb>)
{
mid=(rb+lb)/;
if(Dijkstra(mid))
{
length=dis[e];
ans=mid;
lb=mid;
}
else
rb=mid;
}
if(t!=)
printf("\n");
if(length==INF)
printf("Case %d:\ncannot reach destination\n",t++);
else
{
printf("Case %d:\n",t++);
printf("maximum height = %d\n",ans);
printf("length of shortest route = %d\n",length);
}
}
}

Trucking(HDU 2962 最短路+二分搜索)的更多相关文章

  1. Day4 - I - Trucking HDU - 2962

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

  2. hdu 2962 最短路&plus;二分

    题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...

  3. ACM&colon; HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  4. UESTC 30 &amp&semi;&amp&semi;HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  7. hdu 2962 Trucking &lpar;二分&plus;最短路Spfa&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...

  8. hdu 2962 Trucking &lpar;最短路径&rpar;

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

  9. HDU - 2962 Trucking SPFA&plus;二分

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

随机推荐

  1. 移植SlidingMenu Android library,和安装example出现的问题解决

    很多项目都用到类似左侧滑动菜单的效果,比如facebook,evernote,VLC for android等等,这很酷 源代码可以从GitHub的https://github.com/jfeinst ...

  2. JS兼容所有浏览器的一段加入收藏代码,设置为首页

    <script language="javascript" type="text/javascript"> function addfavorite ...

  3. C&num;输出log信息

    在写程序的过程中,有时候我们需要添加一些log信息,这个时候,可以采用下面的方法来实现. public static void WriteLog(string ExtraMsg, Exception ...

  4. javaScript与MVC

    MVC,就是Module,View,Controller分离,使业务逻辑更加清晰,但是现在公司的项目中很多地方那个不是这样的,很多业务逻辑放在了javascript中实现,这样做的优点就是对于技术要求 ...

  5. 【转】int const A&colon;&colon;func&lpar;&rpar;和int A&colon;&colon;func&lpar;&rpar; const

    int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...

  6. Android OpenGL ES(十二)&colon;三维坐标系及坐标变换初步 &period;

    OpenGL ES图形库最终的结果是在二维平面上显示3D物体(常称作模型Model)这是因为目前的打部分显示器还只能显示二维图形.但我们在构造3D模型时必须要有空间现象能力,所有对模型的描述还是使用三 ...

  7. 指针--摘自C&plus;&plus;技术网 作者dx

    “指针是什么?”“指针就是一种数据类型.”“你确定?”“那数据类型是什么?额,这个???类型就是类型,还能怎么解释嘛.”“指针有多少种?”“指针有好多种,比如整型指针,字符指针等等.”“指针是怎么确定 ...

  8. 1分钟选好最合适你的JavaScript框架

    欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~​ Javascript框架(以下简称框架)也被称为Javascript库,是一组包含丰富功能和函数的JavaScript代码集, ...

  9. &num;YCB&num;待做题目与填坑资料

    各种填坑资料(qwq) 主席树(by YL)戳 树套树(by ZSY)戳 不要问我这些题咋来的(查大佬的水表呗) 题目列表: [HDU5977]Garden of Eden [BZOJ2752][HA ...

  10. ubuntu 16&period;04 源代码安装libusb

    libusb是一个跨平台的usb通讯库:https://libusb.info/ 在ubuntu16上安装 1. 首先安装 libudev-dev sudo apt-get install libud ...