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
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
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2722 2433 2923 2482 2377 
 

一口血喷在了屏幕上...

题目不难而且我的初始思路是对的,开始觉得数据不大没想用二分求高度而已,而且没用二分时间差不大,一倍多。

最后检查了几次,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 ;
}

最新文章

  1. 关于计算机改名无法连接TFS的问题
  2. CGI和ISAPI
  3. JavaWeb---设置content-disposition响应头,让浏览器下载文件
  4. Android bluetooth用户自定义数据
  5. Scrum 项目4.0--软件工程
  6. JavaScript : 基本的处理事件
  7. 【Android - 基础】之Animator属性动画
  8. SGU 144.Meeting
  9. RHEL7使用ssm命令管理LVM
  10. asp.net 2.0 Session丢失问题
  11. Maven详解(三)------ Maven工程目录介绍
  12. shell脚本-批量执行机器命令
  13. ffmpeg 在windows 上编译
  14. 【安富莱STM32H7教程】第1章 初学STM32H7的准备工作
  15. PHP jsonp ajax 跨域 实例
  16. SQL Server2017还原数据库时指定mdf文件及日志文件的名称
  17. 【转载】linux top命令查看内存及多核CPU的使用讲述
  18. kubernetes 常见问题整理
  19. 在浏览器中使用Javascript
  20. net::ERR_CONNECTION_RESET 报错原因

热门文章

  1. 4946: [Noi2017]蔬菜
  2. CC3200使用MQTT的SSL加密证书可用日期修改
  3. Omad群组部署、依赖部署一键解决
  4. WPF Style Setter use a TemplateBinding?
  5. 使用.net 更新word目录
  6. unity发布自定义分辨率
  7. leetcode-二叉树的层次遍历(Java)
  8. 最小生成树与Prim算法
  9. metamask注记词
  10. 感谢信——Alpha版