POJ 1087 网络流 最大流

设置源点和汇点,每个需要使用的设备插座,连接源点到该插座的设备个数,连接会场提供插座到汇点,容量为个数,然后连接插座转换边,容量为无穷

注意转换插座可能是之前没出现过的

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 444;
const int M = 1111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;

struct arclist {
    struct edge{int v, cap, next;} E[N + M << 1];
    int head[N], cur;
    inline void init()
    {
        memset(head, -1, sizeof(head)); cur = 0;
    }
    inline void add(int u, int v, int cap = 1) {
        E[cur].v = v; E[cur].cap = cap;
        E[cur].next = head[u]; head[u] = cur++;
        E[cur].v = u; E[cur].cap = 0;
        E[cur].next = head[v]; head[v] = cur++;
    }
    int SAP(int S, int T, int n)
    {
        int maxflow = 0, pre[N], dis[N] = {}, gap[N] = {};
        int cur[N]; memcpy(cur, head, sizeof(cur));
        gap[0] = n + 1; ++gap[dis[S] = 1];
        for (int u = pre[S] = S; dis[S] <= n; ++gap[++dis[u]], u = pre[u])
        {
            for (bool flag = true; flag;)
            {
                flag = false;
                for (int &p = cur[u]; ~p; p = E[p].next)
                {
                    if (!E[p].cap || dis[u] != dis[E[p].v] + 1) continue;
                    flag = true; pre[E[p].v] = u; u = E[p].v;
                    if (u == T)
                    {
                        int aug = INF;
                        for (int i = S; i != T; i = E[cur[i]].v)
                            if (aug > E[cur[i]].cap)
                            {
                                u = i; aug = E[cur[i]].cap;
                            }
                        for (int i = S; i != T; i = E[cur[i]].v)
                        {
                            E[cur[i]].cap -= aug;
                            E[cur[i] ^ 1].cap += aug;
                        }
                        maxflow += aug;
                    }
                    break;
                }

            }
            if (--gap[dis[u]] == 0) break;
            dis[u] = n;
            for (int p = head[u]; ~p; p = E[p].next)
                if (E[p].cap && dis[u] > dis[E[p].v]) {dis[u] = dis[E[p].v]; cur[u] = p;}
        }
        return maxflow;
    }
}flow;

int to[N], from[N], ct[N], ct1[N];
int trans[N], trant[N];

int main()
{
        string s;
        char str[50];
        int num, n, i, j, k, tot, cnt3, cnt1, cnt2;
        memset(ct, 0, sizeof(ct));
        scanf("%d", &n);
        flow.init();
        cnt1 = cnt2 = cnt3 = 0;
        tot = 1;
        memset(to, 0, sizeof(to));
        memset(ct1, 0, sizeof(ct1));
        map < string , int > index;
        for (i = 0; i < n; ++i)
        {
            int temp;
            scanf("%s", str);
            s = str;
            if (index[s]) temp = index[s];
            else {index[s] = tot++; temp = tot - 1;}
            ct1[temp]++;
        }
        scanf("%d", &n);
        num = n;
        for (i = 0; i < n; ++i)
        {
            int temp;
            char buf[N];
            scanf("%s%s", buf, str);
            s = str;
            if (index[s]) temp = index[s];
            else {index[s] = tot++; temp = tot - 1;}
            ct[temp]++;
        }
        scanf("%d", &n);
        for (i = 0; i < n; ++i)
        {
            int tp1, tp2;
            char buf[N];
            string s1;
            //buf = s1.c_str();
            scanf("%s%s", buf, str);
            s1 = buf; s = str;
            if (index[s1]) tp1 = index[s1];
            else {index[s1] = tot++; tp1 = tot - 1;}
            if (index[s]) tp2 = index[s];
            else {index[s] = tot++; tp2 = tot - 1;}
            trans[cnt3] = tp1; trant[cnt3++] = tp2;
        }
        for (i = 0; i < tot; ++i)
        {
            flow.add(i, tot, ct1[i]);
        }
        for (i = 0; i < tot; ++i)
        {
            if (!ct[i]) continue;
            flow.add(0, i, ct[i]);
        }
        for (i = 0; i < cnt3; ++i)
            flow.add(trans[i], trant[i], INF);
        printf("%d\n", num -  flow.SAP(0, tot, tot + 1));
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值