poj 1062 昂贵的聘礼 最短路

spfa

由于地位的限制

所以松弛的条件改成价格比原价小既能更新

每个节点需要保存最大地位与最小地位

 

#include <stdio.h>

int m,n;
struct node
{
    int e;
    int mon;
}a[101][101];
int d1[101];
short queue[1000001];
int front,end;

struct note
{
    int sum;
    int low;
    int high;
    int e;
}ans[700001];

int min(int a,int b)
{
    if(a<=b) return(a);
    return(b);
}
int max(int a,int b)
{
    if(a>=b) return(a);
    return(b);
}

int search()
{
//    printf("%d\n",end);
    int t=queue[front];
    int i,j,k,tmp;
    int ty=ans[t].e;
        for(i=1;i<=d1[ty];i++)
        if(ans[t].high-ans[a[ty][i].e].low<=m&&ans[a[ty][i].e].high-ans[t].low<=m)
        {
//            printf("hi");
            tmp=a[ty][i].e;
            if(ans[t].sum+a[ty][i].mon<ans[tmp].sum)
            {
//                printf("%d %d\n",tmp,ans[t].sum+a[ty][i].mon);
                end++;
                ans[end].sum=ans[t].sum+a[ty][i].mon;
                ans[end].high=max(ans[t].high,ans[tmp].high);
                ans[end].low=min(ans[t].low,ans[tmp].low);
                ans[end].e=tmp;
                queue[end]=end;
            }

    }
    front++;
    return(0);
}

 


int main()
{
    int i,j,k;
    scanf("%d %d",&m,&n);


    int t1,t2,t3;
    int w1,w2;
    for(i=1;i<=n;i++)
    {
        scanf("%d %d %d",&t1,&t2,&t3);
        ans[i].sum=t1;
        ans[i].low=t2;
        ans[i].high=t2;
        ans[i].e=i;
        for(j=1;j<=t3;j++)
        {

            scanf("%d %d",&w1,&w2);
            d1[w1]++;
            a[w1][d1[w1]].e=i;
            a[w1][d1[w1]].mon=w2;

        }
    }


    for(i=1;i<=n;i++)
    queue[i]=i;


    end=n,front=1;
    while(front<=end)
    search();

 

 

//    for(i=1;i<=d2[1];i++)
//    printf("%d ",ans[1][i].sum);

    int answer=99999999;

//    for(i=1;i<=d2[3];i++)
//    printf("%d ",ans[3][i].sum);


    for(i=1;i<=end;i++)
    if(ans[i].e==1)
    if(ans[i].sum<answer)
    answer=ans[i].sum;

    printf("%d\n",answer);

    return 0;
}

你可能感兴趣的:(poj 1062 昂贵的聘礼 最短路)