F - Trucking

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

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
题意:lLA给你n个节点(<=1000),节点之间最多一条路,路有长度l和高度限制h,现在一辆卡车准备从起点s到终点t,要求
卡车的高度不得高于H,问卡车能达到终点的最高高度H和此最高高度下的最短路径;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
struct edge{
int to,h,l;
};
vector<edge> G[1005];
int dis[1005],inq[1005]; bool ok(int s,int t,int h)
{
queue<int> q;
MM(dis,inf);
MM(inq,0); q.push(s);
inq[s]=1;
dis[s]=0; while(q.size())
{
int u=q.front();q.pop();inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
edge e=G[u][i];
if(e.h<h) continue;
if(dis[e.to]>dis[u]+e.l)
{
dis[e.to]=dis[u]+e.l;
if(!inq[e.to]) {q.push(e.to);inq[e.to]=1;}
}
}
}
return dis[t]!=inf;
} int main()
{
int n,m,kk=0;
scanf("%d %d",&n,&m);
while(1)
{
if(!(n||m)) break;
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=m;i++)
{
int u,v,h,l;
scanf("%d%d%d%d",&u,&v,&h,&l);
if(h==-1) h=inf;
G[u].push_back((edge){v,h,l});
G[v].push_back((edge){u,h,l});
}
int s,t,H;scanf("%d%d%d",&s,&t,&H);
int l=0,r=H+1,ansl=inf;
while(r-l>1)
{
int mid=(l+r)/2;
if(ok(s,t,mid)) {l=mid;ansl=dis[t];}
else r=mid;
}
printf("Case %d:\n",++kk);
if(ansl==inf) printf("cannot reach destination\n");
else {
printf("maximum height = %d\n",l);
printf("length of shortest route = %d\n",ansl);
}
scanf("%d%d",&n,&m);if(n||m) printf("\n");
else break;
}
return 0;
}

  分析:代码有毒啊,,,SPFA最坏情况V*E,按理说应该会被卡的,,,,,,还是dijkstra+堆优化比较稳;

分析:很简单的图论题,最大化某个值二分就当然想到了,关键是找路,其实就是在最短路的代码里面,加上一个对

路径边的选择,就是如果当前路径的高度限制<二分枚举的值得话,那么这条路当然不能选择(其实也就是相当于在

原来的图中把限制高度<二分枚举的h边全部擦掉),,简单的一笔,比以前做的图论题简单不知道哪里去了,,比赛

被吓傻了

 

最新文章

  1. 希尔排序(java)
  2. java 获取本地电脑的分辨率代码
  3. Linux内核设计第一周 ——从汇编语言出发理解计算机工作原理
  4. Jil序列化JSON
  5. 《javascript高级程序设计》 第25章 新兴的API
  6. AceAdmin In MVC之控件
  7. PAT (Basic Level) 1001害死人不偿命的(3n+1)猜想 (15)
  8. 谈谈css3的字体大小单位[rem]
  9. Steps UVA 846
  10. 总结Ajax验证注册功能的两种方式
  11. Apollo源码阅读笔记(一)
  12. 网页性能优化:防止JavaScript、CSS阻塞浏览器渲染页面
  13. 构造方法和一般方法的区别(面试)-----java基础知识总结
  14. java-容器-ArrayList
  15. convert2utf8withbom
  16. openfire聊天记录插件
  17. python 面向对象 issubclass
  18. 【Coursera】History: Dawn of Electronic Computing学后小结
  19. springboot线程池的使用和扩展
  20. scala集合与数据结构

热门文章

  1. 用python实现自己的http服务器——多进程、多线程、协程、单进程非堵塞版、epoll版
  2. MySQL8在CentOS7上的安装
  3. 百度后端C++电话一面
  4. 牛客 109B 好位置 (字符串水题)
  5. tensorflow零起点快速入门(3)
  6. js判断一个 object 对象是否为空
  7. ython实现进程间的通信有Queue,Pipe,Value+Array等,其中Queue实现多个进程间的通信,而Pipe实现两个进程间通信,而Value+Array使用得是共享内存映射文件的方式,所以速度比较快
  8. 文件下载不可以使用ajax
  9. vue项目打包文件配置(vue-clli3)
  10. maven入门--part2 安装