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. JDK动态代理实现原理--转载

    之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白.比如说:InvocationHandler的invoke方法是由谁来调用的,代理对象是怎么生成的,直到前几个星期才把这些问题全部搞明白了.  ...

  2. php中运用GD库实现简单验证码

    昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...

  3. 【移动开发】Android中将我们平时积累的工具类打包

    Android开发的组件打包成JAR安装包,通过封闭成JAR包,可以重复利用,非常有利于扩展和减少工作重复性.这里为了讲解方便,我用了之前的一个代码框架中核心部分,不了解的可以回头看一下:http:/ ...

  4. HTTP知识点总结

    参考: HTTP协议详解:http://blog.csdn.net/gueter/article/details/1524447 图解HTTP学习笔记——简单的HTTP协议:http://networ ...

  5. MM32 RTC学习(兼容STM32)

    RTC学习 RTC简述 实时时钟是一个独立的定时器. RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能. 修改计数器的值可以重新设置系统当前的时间和日期. RTC模块和时钟配 ...

  6. int? 类型数据

    在数据库操作中,会遇到在int的单元格恰好为NULL值的情况,这个时候我们可以直接判断是否为null然后进行赋值,有人就想那我刚好用一下:?表达式不就好了: ) ? ); 这时候编译器会报错,原因就是 ...

  7. easyui-tree绑定数据的几种方式

    没想到easyui对json数据格式要求的那么严谨,折腾了半天 第一种直接使用标签方式,很容易就加载出来了: <ul class="easyui-tree"> < ...

  8. FineUI页面级别的参数配置

    Theme: 控件主题,目前支持三种主题风格(blue/gray/access,默认值:blue) Language: 控件语言(en/zh_CN/zh_TW/...,默认值:zh_CN) FormM ...

  9. WinAPI——谐振动的合成

    #include<Windows.h> #include<math.h> #define NUM 1000 #define PI 3.14159 LRESULT CALLBAC ...

  10. 如何给report自定义page number

    问题描述: report在设置分页后会自动分页,但是有默认的page number,现在的问题是有时default page number不能满足我们的需求,此时就需要自定义page number. ...