• bzoj2324: [ZJOI2011]营救皮卡丘


    比较难受。floyd写挂了。。

    貌似是DAG的最可相交路径覆盖的经典问题

    算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b。然后就转化成了最小不相交路径覆盖问题。

    证明:为了连通两个点,某条路径可能经过其它路径的中间点。比如1->3->4,2->4->5。但是如果两个点a和b是连通的,只不过中间需要经过其它的点,那么可以在这两个点之间加边,那么a就可以直达b,不必经过中点的,那么就转化成了最小不相交路径覆盖。

    然后这题为啥这样搞完就不能用KM做呢(因为带权肯定不是匈牙利了),我觉得是因为有下界,就是每个点必须访问。

    还是有点迷。。。

    ORZ liu_runda吧

    upd:的的确确是经典问题。传递闭包的时候要保证k<=i||k<=j,否则是不合法的(它是个无向图要转成有向)

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    struct node
    {
        int x,y,next,other;
        LL c,d;
    }a[2100000];int len,last[2100000];
    int inf=1<<29;
    
    void ins(int x,int y,LL c,LL d)
    {
        int k1,k2;
        k1=++len;
        a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
        a[len].next=last[x];last[x]=len;
        
        k2=++len;
        a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
        a[len].next=last[y];last[y]=len;
        
        a[k1].other=k2;
        a[k2].other=k1;
    }
    
    int st,ed;LL ans;
    LL list[210000];bool v[210000];
    int pre[210000];LL cc[210000],d[210000];
    bool SPFA()
    {
        for(int i=0;i<=ed;i++)d[i]=inf;
        d[st]=0;
        memset(v,false,sizeof(v));v[st]=true;
        memset(cc,0,sizeof(cc));cc[st]=inf;
        int head=1,tail=2;list[1]=st;
        while(head!=tail)
        {
            int x=list[head];
            for(int k=last[x];k;k=a[k].next)
            {
                int y=a[k].y;
                if(a[k].c>0&&d[y]>d[x]+a[k].d)
                {
                    d[y]=d[x]+a[k].d;
                    pre[y]=k;
                    cc[y]=min(cc[x],a[k].c);
                    if(v[y]==false)
                    {
                        v[y]=true;
                        list[tail++]=y;
                    }
                }
            }
            head++;
            v[x]=false;
        }
        if(d[ed]==inf)return false;
        ans+=d[ed]*cc[ed];
        int x=ed;
        while(x!=0)
        {
            int k=pre[x];
            a[k].c-=cc[ed];a[a[k].other].c+=cc[ed];
            x=a[k].x;
        }
        return true;
    }
    int mp[210][210];
    int main()
    {
        int n,m,K;
        scanf("%d%d%d",&n,&m,&K);st=n*2+2;ed=n*2+3;
        len=0;memset(last,0,sizeof(last));
        for(int i=1;i<=n;i++)mp[i][i]=0;
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i!=j)mp[i][j]=inf;
        for(int i=1;i<=m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            mp[x][y]=mp[y][x]=min(mp[x][y],c);
        }
        
        for(int k=0;k<=n;k++)
            for(int i=0;i<=n;i++)
                for(int j=0;j<=n;j++)
                    if(k<=i||k<=j)
                        mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
                        
        for(int i=1;i<=n;i++)
        {
            ins(st,i+n+1,1,0);
            ins(i,ed,1,0);
        }
        ins(st,n+1,K,0);
        for(int i=0;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                if(mp[i][j]!=inf)
                    ins(i+n+1,j,1,mp[i][j]);
        ans=0;
        while(SPFA()==true);
        printf("%lld
    ",ans);            
        return 0;
    }
  • 相关阅读:
    ubuntu 11.04 Gnome 恢复默认的任务栏面板
    (转载)学习腾讯的产品管理之道
    (转载)项目管理之外谈项目管理
    windows 下键盘映射
    该留意的文章
    一些常用的工具
    ubuntu 11.04 old sources.list
    一个css3流程导图
    echarts雷达图
    highcharts图表
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/8662137.html
Copyright © 2020-2023  润新知