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 $ 跟前面 $ ...
随机推荐
- 在 ubuntu 中安装python虚拟环境
直接看命令一路操作(注:python3 下): 1.安装虚拟环境: sudo pip3 install virtualenv 2.安装虚拟环境扩展管理工具: sudo pip3 install vir ...
- 用Struts2框架报错:The Struts dispatcher cannot be found
报错信息 The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the ...
- SpringBoot 获得 properties 文件中数据方式
参考:https://blog.csdn.net/qq_37171353/article/details/78005845
- How to backup on MSSQL by table level ?
MSSQL is good database. Unlike as Oracle, it seems that can not backup sqlserver databasee tables ...
- Servlet简单的登陆窗口
web.xml配置: jsp代码: 注意:action书写的是为这次登陆做处理类的别名,method就是请求的方式 Get请求方式没有请求实体 //如果只是看效果就光写一下service方法就行了 p ...
- 本周总结(19年暑假)—— Part5
日期:2019.8.11 博客期:111 星期日
- Decimal为SQL Server、MySql等数据库的一种数据类型
Decimal为SQL Server.MySql等数据库的一种数据类型,不属于浮点数类型,可以在定义时划定整数部份以及小数部分的位数.使用精确小数类型不仅能够保证数据计算更为精确,还可以节省储存空间, ...
- HTML相关知识点
标签: 块元素:可以设置宽高,div, 行内元素:不可以设置宽高,span,image, display:inline;//转换成行内元素 display:block;//转换成块元素 display ...
- java并发队列
阻塞队列 常见的阻塞队列有ArrayBlockingQueue,LinkedBlockingDeque,LinkedBlockingQueue,这些队列有界且可以阻塞线程 ArrayBlockingQ ...
- Java的clone方法效率问题
在Java中,经常会需要新建一个对象,很多情况下,需要这个新建的对象和现有的某个对象保持属性一致. 那么,就有两种方式来实现这个对象的构造: ①通过新建一个对象,为这个对象的属性根据原有对象的属性来进 ...