码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5361 In Touch (使用SET集合)

时间:2015-08-10 02:00:56      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:多校

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 l技术分享i技术分享技术分享 and no larger than r技术分享i技术分享技术分享. The cost to use i技术分享-th soda‘s teleporter is c技术分享i技术分享技术分享.

The 1技术分享-st soda is their leader and he wants to know the minimum cost needed to reach i技术分享-th soda (1in)技术分享.
 

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技术分享(1n2×10技术分享5技术分享)技术分享, the number of soda.
The second line contains n技术分享 integers l技术分享1技术分享,l技术分享2技术分享,,l技术分享n技术分享技术分享. The third line contains n技术分享 integers r技术分享1技术分享,r技术分享2技术分享,,r技术分享n技术分享技术分享. The fourth line contains n技术分享 integers c技术分享1技术分享,c技术分享2技术分享,,c技术分享n技术分享技术分享.(0l技术分享i技术分享r技术分享i技术分享n,1c技术分享i技术分享10技术分享9技术分享)技术分享
 

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
 

题意:有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");
    }
}


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

HDU 5361 In Touch (使用SET集合)

标签:多校

原文地址:http://blog.csdn.net/u010372095/article/details/47381753

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!