吐了,Dinic又不过,路过巨神帮忙看看吧
查看原帖
吐了,Dinic又不过,路过巨神帮忙看看吧
519384
Link_Cut_Y楼主2022/5/3 15:39

36pts已阵亡

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 150 + 270 + 10, M = (150 * 270 + 150 + 270) << 1;
const int INF = 1E9;

int h[N], e[M], ne[M], f[M], idx;
int d[N], cur[N];
int n, m, S, T;

void add(int a, int b, int c)
{
    e[ ++ idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx;
}

bool bfs()
{
    queue<int> q;
    memset(d, -1, sizeof d);
    q.push(S), d[S] = 0, cur[S] = h[S];
    
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        
        for (int i = h[t]; i; i = ne[i])
        {
            int ver = e[i];
            if (d[ver] == -1 && f[i])
            {
                d[ver] = d[t] + 1;
                cur[ver] = h[ver];
                if (ver == T) return true;
                q.push(ver);
            }
        }
    }
    
    return false;
}

int find(int u, int limit)
{
    if (u == T) return limit;
    
    int flow = 0;
    
    for (int i = cur[u]; i && flow < limit; i = ne[i])
    {
        cur[u] = i;
        int ver = e[i];
        
        if (d[ver] == d[u] + 1 && f[i])
        {
            int t = find(ver, min(limit - flow, f[i]));
            if (!t) d[ver] = -1;
            f[i] -= t, f[i ^ 1] += t, flow += t;
        }
    }
    
    return flow;
}

int dinic()
{
    int res = 0, flow;
    while (bfs()) while (flow = find(S, INF)) res += flow;
    return res;
}

int main()
{
    scanf("%d%d", &m, &n);
    
    S = 0, T = n + m + 1;
    
    int cnt = 0;
    for (int i = 1; i <= m; i ++ )
    {
        int a;
        cin >> a;
        add(S, i, a), add(i, S, 0);
        cnt += a;
    }
    
    for (int i = 1; i <= n; i ++ )
    {
        int a;
        cin >> a;
        add(i + m, T, a), add(T, i + m, 0);
    }
    
    for (int i = 1; i <= m; i ++ )
        for (int j = m + 1; j <= m + n; j ++ )
            add(i, j, 1), add(j, i, 0);
    
    if (dinic() != cnt) puts("0");
    else
    {
        puts("1");
        for (int i = 1; i <= m; i ++ )
        {
            for (int j = h[i]; j; j = ne[j])
                if (e[j] > m && e[j] <= n + m && !f[j])
                    cout << e[j] - m << ' ';
            cout << endl;
        }
    }
    
    return 0;
}
2022/5/3 15:39
加载中...