poj3436 网络流

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int INF = 0x3f3f3f3f;

int g[105][15], p, n, q[55];
int G[105][105], G_copy[105][105], layer[105], vis[105];

struct node
{
    int x, y, c;
};

bool countLayer(int s, int e)
{
    layer[s] = 0;
    queue<int> q;
    q.push(s);
    memset(vis, 0, sizeof(vis));
    vis[s] = true;
    while (!q.empty())
    {
        int tmp = q.front();
        q.pop();
        for (int i = 1; i <= e; i++)
        {
            if (G[tmp][i] && !vis[i])
            {
                layer[i] = layer[tmp] + 1;
                if (i == e)
                {
                    return true;
                }
                vis[i] = true;
                q.push(i);
            }
        }
    }
    return false;
}

int dinic(int s, int e)
{
    int flow = 0;
    deque<int> q;
    while (countLayer(s, e))
    {
        memset(vis, 0, sizeof(vis));
        vis[s] = true;
        q.push_back(s);
        while (!q.empty())
        {
            int tmp = q.back();
            if (tmp == e)
            {
                int minn = INF;
                int min_index = 0;
                for (int i = 1; i < q.size(); i++)
                {
                    if (G[q[i - 1]][q[i]] && G[q[i - 1]][q[i]] < minn)
                    {
                        minn = G[q[i - 1]][q[i]];
                        min_index = i - 1;
                    }
                }
                for (int i = 1; i < q.size(); i++)
                {
                    G[q[i - 1]][q[i]] -= minn;
                    G[q[i]][q[i - 1]] += minn;
                }
                while (q.size() && q.back() != min_index)
                {
                    vis[q.back()] = false;
                    q.pop_back();
                }
                flow += minn;
            }
            else
            {
                bool flag = false;
                for (int i = 1; i <= e; i++)
                {
                    if (G[tmp][i] && !vis[i] && layer[i] == layer[tmp] + 1)
                    {
                        vis[i] = true;
                        q.push_back(i);
                        flag = true;
                        break;
                    }
                }
                if (!flag && q.size())
                {
                    q.pop_back();
                }
            }
        }
    }
    return flow;
}

int main()
{
    while (cin >> p >> n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> q[i];
            for (int j = 0; j < p; j++)
            {
                cin >> g[i][j];
            }
            for (int j = 0; j < p; j++)
            {
                cin >> g[i + n][j];
            }
        }
        memset(G, 0, sizeof(G));
        for (int i = 2; i < n + 2; i++)
        {
            G[i][i + n] = q[i - 2];
        }
        for (int i = 2; i < n + 2; i++)
        {
            bool flag = true;
            for (int j = 0; j < p; j++)
            {
                if (g[i - 2][j] != 0 && g[i - 2][j] != 2)
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                G[1][i] = INF;
            }
        }
        for (int i = n + 2; i < 2 * n + 2; i++)
        {
            bool flag = true;
            for (int j = 0; j < p; j++)
            {
                if (g[i - 2][j] != 1)
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                G[i][2 * n + 2] = INF;
            }
        }
        for (int i = n + 2; i < 2 * n + 2; i++)
        {
            for (int j = 2; j < n + 2; j++)
            {
                bool flag = true;
                for (int k = 0; k < p; k++)
                {
                    if (!(g[i - 2][k] == g[j - 2][k] || g[j - 2][k] == 2))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    G[i][j] = INF;
                }
            }
        }
        for (int i = 1; i <= 2 * n + 2; i++)
        {
            for (int j = 1; j <= 2 * n + 2; j++)
            {
                G_copy[i][j] = G[i][j];
            }
        }
        cout << dinic(1, 2 * n + 2) << " ";
        int cnt = 0;
        vector<node> v;
        for (int i = n + 2; i < 2 * n + 2; i++)
        {
            for (int j = 2; j < n + 2; j++)
            {
                if (i - n != j && G[i][j] != G_copy[i][j])
                {
                    cnt++;
                    node tmp;
                    tmp.x = i - n - 1;
                    tmp.y = j - 1;
                    tmp.c = G_copy[i][j] - G[i][j];
                    v.push_back(tmp);
                }
            }
        }
        cout << cnt << endl;
        for (int i = 0; i < v.size(); i++)
        {
            cout << v[i].x << " " << v[i].y << " " << v[i].c << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值