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. poj2096 Collecting Bugs(概率dp)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 1792   Accepted: 832 C ...

  2. Windows7 64位系统搭建Cocos2d-x 2.2.1最新版以及Android交叉编译环境(具体教程)

    原文地址:http://blog.csdn.net/sttyytw/article/details/17005263 声明:本教程在參考了下面博文,并经过自己的摸索后实际操作得出,本教程系本人原创,因 ...

  3. C++标准库之 Lower_Bound, upper_Bound

    关于二分查找,这绝对是最简单却又最难的实现了,其各种版本号能够參见http://blog.csdn.net/xuqingict/article/details/17335833 在C++的标准库中,便 ...

  4. IOS后台执行机制 与 动作

    当用户按下"Home"键或者系统启动另外一个应用时,前台foreground应用首先切换到Inactive状态,然后切换到Background状态.此转换将会导致先后调用应用代理的 ...

  5. unity3d Find()使用

    1. Hierarchy 创建对象如两个cube时,未修改名称,名称都为cube时. js添加至Camera: private var cubeObj : GameObject; //private ...

  6. <display>标签的几个属性

    <display>这个标签个人觉得挺强大的,但是用不好的话就会成为个累赘,下面给大家分享一下他的几个属性. none:表示此元素不会被显示. block:此元素将显示为块元素,前后会换行. ...

  7. (转)jquery.validate.js 的 remote 后台验证

    之前已经有一篇关于jquery.validate.js验证的文章,还不太理解的可以先看看:jQuery Validate 表单验证(这篇文章只是介绍了一下如何实现前台验证,并没有涉及后台验证remot ...

  8. XP系统取消开机硬件检查

    非正常关机后进行磁盘检查,主要用于检查磁盘错误等.非法关机后会丢失一些文件或产生一些文件错误,而硬盘自检恰恰就是来检查这些错误并对之进行必要的修复,此功能如果被关闭,不能进行必要的数据恢复,久而久之会 ...

  9. oracle sql语句中使用if逻辑

    l在 SQL 语句中使用IF-THEN-ELSE 逻辑 l l使用两种方法: •CASE 表达式:SQL99的语法,类似Basic,比较繁琐 •DECODE 函数:Oracle自己的语法,类似Java ...

  10. [转]C++ list 类学习笔记

    双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list和vector一样,是两个常被使用的容器.和vector不一样的是,list不支持对元素 ...