Cod sursa(job #776719)

Utilizator visanrVisan Radu visanr Data 10 august 2012 12:00:14
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.43 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;


#define nmax 1010
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define INF 1000000010
#define PI pair<int, int>
int N, M, nr, sol[10 * nmax], node, a, b, c;
short int flow[nmax][nmax], cap[nmax][nmax];
int father[nmax];
PI mc[10 * nmax];
vector<int> G[nmax];
char used[nmax];


int BFS();

int main()
{
    freopen("critice.in", "r", stdin);
    freopen("critice.out", "w", stdout);
    int i, j, val;
    scanf("%i %i", &N, &M);
    for(i = 1; i <= M; i++)
    {
          scanf("%i %i %i", &a, &b, &c);
          cap[a][b] += c; cap[b][a] += c;
          G[a].pb(b); G[b].pb(a);
          mc[i] = mp(a, b);
    }
    while(BFS())
    {
                val = INF;
                for(node = N; father[node] != -1; node = father[node])
                         val = min(val, cap[father[node]][node] - flow[father[node]][node]);
                for(node = N; father[node] != -1; node = father[node])
                {
                         flow[father[node]][node] += val;
                         flow[node][father[node]] -= val;
                }
    }
    for(i = 1; i <= M; i++)
    {
          cap[mc[i].f][mc[i].s] ++;
          cap[mc[i].s][mc[i].f] ++;
          if(BFS()) sol[nr ++] = i;
          cap[mc[i].f][mc[i].s] --;
          cap[mc[i].s][mc[i].f] --;
    }
    printf("%i\n", nr);
    for(i = 0; i < nr; i++) printf("%i\n", sol[i]);
    return 0;
}

int BFS()
{
    queue<int> q;
    q.push(1);
    memset(used, 0, sizeof(used));
    memset(father, -1, sizeof(father));
    used[1] = 1;
    while(!q.empty())
    {
                     int node = q.front();
                     q.pop();
                     for(vector<int> :: iterator it = G[node].begin(); it != G[node].end(); ++ it)
                     {
                                     if(!used[*it] && flow[node][*it] < cap[node][*it])
                                     {
                                                   used[*it] = 1;
                                                   father[*it] = node;
                                                   q.push(*it);
                                                   if(*it == N) return 1;
                                     }
                     }
    }
    return 0;
}