Cod sursa(job #1709398)

Utilizator cojocarugabiReality cojocarugabi Data 28 mai 2016 12:05:21
Problema Tribut Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.99 kb
# include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define db long double
# define scn(x) scanf("%I64d",&x)
# define scan(x) scanf("%d",&x)
# define print(x) printf("%d ",x)
# define prnt(x) printf("%I64d ",x);
# define eol printf("\n")
# define IOS ios_base :: sync_with_stdio(0)
# define pe "Possible"
# define ie "Impossible"
# define halt(...) {fo << (__VA_ARGS__) << '\n';exit(0);}
# define rep(n) for (int qwerty = 1;qwerty <= n;++qwerty)
# define pp(n) cerr << #n << " = " << n << '\n'
# define ppp(v) for (auto it : v) cerr << it << ' ';cerr << '\n'
const int mod = 1e9 + 7;
int F[1 << 9][1 << 9];
int d[1 << 9];
int bfs(int n)
{
    d[0] = 0;
    for (int i = 1;i <= n;++i) d[i] = -1;
    queue < int > Q;
    Q.push(0);
    while (!Q.empty() && d[n] == -1)
    {
        int node = Q.front();
        Q.pop();
        for (int i = 0;i <= n;++i)
            if (d[i] == -1 && F[node][i])
                Q.push(i),d[i] = node;
    }
    if (d[n] == -1) return 0;
    int flow = 1e9;
    for (int vertex = n;vertex;vertex = d[vertex])
        flow = min(flow,F[d[vertex]][vertex]);
    for (int vertex = n;vertex;vertex = d[vertex])
        F[d[vertex]][vertex] -= flow,F[vertex][d[vertex]] += flow;
    return flow;
}
int main(void)
{
    int t;
    ifstream fi("tribut.in");
    ofstream fo("tribut.out");
    fi>>t;
    while (t --)
    {
        int n,m;
        fi>>n>>m;
        memset(F,0,sizeof(F));
        for (int i = 1;i <= n;++i) fi>>F[0][i];
        for (int i = 1;i <= m;++i)
        {
            int cnt,K;
            fi>>cnt>>K;
            while (cnt --)
            {
                int val;
                fi>>val;
                F[val][n+i] = 1e7;
            }
            F[i+n][n+m+1] = K;
        }
        int flow = 0,cnt = 0;
        while ((cnt = bfs(n+m+1)) != 0) flow += cnt;
        fo << flow << '\n';
    }
    return 0;
}