1 条题解

  • 0
    @ 2025-10-8 16:57:24
    #include<bits/stdc++.h>
    using namespace std;
    const int N=110, M=1e6+10, INF=0x3f3f3f3f;
    struct edge{int x, y, f, pre;} a[M]; int alen, last[N], cur[N]; 
    void ins(int x, y, int f)
    {
        alen++; a[alen]=edge{x, y, f, last[x]}; last[x]=alen;
        alen++; a[alen]=edge{y, x, 0, last[y]}; last[y]=alen;
    }
    int n, m, st, ed, h[N];
    bool bfs()
    {
        queue<int> Q; Q.push(st);
        memset(h, 0, sizeof(h)); h[st]=1;
        while(!Q.empty())
        {
            int x=Q.front(); Q.pop();
            for(int k=last[x]; k; k=a[k].pre) if(a[k].f)
            {
                int y=a[k].y;
                if(!h[y])
                {
                    h[y]=h[x]+1;
                    Q.push(y);
                }
            }
        }
        return (h[ed]>0);
    } 
    int dinic(int x, int f)
    {
        if(x==ed) return f; int sx=0;
        for(int k=cur[x]; k; k=a[k].pre) if(a[k].f)
        {
        	cur[x]=k;
            int y=a[k].y;
            if(h[y]==(h[x]+1))
            {
                int sy=dinic(y, min(a[k].f, f-sx));
                a[k].f-=sy; a[k^1].f+=sy;
                sx+=sy; if(sx==f) return f;
            }
        }
        if(!sx) h[x]=0;
        return sx;
    }
    int main()
    {
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            alen=1; memset(last, 0, sizeof(last));
            for(int i=1; i<=m; i++)
            {
                int x, y; scanf(" (%d,%d)", &x, &y); x++; y++;
                if(x==y) continue;
                ins(x+n, y, 2*n); ins(y+n, x, 2*n);
            }
            for(int i=1; i<=n; i++) ins(i, i+n, 1);
            int ans=n, sum, x; 
            for(st=n+1; st<=2*n; st++)
                for(ed=st-n+1; ed<=n; ed++)
                {
                    for (int k=2;k<=alen;k+=2)a[k].f+=a[k^1].f,a[k^1].f=0;
                    sum=0; 
                    while(bfs())
    				{
    					memcpy(cur,last,sizeof(last));
    					sum+=dinic(st, 2*n);
    				}
                    ans=min(ans, sum);
                }
            printf("%d\n", ans);
        }
        return 0;
    }
    
    • 1

    *【最小割】有线电视网络[POJ1966]

    信息

    ID
    1470
    时间
    1000ms
    内存
    64MiB
    难度
    9
    标签
    递交数
    183
    已通过
    19
    上传者