Luogu P1828 香甜的黄油 Sweet Butter

Luogu P1828 香甜的黄油 Sweet Butter

枚举+SPFA
没了。。。

#include<cstdio>
#include<cstring>

int n,ans=999999999,len=0,m,k,anx;
struct nod1{int x,y,c,next;}b[100100];
struct nod2{int v,z;}d[100100];
int a[100100],first[100100];

int spfa(int st)
{
    int sum=0;
    int tou=1,wei=2;
    int f[100100];
    f[tou]=st;
    d[st].z=0;
    d[st].v=1;
    while(tou<wei)
    {
        int x=f[tou];
        for(int i=first[x];i;i=b[i].next)
        {
            int y=b[i].y;
            if(d[x].z+b[i].c<d[y].z)
            {
                d[y].z=d[x].z+b[i].c;
                if(d[y].v==0)
                {
                    d[y].v=1;
                    f[wei]=y;
                    wei++;
                }
            }
        }
        tou++;
        d[x].v=0;
    }
    for(int i=1;i<=m;i++)
    {
        sum+=a[i]*d[i].z;
        //printf("%d|",d[i].z);
    }
    //printf("\n");
    return sum;
}

int ins(int x,int y,int c)
{
    len++;
    b[len].x=x;
    b[len].y=y;
    b[len].c=c;
    b[len].next=first[x];
    first[x]=len;
}

int main()
{
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        a[x]++;
    }
    for(int i=1;i<=k;i++)
    {
        int x,y,c;
        scanf("%d %d %d",&x,&y,&c);
        ins(x,y,c);
        ins(y,x,c);
    }
    for(int i=1;i<=m;i++)
    {
        for(int i=1;i<=m;i++)
        {
            d[i].z=999999999;
            d[i].v=0;
        }
        int aa=spfa(i);
        if(ans>aa)ans=aa;
    }
    printf("%d",ans);
}

做的最快的绿题o v o

猜你喜欢

转载自blog.csdn.net/qq_42142540/article/details/80842063