hdu 5361 In Touch(最短路+并查集)
来源:互联网 发布:中国大学生软件杯 编辑:程序博客网 时间:2024/06/16 12:46
题目链接:hdu 5361 In Touch
最短路,D[i]表示从i节点出发时最短距离,最后答案减掉C[i]即可。然后用并查集优化维护处理过的节点。
#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 2 * 1e5 + 5;const ll inf = 1LL << 60;struct Pi {int pos;ll dis;Pi (int pos = 0, ll dis = 0): pos(pos), dis(dis) {}bool operator < (const Pi& u) const {return dis > u.dis;}};int N, L[maxn], R[maxn], C[maxn], F[maxn];ll D[maxn];int find (int x) {return F[x] = (F[x] == x ? x : find(F[x]));}void init () {scanf("%d", &N);for (int i = 1; i <= N; i++) {F[i] = i;D[i] = inf;}for (int i = 1; i <= N; i++)scanf("%d", &L[i]);for (int i = 1; i <= N; i++)scanf("%d", &R[i]);for (int i = 1; i <= N; i++)scanf("%d", &C[i]);}void solve (int s) {D[s] = C[s];priority_queue<Pi> Q;Q.push(Pi(s, D[s]));while (!Q.empty()) {int u = Q.top().pos;Q.pop();for (int i = -1; i <= 1; i += 2) {int lp = u + L[u] * i;int rp = u + R[u] * i;if (lp > rp)swap(lp, rp);lp = max(lp, 1);lp = min(lp, N + 1);if (lp > rp)continue;while (true) {lp = find(lp);if (lp <= 0 || lp > N || lp > rp)break;if (D[lp] > D[u] + C[lp]) {D[lp] = D[u] + C[lp];Q.push(Pi(lp, D[lp]));}F[find(lp)] = find(lp + 1);lp++;}}}printf("0");for (int i = 2; i <= N; i++)printf(" %I64d", D[i] == inf ? -1 : D[i] - C[i]);printf("\n");}int main () {int cas;scanf("%d", &cas);while (cas--) {init();solve(1);}return 0;}
0 0
- hdu 5361 In Touch(最短路+并查集)
- HDU5361In Touch (最短路+并查集)
- HDU 5361(最短路+并查集优化)
- HDU 5361 In Touch(最短路 + 线段树)
- HDU 5361 In Touch (优先队列+并查集优化)
- HDU 5361 最短路+并查集优化
- hdu 5361 In Touch 最短路(set+搜索实现)
- In Touch (hdu 5361 优先队列的Dij + 并查集优化)
- HDU 5361 In Touch(并查集实现区间删除)
- HDU 5360 - In Touch(最短路)
- HDU 5361 In Touch (2015 多校6 1009 最短路 + 区间更新)
- [BZOJ3694]最短路(并查集)
- HDOJ 5361 In Touch dijkstra最短路
- bzoj3694 最短路 并查集(树链剖分)
- hdu5361(最短路+并查集)
- hdu5361 最短路+并查集
- 做运动 并查集+最短路
- 【HDU】5910 Advanced Traffic System【线段树+并查集】【数据结构模拟最短路】
- 伊斯坦布尔之旅第一站:韩国仁川机场免费中转旅游
- poj 1185 炮兵阵地 (状态压缩)
- HDU 5355 Cake(数学 DFS)
- POJ-1833 排列-字典序
- c++类实现二叉查找树的抽象数据结构
- hdu 5361 In Touch(最短路+并查集)
- hdu 2084 - 数塔(解题报告)
- waitpid和SIGCHLD信号回收僵尸进程
- List,Map的遍历
- POJ-2136 Vertical Histogram-用*号统计字母个数
- 【bzoj2141】 排队 树状数组+主席树
- NSKeyedArchiver
- B. ZgukistringZ-简单字符串处理-Codeforces Round #307 (Div. 2)
- Java IO流--IO包中的其他类