In Touch (hdu 5361 优先队列的Dij + 并查集优化)

In Touch

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1109    Accepted Submission(s): 298

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

Hint

If you need a larger stack size,
please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.

Source

2015 Multi-University Training Contest 6

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5368 5367 5366 5365 5364

题意:有n个点站成一排,相邻距离为1,每个点 i 可以联系上距离自己 x 的点并且花费Ci,其中Li<=x<=Ri,从点1开始,求联系到每个点的最少费用。

思路:边太多,不可能建完边后再求最短路,感觉有点像隐式图,然后就是巧妙用到Dijstra,需要注意到的就是,这里是每个点有权值而不是边,那么dist[i]表示从1到 i 的花费再加上点 i 的花费,这样每个点就只会被更新一次,更新后在以后就不会再次被更新了,这里用到并查集把已经更新的点得father指向还没被更新的点。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <functional>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef __int64 ll;
using namespace std;

const ll INF = 1LL << 60;   //要大点
#define mod 1000000009
const int maxn = 200010;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

typedef pair<ll,int>Pir;
ll L[maxn],R[maxn],C[maxn],dist[maxn];
int n,father[maxn];

void init()
{
    for (int i=0;i<=n+5;i++)
    {
        father[i]=i;
        dist[i]=INF;
    }
}

int find_father(int x)
{
    if (x!=father[x])
        father[x]=find_father(father[x]);
    return father[x];
}

void solve()
{
    dist[1]=C[1];
    priority_queue<Pir,vector<Pir>,greater<Pir> >Q;
    Q.push(make_pair(dist[1],1));
    while (!Q.empty())
    {
        Pir st=Q.top(); Q.pop();
        int u=st.second;
        for (int i=-1;i<=1;i+=2)
        {
            int l=u+i*L[u];
            int r=u+i*R[u];
            if (l>r) swap(l,r);
            l=max(1,l);
            l=min(l,n+1);
            if (l>r) continue;
            for (int v=l;;v++)
            {
                v=find_father(v);
                if (v<=0||v>n||v>r) break;
                if (dist[v]>dist[u]+C[v])
                {
                    dist[v]=dist[u]+C[v];
                    Q.push(make_pair(dist[v],v));
                }
                father[find_father(v)]=find_father(v+1);
            }
        }
    }
    printf("0");
    for (int i=2;i<=n;i++)
    {
        if (dist[i]>=INF)
            printf(" -1");
        else
            printf(" %I64d",dist[i]-C[i]);
    }
    printf("\n");
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n);
        for (i=1;i<=n;i++)
            scanf("%I64d",&L[i]);
        for (i=1;i<=n;i++)
            scanf("%I64d",&R[i]);
        for (i=1;i<=n;i++)
            scanf("%I64d",&C[i]);
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-02 14:20:38

In Touch (hdu 5361 优先队列的Dij + 并查集优化)的相关文章

HDU 1198 Farm Irrigation (并查集优化,构图)

本题和HDU畅通工程类似,只不过畅通工程给出了数的连通关系, 而此题需要自己判断连通关系,即两个水管是否可以连接到一起,也是本题的难点所在. 记录状态,不断combine(),注意只需要判断左方和上方就行,这样不会重复判断,而且肯定都可以遍历到所有的状态. #include<stdio.h> #include<iostream> #include<string> //记录水管的形状,每种水管用一个由'0'和'1'组成的长度为4的字符串代表, //分别表示上下左右四边是否

HDU 3635 延缓更新的并查集

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2839    Accepted Submission(s): 1097 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

HDU 3407.Zjnu Stadium 加权并查集

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3726    Accepted Submission(s): 1415 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built

HDU 1213 How Many Tables (并查集)

How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1213 Appoint description:  System Crawler  (2015-05-25) Description Today is Ignatius' birthday. He invites a lot of friends. Now

hdu 1232 畅通工程(并查集算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31088    Accepted Submission(s): 16354 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条

HDU 3371 Connect the Cities(并查集+Kruskal)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先联通了几个点,所以为了判断连通性 很容易想到用并查集+kruskal. 不过要注意 这题有一个坑点,就是边数很多 上限是25000,排序的话可能就超时了.而点数则比较少 上限是500,所以很多人选择用Prim做.但我个人觉得这样连通性不好判断.其实边数多没关系,我们主要去重就好啦,用邻接矩阵存下两点

hdu 2545 树上战争(并查集)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545 树上战争 Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 564    Accepted Submission(s): 305 Problem Description 给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,