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. [转] Gradle中的buildScript代码块

    PS: 在build script中的task apply plugin: 'spring-boot' 需要 classpath("org.springframework.boot:spri ...

  2. [转] 在 Linux 中怎样使用cp命令合并目录树

    PS:通过cp -r --link a/* b/* merged 硬链接不需要复制 怎样将两个布局相似的目录树合并成一个新的目录树?为理解该问题让我们思考下面的例子. 假设 dir1 和 dir2 目 ...

  3. UVA 11770 Lighting Away

    RunID User Problem Result Memory Time Language Length Submit Time 2482977 zhyfzy J Accepted 0 KB 138 ...

  4. PullToRefresh下拉刷新 加载更多 详解 +示例

    常用设置 项目地址:https://github.com/chrisbanes/Android-PullToRefresh a. 设置刷新模式 如果Mode设置成Mode.PULL_FROM_STAR ...

  5. POJ2914

    POJ2914 无向图的最小割 题意:给你一个无向图,然后去掉其中的n条边,使之形成两个连通分量,也即原无向图不连通,求n的最小值. 输入: m(无向图点集),n(无向图边集) a,b,c(a,b两点 ...

  6. Javascript 追本溯源

    一直以来对Javascript的继承关系都是通过死记硬背下来的,对于一个理科生,喜欢逻辑思维的人来讲,死记硬背特别头痛,且理科生对于能够死记硬背下来的东西也很容易忘记,不知道其他理科生童鞋们是否如此, ...

  7. SQL从入门到基础 - 01 数据库开发及ADO.Net

    一.数据库概述 1. 用自定义文件格式保存数据的劣势:并发性差,查找数据的速度差. 2. DBMS(DataBase Management System数据库管理系统)和数据库.平时谈到“数据库”的含 ...

  8. HTML入门学习

    html 基本结构    <!DOCTYPE html> ----------------声明文档的解析类型, 避免浏览器的怪异模式<html> --------------- ...

  9. 准备开发一个基于canvas的图表库,记录一些东西(一)

    开源的图表库已经有很多了,这里从头写个自己的,主要还是 提高自己js的水平,增加复杂代码组织的经验 首先写一个画图的库,供以后画图表使用.经过2天的开发,算是能拿出点东西了,虽然功能还很弱,但是有了一 ...

  10. phpcms v9联动菜单的调用方法_详解get_linkage函数

    phpcms v9联动菜单调用方法[此为内容页调用方法]: {get_linkage($areaid,1,' >> ',1)} 显示效果: phpcms吧 >> 模板下载 &g ...