hdu 2363 Cycling (最短路spfa + 暴力枚举)
来源:互联网 发布:网络公开课IT 编辑:程序博客网 时间:2024/05/18 01:54
Cycling
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 114 Accepted Submission(s): 52Problem Description
You want to cycle to a programming contest. The shortest route to the contest might be over the tops of some mountains and through some valleys. From past experience you know that you perform badly in programming contests after experiencing large differences in altitude. Therefore you decide to take the route that minimizes the altitude difference, where the altitude difference of a route is the difference between the maximum and the minimum height on the route. Your job is to write a program that finds this route.
You are given:
the number of crossings and their altitudes, and
the roads by which these crossings are connected.
Your program must find the route that minimizes the altitude difference between the highest and the lowest point on the route. If there are multiple possibilities, choose the shortest one.
For example:
In this case the shortest path from 1 to 7 would be through 2, 3 and 4, but the altitude difference of that path is 8. So, you prefer to go through 5, 6 and 4 for an altitude difference of 2. (Note that going from 6 directly to 7 directly would have the same difference in altitude, but the path would be longer!)
You are given:
the number of crossings and their altitudes, and
the roads by which these crossings are connected.
Your program must find the route that minimizes the altitude difference between the highest and the lowest point on the route. If there are multiple possibilities, choose the shortest one.
For example:
In this case the shortest path from 1 to 7 would be through 2, 3 and 4, but the altitude difference of that path is 8. So, you prefer to go through 5, 6 and 4 for an altitude difference of 2. (Note that going from 6 directly to 7 directly would have the same difference in altitude, but the path would be longer!)
Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:
One line with two integers n (1 <= n <= 100) and m (0 <= m <= 5000): the number of crossings and the number of roads. The crossings are numbered 1..n.
n lines with one integer hi (0 <= hi <= 1 000 000 000): the altitude of the i-th crossing.
m lines with three integers aj , bj (1 <= aj , bj <= n) and cj (1 <= cj <= 1 000 000): this indicates that there is a two-way road between crossings aj and bj of length cj . You may assume that the altitude on a road between two crossings changes linearly.
You start at crossing 1 and the contest is at crossing n. It is guaranteed that it is possible to reach the programming contest from your home.
One line with two integers n (1 <= n <= 100) and m (0 <= m <= 5000): the number of crossings and the number of roads. The crossings are numbered 1..n.
n lines with one integer hi (0 <= hi <= 1 000 000 000): the altitude of the i-th crossing.
m lines with three integers aj , bj (1 <= aj , bj <= n) and cj (1 <= cj <= 1 000 000): this indicates that there is a two-way road between crossings aj and bj of length cj . You may assume that the altitude on a road between two crossings changes linearly.
You start at crossing 1 and the contest is at crossing n. It is guaranteed that it is possible to reach the programming contest from your home.
Output
For each testcase, output one line with two integers separated by a single space:
the minimum altitude difference, and
the length of shortest path with this altitude difference.
the minimum altitude difference, and
the length of shortest path with this altitude difference.
Sample Input
17 949133541 2 12 3 13 4 14 7 11 5 45 6 46 7 45 3 26 4 2
Sample Output
2 11
Source
bapc2007_pre
Recommend
lcy
把所有点的高度差都没举出来,从小到大排序,然后从小到大,求最短路。
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <vector>using namespace std;constexpr int INF = 0x3fffffff;int n, m;struct Altitude { int high; int low;}altitude[10050];struct Node { Node() = default; Node(int vv, int ww):v(vv), w(ww){} int v; int w;};vector<Node> map[105];int a[105]; //各个点的高度int dis[105];int visited[105];void spfa(int high, int low) { int v, u, w; for (int i = 1; i <= 100; ++i) { dis[i] = INF; visited[i] = 0; } if (a[1] < low || a[1] > high)//起点范围判断。 return; dis[1] = 0; queue<int> que; que.push(1); // 从1开始 visited[1] = 1; while (!que.empty()) { u = que.front(); que.pop(); visited[u] = 0; //cout << map[u].size() << endl; for (int i = 0; i < map[u].size(); ++i) { v = map[u][i].v; if (a[v] < low || a[v] > high) continue; w = map[u][i].w; if (dis[u] + w < dis[v]) { dis[v] = dis[u] + w; if (!visited[v]) { que.push(v); visited[v] = 1; } } } }}int main() { int t; cin >> t; while (t--) { for (int i = 0; i <= 100; ++i) { map[i].clear(); } cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int k = 0; for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { altitude[k].high = max(a[i], a[j]); altitude[k++].low = min(a[i], a[j]); } } for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; //implicit construct map[u].push_back({v, w}); map[v].push_back({u, w}); } //lambda function sort(altitude, altitude + k, [](Altitude a1, Altitude a2)->bool{return (a1.high - a1.low) < (a2.high - a2.low);}); int i; for (i = 0; i < k; ++i) { spfa(altitude[i].high, altitude[i].low); if (dis[n] != INF) { break; } } cout << altitude[i].high - altitude[i].low << " " << dis[n] << endl; } }
阅读全文
0 0
- hdu 2363 Cycling (最短路spfa + 暴力枚举)
- HDU 2363 Cycling(枚举+最短路)
- HDU 2363 Cycling(并查集 SPFA 暴力枚举)
- HDU 2363 Cycling 最短路+枚举
- HDU 2363 Cycling 枚举限制+最短路
- 【HDU】2363 Cycling 最短路
- HDU 2363 Cycling(二分+枚举+限制最短路,好题)
- hdu 2363 Cycling Dijkstra最短路
- HDU 2363 Cycling 二分+ 限制最短路
- 【最短路】HDOJ 2363 Cycling
- hdu1595(枚举+最短路,Dijkstra/SPFA)
- hdu 2363(最短路+枚举)
- HDU 2544-最短路(最短路spfa)
- HDU 2544 最短路(最短路/spfa)
- HDU 2544 最短路 (Dijkstra || SPFA)
- hdu 2544 最短路(dijkstra||spfa)
- hdu-2544-最短路(SPFA)
- hdu 2544 最短路(SPFA算法)
- Python中利用set去除列表(list)重复元素
- 融云SDK设置头像与消息监听注意事项
- Cadence学习总结
- HealthKit读取步数、运动距离、已爬楼层等信息
- 简述极大似然估计
- hdu 2363 Cycling (最短路spfa + 暴力枚举)
- SwipeRefreshLayout自动刷新的问题
- Maven使用中的常见问题整理
- 2.React中文之Introducing JSX
- 关于js Uncaught TypeError: Cannot read property 'style' of null 问题
- JAVA 线程中的synchronized关键字的使用
- 乘法逆元、扩展欧几里得算法、二元一次方程、a的n次方取余
- 字符串连接
- 进程管理