[POJ 1966] Cable TV Network

[题目链接]

         http://poj.org/problem?id=1966

[算法]

         拆点 + 最小割

[代码]

         

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h>
using namespace std;
const int inf = 2e9;
#define MAXN 55

int i,n,m,s,t,ans,S,T,tot;
int head[MAXN << 1],depth[MAXN << 1],u[MAXN * MAXN],v[MAXN * MAXN];
bool g[MAXN][MAXN];

struct edge
{
        int to,w,nxt;
} e[MAXN * MAXN * 2];

inline void addedge(int u,int v,int w)
{
        tot++;
        e[tot] = (edge){v,w,head[u]};
        head[u] = tot;
        tot++;
        e[tot] = (edge){u,0,head[v]};
        head[v] = tot;
}
inline bool bfs()
{
        int i,l,r,u,v,w;
        static int q[MAXN << 1];
        memset(depth,0,sizeof(depth));
        q[l = r = 1] = S;
        depth[S] = 1;
        while (l <= r)
        {
                u = q[l];
                l++;
                for (i = head[u]; i; i = e[i].nxt)
                {
                        v = e[i].to;
                        w = e[i].w;
                        if (!depth[v] && w)
                        {
                                depth[v] = depth[u] + 1;
                                q[++r] = v;
                                if (v == T) return true;
                        }
                }
        }
        return false;
}
inline int dinic(int u,int flow)
{
        int i,v,w,k,rest = flow;
        if (u == T) return flow;
        for (i = head[u]; i && rest; i = e[i].nxt)
        {
                v = e[i].to;
                w = e[i].w;
                if (depth[v] == depth[u] + 1 && w)
                {
                        k = dinic(v,min(rest,w));
                        if (!k) depth[v] = 0;
                        e[i].w -= k;
                        e[i ^ 1].w += k;
                        rest -= k;
                }
        }
        return flow - rest;
}
inline int getans(int s,int t)
{
        int i,Minimum_Cut,flow;
        tot = 1;
        memset(head,0,sizeof(head));
        for (i = 0; i < n; i++) 
        {
                if (i != s && i != t)
                        addedge(i,i + n,1);
                else addedge(i,i + n,inf);
        }
        for (i = 1; i <= m; i++)
        {
                addedge(u[i] + n,v[i],inf);
                addedge(v[i] + n,u[i],inf);        
        } 
        Minimum_Cut = 0;
        S = s;
        T = t;
        while (bfs())
        {
                while (flow = dinic(S,inf)) 
                        Minimum_Cut += flow;
        }
        return Minimum_Cut;
}

int main() 
{
        
        while (scanf("%d%d",&n,&m) != EOF)
        {
                ans = inf;
                memset(g,false,sizeof(g));
                for (i = 1; i <= m; i++) 
                {
                        scanf(" (%d,%d)",&u[i],&v[i]);
                        g[u[i]][v[i]] = g[v[i]][u[i]] = true;
                }
                for (s = 0; s < n; s++)        
                {
                        for (t = 0; t < n; t++)
                        {
                                if (!g[s][t] && s != t) ans = min(ans,getans(s,t));
                        }
                }
                if (n <= 1 || ans == inf) ans = n;
                printf("%d\n",ans);
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9425494.html