2015 Multi-University Training Contest 6 I(hdu5361 In Touch)

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

题意:给出一行点,每个位置上的点只能移动[ l[ i ] , r[ i ] ]的距离,移动依次花费cost[ i ],问从1到n点最小化费。

思路:其实这个很明显可以看做一个最短路的题目,但还要加一些优化才能过,看了题解才知道Dijkstra+并查集。

         利用并查集,如果一个点被更新过,那么就把它与它之后的最近的一个未更新的点进行merge操作,那么我们就做到了不再访问已更新过的点。同一并查集的集合中最终源头是一个没有访问的点且最右的点。毕竟要知道路径花费全为正,那么同一个点被访问两次必定花费更大,所以没必要。并查集主要就起这个优化。

代码:

来自:https://blog.csdn.net/stay_accept/article/details/51854322

const long long INF=0x3f3f3f3f3f3f3f3f;
typedef pair<long long,int> P;
int V;
int l[500005],r[500005],par[500005];
long long d[500005],w[500005];
void init(int n)
{
    int i;
    for(i=0;i<=2*n;i++)
    {
        par[i]=i;
        d[i]=INF;
    }
}
int find(int x)
{
    if(par[x]==x)
    return par[x];
    return par[x]=find(par[x]);
}
void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y) return;
    par[x]=y;
}
struct edge
{
    int to,cost;
};
void dijkstra(int s)
{
    int i,j,v,le,re;
    priority_queue<P,vector<P>,greater<P> >que;
    d[s]=w[s];                                         //应该再加一个边权,如果用点的距离
    que.push( P(d[s],s) );                     //则不知道应该再往哪走
    while( !que.empty() ) 
    {
        P p=que.top();
        que.pop();
        v=p.second;
        for(i=-1;i<=1;i+=2)
        {
            le=v+l[v]*i;
            re=v+r[v]*i;
            if(le>re) swap(le,re);
            le=max(1,le);
            re=min(V,re);
            for(j=le;j<=re;j++)
            {
                j=find(j);                     //用并查集找出当前点上一个区间最后一个点
                if(j>re) break;
                if(d[j]>d[v]+w[j])
                {
                    d[j]=d[v]+w[j] ;
                    que.push(P(d[j],j)) ;
                }
                unite(j,j+1);
            }
        }
    }
}
int main()
{                                     //因为每个点所连的边的权值都相等,因此每经过
    int i,j,t;                                  //一个点一定不会在经过第二次,所以问题就变为
    scanf("%d",&t);                             //如何在最短路时直接找到没有更新过的点
    while(t--)
    {
        scanf("%d",&V);
        for(i=1;i<=V;i++) scanf("%d",&l[i]);
        for(i=1;i<=V;i++) scanf("%d",&r[i]);
        for(i=1;i<=V;i++) scanf("%I64d",&w[i]);
        init(V);
        dijkstra(1);
        for(i=1;i<V;i++)
        {
            if(d[i]==INF) printf("-1 ");
            else printf("%I64d ",d[i]-w[i]);
        }
        if(d[V]==INF) puts("-1");
        else printf("%I64d\n",d[V]-w[V]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值