Cod sursa(job #1709250)

Utilizator ubb_oprimabuzurile_2016UBB - OprimAbuzurile2016 - Petru Bianca Cosmin ubb_oprimabuzurile_2016 Data 28 mai 2016 11:28:35
Problema Tribut Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.12 kb
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <fstream>
#include <bitset>

using namespace std;

const int maxn = 205;
const int oo = 0x3f3f3f3f;

int n, m, source, sink, cap[maxn][maxn], father[maxn];
vector <int> g[maxn];
bitset <maxn> used;

inline bool bfs() {
  used.reset();
  memset(father, -1, sizeof(father));
  used[source] = 1;
  father[source] = 0;
  queue <int> q;
  q.push(source);
  while(!q.empty()) {
    int node = q.front();
    q.pop();
    if(node == sink)
      continue;
    for(auto it : g[node]) {
      if(cap[node][it] > 0 && !used[it]) {
        used[it] = 1;
        father[it] = node;
        q.push(it);
      }
    }
  }
  return used[sink];
}

inline int getMaxFlow() {
  int maxflow = 0;
  while(bfs()) {
    for(auto it : g[sink]) {
        if(cap[it][sink] > 0 && used[it]) {
          father[sink] = it;
          int bottleneck = oo;
          for(int i = sink ; i != source ; i = father[i]) {
            bottleneck = min(bottleneck, cap[father[i]][i]);
          }
          if(!bottleneck)
            continue;
          maxflow += bottleneck;
          for(int i = sink; i != source ; i = father[i]) {
            cap[father[i]][i] -= bottleneck;
            cap[i][father[i]] += bottleneck;
          }
        }
    }
  }
  return maxflow;
}


int main() {
  ifstream fin("tribut.in");
  ofstream fout("tribut.out");
  int t;
  fin >> t;
  while(t --) {
    fin >> n >> m;
    source = 0;
    sink = n + m + 1;
    for(int i = 1; i <= n; ++ i) {
      fin >> cap[source][i];
      g[source].push_back(i);
      g[i].push_back(source);
      cap[i][source] = 0;
    }
    for(int i = 1; i <= m; ++ i) {
      int p;
      fin >> p >> cap[i + n][sink];
      g[i + n].push_back(sink);
      g[sink].push_back(i + n);
      for(int j = 1; j <= p; ++ j) {
        int x;
        fin >> x;
        g[x].push_back(i + n);
        g[i + n].push_back(x);
        cap[i + n][x] = 0;
        cap[x][i + n] = oo;
      }
    }

    fout << getMaxFlow() << '\n';

    for(int i = source; i <= sink; ++ i)
      vector <int> ().swap(g[i]);
    memset(cap, 0, sizeof(cap));
  }

}