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 $ 跟前面 $ ...
随机推荐
- docker 的实践操作
查看版本信息 [root@k8s-1 ~]# docker version Client: Version: 18.09.6 API version: 1.39 Go version: go1.10. ...
- C++运算符重载复习
本人理解运算符重载实质 就类似函数重载 运算符重载都可以写成一个函数 里面传入参数 来调用 运算符重载不是必须的 但是重载后会方便很多. 小例子 一个类实现 ++ 和+某个数重载 大于号重载 ...
- 4 CSS导航栏&下拉菜单&属性选择器&属性和值选择器
CSS导航栏 熟练使用导航栏,对于任何网站都非常重要 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜单 垂直导航栏: <!DOCTYPE html> <html> & ...
- typescript 起步之安装及配置 ts-node 环境变量
最近vue 3.0 版本发布,让我认识到 typescript 将占有越来越重要的地位,所以我也开启了typescript学习之旅. 要想编写第一个 hello typescript 程序,当然要经过 ...
- Caffe2 手写字符识别(MNIST - Create a CNN from Scratch)[8]
本教程创建一个小的神经网络用于手写字符的识别.我们使用MNIST数据集进行训练和测试.这个数据集的训练集包含60000张来自500个人的手写字符的图像,测试集包含10000张独立于训练集的测试图像.你 ...
- P1120/UVA307 小木棍(sticks) 题解
题目描述 pdf 题解 注意的问题是,各个原始木棒的长度都是一样的! 说一下本题的总思路即:DFS+超强力剪枝!(详见本人的 AC 程序) 首先,我们要从小到大枚举原始木棒的长度len,也就是枚举答案 ...
- How to recover if NMC cound not connect
Some times we suddently find that the NMC can not login,. You would see the sybase database error if ...
- Python—数据类型之集合(Set)
1.集合是一个无序的,且不重复元素的集合.它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的. 2.基本功能包括关系测试和消除重复元素.注意:集合存在的意义就是去 ...
- 你需要知道的 JavaScript 类(class)的这些知识
作者: Dmitri Pavlutin译者:前端小智来源:dmitripavlutin 点赞再看,养成习惯 本文 GitHub https://github.com/qq44924588... 上已经 ...
- ecshop代码分析一(init.php文件)
ecshop代码分析一(init.php文件) 因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析: 首先是init.php文件,这个文件在ecshop每个页面都 ...