HDU 5361 In Touch (使用SET集合)

In Touch

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

Total Submission(s): 1184    Accepted Submission(s): 313

Problem Description

There are n soda living in a straight line. soda are numbered by1,2,…,n
from left to right. The distance between two adjacent soda is 1 meter. Every soda has a teleporter. The teleporter ofi-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 integerT,
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

题意:有n个点,排成一行,两个相邻点的距离为1,第 i 个点只能走相隔距离为 L[i]<=dis<=R[i],如果第 i 个点走到 第 j 个点花费为 cost[ i ],现在从1点开始,问能到的其他点 i 的最小花费为多少,不能到的点花费输出-1。

解题:因为每个点走到其他的点花费>=1,所以可以用SET 或 优先队列 按花费最小的点先取出,每个点只走一次。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
const int N = 310000;
#define ll __int64
struct NODE
{
    int id;
    ll cost;
    friend bool operator < (NODE aa , NODE bb)
    {
        if(aa.cost==bb.cost)
            return aa.id<bb.id;
        return aa.cost<bb.cost;
    }
};

set<NODE>node;
set<int>id;
ll C[N];
struct nnn
{
    int l, r , cost;
}man[N];

int main()
{
    int T,n;
    NODE now , pre;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(C,-1,sizeof(C));
        for(int i=1; i<=n; i++)
            scanf("%d",&man[i].l);
        for(int i=1; i<=n; i++)
            scanf("%d",&man[i].r);
        for(int i=1; i<=n; i++)
            scanf("%d",&man[i].cost);

        node.clear();
        id.clear();

        for(int i=2; i<=n; i++)
            id.insert(i);

        C[1]=0;
        now.id=1;
        now.cost=man[1].cost;
        node.insert(now);

        set<int>::iterator it,it1;
        while(!node.empty())
        {
            now=*node.begin();
            node.erase(node.begin());

            int L,R;
            L = now.id + man[now.id].l;
            R = now.id + man[now.id].r;
            it=id.lower_bound( L );
            while(it!=id.end()&&*it<=R){
                pre.id=*it;
                pre.cost= now.cost +  man[ pre.id ].cost;
                node.insert( pre );

                C[pre.id] = now.cost;

                it1 = it++;
                id.erase( it1 );
            }

            L = now.id - man[now.id].r;
            R = now.id - man[now.id].l;
            it = id.lower_bound( L );
            while(it!=id.end()&&*it<=R){
                pre.id=*it;
                pre.cost = now.cost + man[ pre.id ].cost;
                node.insert( pre );

                C[pre.id]=now.cost;

                it1=it++;
                id.erase( it1 );
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(i!=1)printf(" ");
            printf("%I64d",C[i]);
        }
        printf("\n");
    }
}

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

时间: 2024-11-02 14:39:21

HDU 5361 In Touch (使用SET集合)的相关文章

Hdu 5361 In Touch (dijkatrs+优先队列)

题目链接: Hdu 5361  In Touch 题目描述: 有n个传送机排成一排,编号从1到n,每个传送机都可以把自己位置的东西传送到距离自己[l, r]距离的位置,并且花费c,问从1号传送机到其他传送机的最小花费是多少?? 解题思路: 看了题解发现是dijkstra(求最短单源路径)+并查集(优化),刚开始的时候并不知道怎么用并查集优化,然后就提交了一个没有并查集的代码,果断TLE.然后开始搜代码,突然惊叹真是优化的太美妙. 因为每次从队列中取出的都是最优的,所以更新dis的时候可以直接把这

HDU 5361 In Touch (2015 多校6 1009)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:最短路,求源点到所有点的最短距离.但与普通最短路不同的是,给出的边是某点到区间[l,r]内任意点的距离. 输入一个n,代表n个点,输入n个l[i],输入n个r[i],输入n个c[i]. 对于i,表示i到区间[i - r[i]],i - l[i]]和区间[i + l[i],i + r[i]]内的任意点的距离为c[i]. 求1到各个点的最短距离. 思路:若建边跑最短路的话,因为边过多,所以不可行

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

hdu 5361 2015多校联合训练赛#6 最短路

In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 67    Accepted Submission(s): 11 Problem Description There are n soda living in a straight line. soda are numbered by  from left to ri

hdu Keep In Touch

Keep In Touch Accepts: 109 Submissions: 243 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description There are nnn cities numbered with successive integers from 111 to nnn in Byteland. Also, there are mmm

HDOJ 5361 In Touch dijkstra最短路

边数太多,单考虑到一个点到一片点的距离都是一样的. 如果取dist[v]+cost[v]最小的点更新出的最短路肯定是最小的,用一个set维护可以用来更新最短路的点(按dist[v]+cost[v]排序)用另一个set维护需要被更新的点,每当一个点被更新后就将这个点从需要被更新的set中去掉 每个点最多只会被更新一次 In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe

More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 21167    Accepted Submission(s): 7720 Problem Description Mr Wang wants some boys to help him with a project. Because the project

【BZOJ 4547】【HDU 5157】小奇的集合

http://www.lydsy.com/JudgeOnline/problem.php?id=4547 本蒟蒻并不会矩乘求Fibonacci数列前缀和,所以果断分块打表,常数竟然比矩乘要小! PS:今天是长者90岁大寿,+1s #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int p = 10000007; const int bef[1002] =

用 Sencha Touch 构建移动 web 应用程序

Sencha Touch 是一个使用 HTML5.CSS3 和 JavaScript 语言构建的移动 web 应用程序框架,在本文中,学习如何应用您当前的 web 开发技能进行移动 web 开发.下载和建立 Sencha Touch,通过一个样例应用程序探究基本原理.学习开始使用 Sencha Touch 框架所需的一切 2012 年 3 月 19 日 内容 概述 Sencha Touch 准备开始 UI components 结束语 参考资料 评论 概述 在软件开发领域中,有两个重要的趋势越来