zoukankan      html  css  js  c++  java
  • poj 1966(网络流求最小割集)

    狠狠的复杂度 n*n*n*n*m 。。。 你是在开玩笑嘛。。

    求最小割集的办法,枚举两个不相邻的点分别作为源点和汇点, 看流过的流量为多少, 为了体现一个点只能用一次所有每个点要拆成两个点

    Cable TV Network
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 3424   Accepted: 1593

    Description

    The interconnection of the relays in a cable TV network is bi-directional. The network is connected if there is at least one interconnection path between each pair of relays present in the network. Otherwise the network is disconnected. An empty network or a network with a single relay is considered connected. The safety factor f of a network with n relays is: 
    1. n, if the net remains connected regardless the number of relays removed from the net. 
    2. The minimal number of relays that disconnect the network when removed. 

    For example, consider the nets from figure 1, where the circles mark the relays and the solid lines correspond to interconnection cables. The network (a) is connected regardless the number of relays that are removed and, according to rule (1), f=n=3. The network (b) is disconnected when 0 relays are removed, hence f=0 by rule (2). The network (c) is disconnected when the relays 1 and 2 or 1 and 3 are removed. The safety factor is 2.

    Input

    Write a program that reads several data sets from the standard input and computes the safety factor for the cable networks encoded by the data sets. Each data set starts with two integers: 0<=n<=50,the number of relays in the net, and m, the number of cables in the net. Follow m data pairs (u,v), u < v, where u and v are relay identifiers (integers in the range 0..n-1). The pair (u,v) designates the cable that interconnects the relays u and v. The pairs may occur in any order.Except the (u,v) pairs, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

    Output

    For each data set, the program prints on the standard output, from the beginning of a line, the safety factor of the encoded net.

    Sample Input

    0 0
    1 0
    3 3 (0,1) (0,2) (1,2)
    2 0
    5 7 (0,1) (0,2) (1,3) (1,2) (1,4) (2,3) (3,4)

    Sample Output

    0
    1
    3
    0
    2

    Hint

    The first data set encodes an empty network, the second data set corresponds to a network with a single relay, and the following three data sets encode the nets shown in figure 1.

    Source

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    #define N 60
    #define INF 0x3ffffff
    
    struct node
    {
        int to,next,w;
    }edge[N*N*N];
    
    int cnt,pre[2*N];
    int g[N][N];
    int n,m;
    int s,t;
    int num;
    int mark[N];
    int tt;
    int gap[2*N],lv[2*N];
    
    void dfs(int k)
    {
        num++;
        mark[k]=1;
        for(int i=0;i<n;i++)
        {
            if(mark[i]==1 || g[k][i]==0) continue;
            dfs(i);
        }
    }
    
    void add_edge(int u,int v,int w)
    {
        edge[cnt].to=v;
        edge[cnt].w=w;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    int sdfs(int k,int w)
    {
        if(k==t) return w;
        int f=0;
        int mi=tt;
        for(int p=pre[k];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(edge[p].w)
            {
                if(lv[k] == lv[v]+1)
                {
                    int tmp=sdfs(v,min(edge[p].w ,w-f));
                    f+=tmp;
                    edge[p].w-=tmp;
                    edge[p^1].w+=tmp;
                    if(f==w || lv[s]==tt ) return f;
                }
                if( lv[v]<mi ) mi=lv[v];
            }
        }
        if(f==0)
        {
            gap[lv[k]]--;
            if(gap[ lv[k] ]==0)
            {
                lv[s]=tt;
                return f;
            }
            lv[k]=mi+1;
            gap[lv[k]]++;
        }
        return f;
    }
    
    
    int sap()
    {
        int sum=0;
        tt=2*n;
        memset(gap,0,sizeof(gap));
        memset(lv,0,sizeof(lv));
        gap[0]=tt;
        while(lv[s]<=tt)
        {
            sum += sdfs(s,INF);
        }
        return sum;
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(g,0,sizeof(g));
            for(int i=0;i<m;i++)
            {
                int x,y;
                scanf(" (%d,%d)",&x,&y);
                g[x][y]=g[y][x]=1;
            }
            if(n==0)
            {
                printf("0\n");
                continue;
            }
            /*num=0;
            memset(mark,0,sizeof(mark));
            dfs(0);
            if(num!=n)
            {
                printf("0\n");
                continue;
            }*/
            int mi=INF;
            for(int ss=0;ss<n;ss++)
                for(int i=ss+1;i<n;i++)
                {
                    if( g[ss][i]==1 || ss==i ) continue; // 找两个不相邻的点
                    cnt=0;
                    memset(pre,-1,sizeof(pre));
                    s=2*ss+1;
                    t=2*i;
                    for(int j=0;j<n;j++)
                    {
                        add_edge(2*j,2*j+1,1);
                        add_edge(2*j+1,2*j,0);
                    }
                    for(int i1=0;i1<n;i1++)
                        for(int j=0;j<n;j++)
                        {
                            if(g[i1][j]==1)
                            {
                                add_edge(2*i1+1,2*j,INF);
    
                                add_edge(2*j+1,2*i1,INF);
                            }
                        }
                    mi=min(sap(),mi);
                }
            if(mi==INF) printf("%d\n",n);
            else printf("%d\n",mi);
        }
        return 0;
    }
  • 相关阅读:
    省常中模拟 Test4
    省常中模拟 Test3 Day1
    省常中模拟 Test3 Day2
    省常中模拟 Test1 Day1
    树型动态规划练习总结
    noip2010提高组题解
    noip2003提高组题解
    noip2009提高组题解
    noip2004提高组题解
    noip2002提高组题解
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3077084.html
Copyright © 2011-2022 走看看