hdu5361

In Touch

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 482    Accepted Submission(s): 138


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  (1in)
 

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   (1n2×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 (0lirin,1ci109)
 

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
 


题目大意:有n个站点  每个站点只能联系距离为   l   -   r   内的站点,所需要的费用为cost 求第一个站点到

                   剩下所有站点需要的最小费用

代码:

/*踏实!!努力!!*/
/*
   在区间之上的最短路!!!
   刚开始做的时候,想到暴力肯定会差超时,没想到用set
   
   思路:利用set对区间内所有的cost值进行排序之后,同时利用set
   可除去区间内已经存在的最短路,这样极大的缩小了时间,暴力也是
   需要好的容器更加快捷
*/
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<set>
#include<stack>
using namespace std;
#define ll long long
#define N 200005
int lp[N],rp[N],n,data[N];
ll dis[N];
struct Node{
    int id;
    ll cost;
};
set<Node>findd;
set<int>num;
bool operator < (Node a,Node b)
{
    if(a.cost==b.cost) return a.id<b.id;
    return a.cost<b.cost;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&lp[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&rp[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&data[i]);
        findd.clear();
        num.clear();
        memset(dis,-1,sizeof(dis));

        Node x,y;
        x.id=1;
        x.cost=data[1];
        findd.insert(x);
        dis[1]=0;
        for(int i=2;i<=n;i++)
            num.insert(i);

        set<int>::iterator it,it2;
        while(findd.size()>0){
            x=*findd.begin();
            findd.erase(findd.begin());

            //从*it.id的左侧开始查找
            it=num.lower_bound(x.id-rp[x.id]);
            while(it!=num.end()&&*it<=x.id-lp[x.id]){
                y.id=*it;
                y.cost=x.cost+data[y.id];
                findd.insert(y);
                dis[y.id]=x.cost;
                it2=it++;
                num.erase(it2);
            }
            //从*it.id的右侧开始查找
            it=num.lower_bound(x.id+lp[x.id]);
            while(it!=num.end()&&*it<=x.id+rp[x.id]){
                y.id=*it;
                y.cost=x.cost+data[y.id];
                findd.insert(y);
                dis[y.id]=x.cost;
                it2=it++;
                num.erase(it2);
            }
        }
        printf("%d",dis[1]);
        for(int i=2;i<=n;i++)
            printf(" %lld",dis[i]);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值