hdu 2962 Trucking (最短路径)
2024-09-20 17:16:54
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1692 Accepted Submission(s): 587
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
3
1
1 2 -1 100
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
Source
Recommend
一口血喷在了屏幕上...
题目不难而且我的初始思路是对的,开始觉得数据不大没想用二分求高度而已,而且没用二分时间差不大,一倍多。
最后检查了几次,wa了好几次,才发现是没有跳出 0 0!!还以为二分又写错了= =
用C写的邻接表比vector快点。
最短路+枚举高度:
//140MS 600K 1953 B C++
#include<iostream>
#include<queue>
#define N 1005
#define inf 0x3fffffff
using namespace std;
struct node{
int v,h,d;
int next;
}edge[*N];
vector<node>V[N];
int d[N];
int vis[N];
int n,s,e;
int head[N],edgenum;
void addedge(int u,int v,int h,int d)
{
edge[edgenum].v=v;
edge[edgenum].h=h;
edge[edgenum].d=d;
edge[edgenum].next=head[u];
head[u]=edgenum++;
}
void spfa(int s,int maxh)
{
for(int i=;i<=n;i++)
d[i]=inf;
memset(vis,,sizeof(vis));
queue<int>Q;
d[s]=;
Q.push(s);
vis[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
int h=edge[i].h;
int w=edge[i].d;
if(d[v]>d[u]+w && h>=maxh){
d[v]=d[u]+w;
if(!vis[v]){
Q.push(v);
vis[v]=;
}
}
}
}
}
int main(void)
{
int m,a,b,h,c,k=;
while(scanf("%d%d",&n,&m)!=EOF && (n+m))
{
if(k!=) printf("\n");
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
V[i].clear();
edgenum=;
for(int i=;i<m;i++){
scanf("%d%d%d%d",&a,&b,&h,&c);
if(h==-) h=inf;
addedge(a,b,h,c);
addedge(b,a,h,c);
}
scanf("%d%d%d",&s,&e,&h);
printf("Case %d:\n",k++);
int ans=inf;
int l=,r=h;
while(l<r){
int mid=(l+r+)>>;
spfa(s,mid);
if(d[e]!=inf){
l=mid;ans=d[e];
}else r=mid-;
}
if(ans!=inf){
printf("maximum height = %d\n",r);
printf("length of shortest route = %d\n",ans);
}else printf("cannot reach destination\n");
}
return ;
}
最新文章
- 关于计算机改名无法连接TFS的问题
- CGI和ISAPI
- JavaWeb---设置content-disposition响应头,让浏览器下载文件
- Android bluetooth用户自定义数据
- Scrum 项目4.0--软件工程
- JavaScript : 基本的处理事件
- 【Android - 基础】之Animator属性动画
- SGU 144.Meeting
- RHEL7使用ssm命令管理LVM
- asp.net 2.0 Session丢失问题
- Maven详解(三)------ Maven工程目录介绍
- shell脚本-批量执行机器命令
- ffmpeg 在windows 上编译
- 【安富莱STM32H7教程】第1章 初学STM32H7的准备工作
- PHP jsonp ajax 跨域 实例
- SQL Server2017还原数据库时指定mdf文件及日志文件的名称
- 【转载】linux top命令查看内存及多核CPU的使用讲述
- kubernetes 常见问题整理
- 在浏览器中使用Javascript
- net::ERR_CONNECTION_RESET 报错原因