In Touch

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 178    Accepted Submission(s): 44

Problem Description
There are n soda living in a straight line. soda are numbered by 1,2,…,n from left to right. The distance between two adjacent soda is 1 meter. Every soda has a teleporter. The teleporter of i-th soda can teleport to the soda whose distance between i-th soda is no less than li and no larger than ri. The cost to use i-th soda's teleporter is ci.

The 1-st soda is their leader and he wants to know the minimum cost needed to reach i-th soda (1≤i≤n). 

 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤2×105), the number of soda. 
The second line contains n integers l1,l2,…,ln. The third line contains n integers r1,r2,…,rn. The fourth line contains n integers c1,c2,…,cn. (0≤li≤ri≤n,1≤ci≤109)

 
Output
For each case, output n integers where i-th integer denotes the minimum cost needed to reach i-th soda. If 1-st soda cannot reach i-the soda, you should just output -1.
 
Sample Input
1
5
2 0 0 0 1
3 1 1 0 5
1 1 1 1 1
 
Sample Output
0 2 1 1 -1
 
大致思路: 从第一个点开始更新, 更新过的点缩成一个点, 因为在Dijkstra里 每次取出的都是最小的distance, 所以更新过的点 后面肯定不需要再次更新。更新一个点后加入堆中, 因为通过这个点可能更新别的点。
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+10;
typedef long long LL;
typedef pair <LL, int>pli;
const LL inf = 1LL << 60;
LL L[MAXN], R[MAXN], cost[MAXN], dis[MAXN];
int dsu[MAXN];
void init (){
for (int i = 0 ; i< MAXN; i++){
dis[i] = inf;
dsu[i] = i;
}
}
int find (int s){
return dsu[s] = (dsu[s] == s ? s : find(dsu[s]));
}
int main() {
int n, T;
scanf ("%d", &T);
while (T--) {
init();
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
scanf ("%I64d", L+i);
}
for (int i = 1; i <= n; i++) {
scanf ("%I64d", R+i);
}
for (int i = 1; i <= n; i++) {
scanf ("%I64d", cost+i);
}
init();
dis[1] = cost[1];
priority_queue<pli, vector<pli>, greater<pli> >Q;
Q.push(make_pair(0LL, 1));
while (!Q.empty()){
pli tmp = Q.top();
Q.pop();
int u = tmp.second;
for (int i = -1; i <= 1; i += 2){
int lf = L[u] * i + u;
int rg = R[u] * i + u;
if (lf > rg){
swap(lf, rg);
}
lf = max(lf, 1);
lf = min(lf, n + 1);
if (lf > rg){
continue;
}
for (int v = lf; ; v ++){
v = find(v);
if (v <= 0 || v > n || v > rg){
break;
}
if (dis[v] > dis[u] + cost[v]){
dis[v] = dis[u] + cost[v];
Q.push(make_pair(dis[v], v));
}
dsu[find(v)] = find(v + 1);
}
}
}
printf("0");
for (int i = 2; i <= n; i++) {
printf(" %I64d", dis[i] != inf ? dis[i] - cost[i] : -1);
}
printf("\n");
}
return 0;
}

  

(2015多校第6场)HDU5361--In Touch (Dijkstra应用)的更多相关文章

  1. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  2. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  3. HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)

    Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Sub ...

  4. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  5. hdu5289 2015多校联合第一场1002 Assignment

    题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...

  6. hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j ...

  7. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  8. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. android:ellipsize的使用

    EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...

  2. 初次使用cocoapods注意事项

    在仅仅用cocoapods时可能会遇到各种各样的错误和问题 这里中总结下: 1.首先使用cocoapods有非常多优点,在github上非常多优秀的开源项目都用到了它;假设你不会使用它,那么非常多优秀 ...

  3. Robotium -- AndroidUI优化工具HierarchyViewer

    为什么使用HierarchyViewer 不合理的布局会使我们的应用程序UI性能变慢,HierarchyViewer能够可视化的角度直观地获得UI布局设计结构和各种属性的信息,帮助我们优化布局设计.H ...

  4. Android入门之ActionBar实现Tab导航

    效果图: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=&qu ...

  5. jQuery 對table的基本操作

    一.鼠标移动到行更换背景色: css样式: .hover{ background-color: #cccc00; } Js脚本: $(document).ready(function () { //鼠 ...

  6. Java基础知识强化26(1):Object类之Object类的概述

    1.Object类 类Object是类层次结构的根类,每个类都使用 Object作为超类.所有对象(包括数组)都实现这个类的方法 每个类直接或者间接继承自Object类   2.Object类无参构造 ...

  7. cassandra新增、更新、删除数据。

    package client; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java ...

  8. 记一次T-SQL查询优化 索引的重要性

    概述 在一次调优一个项目组件的性能问题时,发现SQL的设计真的是非常的重要,所以写一篇博文来记录总结一下. 环境介绍 这个项目组件是一个Window服务,内部在使用轮循机会在处理一个事件表中的事件,将 ...

  9. C#方法的使用

    static void Main(string[] arr) { , ); Console.WriteLine(max); Console.ReadKey(); } /// <summary&g ...

  10. 汇编test和cmp区别

    来自http://tunps.com/assembly-test-and-cmp 看过破解教程,都知道test,cmp是比较关键,可是我一直不清楚它们究竟是怎么比较的,最后下决心找了很多资料,和大家一 ...