Cod sursa(job #1600645)

Utilizator vladrochianVlad Rochian vladrochian Data 15 februarie 2016 11:39:40
Problema Critice Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <cstring>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

const int N_MAX = 1000;
const int M_MAX = 10000;

ifstream fin("critice.in");
ofstream fout("critice.out");

int N, M;
vector<int> al[N_MAX + 5];
pair<int, int> e[M_MAX + 5];

bool use[N_MAX + 5];
int padre[N_MAX + 5];
int cap[N_MAX + 5][N_MAX + 5];

bool access[2][N_MAX + 5];
vector<int> sol;

void Read() {
   fin >> N >> M;
   for (int i = 1; i <= M; ++i) {
      int x, y, c;
      fin >> x >> y >> c;

      al[x].push_back(y);
      al[y].push_back(x);
      cap[x][y] = c;
      cap[y][x] = c;

      e[i] = make_pair(x, y);
   }
}

bool Bfs() {
   memset(use, 0, sizeof use);
   queue<int> q;
   q.push(1);
   use[1] = true;

   while (!q.empty()) {
      int node = q.front();
      q.pop();

      if (node == N)
         continue;

      for (int i : al[node])
         if (!use[i] && cap[node][i]) {
            q.push(i);
            use[i] = true;
            padre[i] = node;
         }
   }

   return use[N];
}

void GetFlow() {
   while (Bfs())
      for (int node : al[N])
         if (use[node] && cap[node][N]) {
            padre[N] = node;
            int flow = 1e9;

            for (int i = N; i != 1; i = padre[i])
               flow = min(flow, cap[padre[i]][i]);

            for (int i = N; i != 1; i = padre[i]) {
               cap[padre[i]][i] -= flow;
               cap[i][padre[i]] += flow;
            }
         }
}

void Dfs(bool use[], int node) {
   use[node] = true;
   for (int i : al[node])
      if (!use[i] && cap[node][i])
         Dfs(use, i);
}

void Check() {
   Dfs(access[0], 1);
   Dfs(access[1], N);

   for (int i = 1; i <= M; ++i) {
      int x = e[i].first;
      int y = e[i].second;
      if ((access[0][x] && access[1][y]) || (access[0][y] && access[1][x]))
         sol.push_back(i);
   }
}

void Print() {
   fout << sol.size() << "\n";
   for (int i : sol)
      fout << i << "\n";
}

int main() {
   Read();
   GetFlow();
   Check();
   Print();
   return 0;
}