(2015多校第6场)HDU5361--In Touch (Dijkstra应用)

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;
}

  

时间: 2024-12-25 03:10:40

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

hdu5289||2015多校联合第一场1002贪心+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to

HDU 5352 MZL&#39;s City(2015 多校 第5场,最小费用最大流)

  MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 94 Problem Description MZL is an active girl who has her own country. Her big country has N cities nu

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

http://acm.hdu.edu.cn/showproblem.php?pid=5294 Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the entrance of the tomb while Dumb Zhang's at the end of it. The tomb is made up of many chambers, the total numb

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

总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connect

HDU 5378 Leader in Tree Land(2015 多校第7场 dp)

Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 150    Accepted Submission(s): 54 Problem Description Tree land has  cities, connected by  roads. You can go to any city from

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

Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 965    Accepted Submission(s): 119 Special Judge Problem Description There are m soda and today is their birthday. The 1-st soda has prepa

HDU 5316 Magician(2015多校第三场 线段树)

Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1700    Accepted Submission(s): 503 Problem Description Fantasy magicians usually gain their ability through one of three usual methods

2015多校第7场 HDU 5379 Mahjong tree 构造,DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的标号要是连续的(2)每一颗子树的所有节点标号是连续的.问有多少种标号方案. 解法:对于每一层顶多只能存在2个非叶子节点,否则无解:对于每一层有x个叶子节点,y个非叶子节点,那么ans=(ans * x!)%mod,另外如果y!=0,还得ans=2*ans%mod. #include <bits/st