Day4 - I - Trucking HDU - 2962
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.
InputThe 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.OutputFor 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 思路:
读完题,发现没有告诉高度的范围,只告诉了最大值,然后问题是2维比较,时间有10k ms,就想到二分高度,判断时就只需要判断高度,然后正常最短路算法即可,
注意输出格式(PE三发)
const int INF = 0x3f3f3f3f;
int G[][], C, R, H[][], vis[], d[], Start, End, LimitHeight; struct Node {
int u, sum;
Node(int _u, int _sum):u(_u), sum(_sum) {}
bool operator<(const Node &a) const {
return a.sum < sum;
}
}; void init() {
for(int i = ; i <= C; ++i)
for(int j = ; j <= C; ++j) {
G[i][j] = ;
H[i][j] = ;
}
} int check(int height) {
for(int i = ; i <= C; ++i) {
vis[i] = ;
d[i] = INF;
}
priority_queue<Node> q;
q.push(Node(Start, ));
d[Start] = ;
while(!q.empty()) {
Node now = q.top();
q.pop();
int u = now.u;
if(vis[u]++) continue;
if(u == End) return d[End];
for(int i = ; i <= C; ++i) {
if(G[u][i] && (H[u][i] == - || H[u][i] >= height) && d[i] > d[u] + G[u][i]) {
d[i] = d[u] + G[u][i];
q.push(Node(i, d[i]));
}
}
}
return ;
} int main() {
ios::sync_with_stdio(false);
int t1, t2, t3, t4, kase = ;
while(cin >> C >> R && C+R) {
init();
for(int i = ; i <= R; ++i) {
cin >> t1 >> t2 >> t3 >> t4;
G[t1][t2] = G[t2][t1] = t4;
H[t1][t2] = H[t2][t1] = t3;
}
cin >> Start >> End >> LimitHeight;
int l = , r = LimitHeight, mid, tmp, Height=-, path=-;
while(l <= r) {
mid = (r + l) >> ;
tmp = check(mid);
if(tmp > ) {
Height = mid;
path = tmp;
l = mid + ;
} else
r = mid - ;
}
if(kase) cout << "\n";
cout << "Case " << ++kase << ":\n";
if(Height == -) {
cout << "cannot reach destination\n";
continue;
}
cout << "maximum height = " << Height << "\n";
cout << "length of shortest route = " << path << "\n";
}
return ;
}
Day4 - I - Trucking HDU - 2962的更多相关文章
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
- Trucking(HDU 2962 最短路+二分搜索)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2962 Trucking (最短路径)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU - 2962 Trucking SPFA+二分
Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...
- HDU 2962 Trucking
题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- Day4 - C - 六度分离 HDU - 1869
1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人 ...
- hdu 2962 最短路+二分
题意:最短路上有一条高度限制,给起点和最大高度,求满足高度最大情况下,最短路的距离 不明白为什么枚举所有高度就不对 #include<cstdio> #include<cstring ...
- hdu 2962 题解
题目 题意 给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot reach destination $ 跟前面 $ ...
随机推荐
- BigOps自动化运维安装以及所遇故障处理
本文参考官方文档进行安装,以及在安装中所遇到的问题呈现给大家.废话就不说了,开始安装.一.准备工作:本机系统环境是CentOS 7 x86 64位硬件配置建议物理内存8G+.CPU 4 cores+. ...
- 15 JavaScript弹窗(警告框alert、确认框confirm、提示框Promt)
警告框:window.alert().通常用于确认用户可以得到某些信息 <body> <script type="text/javascript" charset ...
- 九 三种Struts2访问Servlet方式总结
Servlet是单例的,Action是多例的. 多个程序访问Servlet只会创建一个Servlet对象,多个程序访问Action会创建对应的多个Action对象. 跳转页面可以获取对象的属性,说明使 ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:数据导出与导入
create directory dump_dir as 'd:\dump'; grant read,write on directory dump_dir to scott; --在cmd下 exp ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:缩写
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 缩写</title> <lin ...
- vs2013中配置SQLite数据库
转载:https://maplefan.com/index.php/2019/08/14/visual-studio-2013%e9%85%8d%e7%bd%aesqlite3%e7%9a%84%e6 ...
- 38 java 使用标签跳出多层嵌套循环
public class Interview { public static void main(String[] args) { //使用带标签的break跳出多层嵌套循环 Boolean flag ...
- Azure DNS-
先看什么是DNS,通常来讲,DNS是将域名解析成IP的服务,例如www.azure.cn对应的IP地址是139.217.8.104 使用域名访问有如下好处: 1. 好记,使用特定的字母组合,代替ip地 ...
- express框架开发接口部署线上环境PM2
1.PM2介绍 PM2是一个线上环境下,用于启动nodejs进程守护的工具,用来保证服务的稳定及分摊服务器进程和压力. 2.下载安装 npm install pm2 -g => pm2 --v ...
- SessionAttributes注解
SessionAttributes注解: a.该注解只能应用在类上: b.该注解用于将Map.ModelMap.Model或ModelAndView中的数据暂存到HttpSession中以使其可以在多 ...