Cod sursa(job #1885200)

Utilizator antanaAntonia Boca antana Data 19 februarie 2017 18:41:26
Problema Critice Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.3 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>

#define MAXN 1001
#define MAXM 10001
#define INF 0x3f3f3f3f

using namespace std;

vector <int> G[MAXN];

int n, m, maxflow, source, sink, dad[MAXN], flow[MAXN][MAXN], capacity[MAXN][MAXN];
int a, k, r, q[MAXN], idx[MAXN][MAXN], left_t[MAXN], right_t[MAXN], ans[MAXM];
bool seen[MAXN];

inline bool firstBFS()
{
    int left, right, son, node, i, nr;

    k = 0;
    left = right = 0;
    memset(seen, 0, sizeof seen);

    q[right++] = source;
    seen[source] = 1;
    while(left < right)
    {
        node = q[left++];
        nr = 0;
        if(node == sink) continue;
        for(i=0; i<G[node].size(); ++i)
        {
            son = G[node][i];
            if(!seen[son] && flow[node][son] < capacity[node][son])
            {
                nr++;
                seen[son] = 1;
                dad[son] = node;
                q[right++] = son;
            }
        }
        if(!nr) left_t[++k] = node;
    }

    return seen[sink];
}

inline bool secondBFS()
{
    int left, right, i, node, son, nr;

    r = 0;
    memset(seen, 0, sizeof seen);
    left = right = 0;
    q[right++] = sink;
    seen[sink] = 1;

    while(left < right)
    {
        node = q[left++];
        nr = 0;
        if(node == source) continue;
        for(i=0; i<G[node].size(); ++i)
        {
            son = G[node][i];
            if(!seen[son] && flow[node][son] < capacity[node][son])
            {
                nr ++;
                q[right++] = son;
                dad[son] = node;
                seen[son] = 1;
            }
        }
        if(!nr) right_t[++r] = node;
    }

    return seen[source];
}

inline void computeMaxFlow(int t, int lt)
{
    int i, node, minflow;
    for(i=0; i<G[t].size(); ++i)
    {
        node = G[t][i];
        if(seen[node] && flow[node][t] < capacity[node][t])
        {
            minflow = INF;
            dad[t] = node;
            for(node=t; node!=lt; node=dad[node])
                minflow = min(minflow, capacity[dad[node]][node] - flow[dad[node]][node]);
            if(minflow != 0)
                for(node=t; node!=lt; node=dad[node])
                {
                    flow[dad[node]][node] += minflow;
                    flow[node][dad[node]] -= minflow;
                }
            maxflow += minflow;
        }
    }
}

int main()
{
    freopen("critice.in", "r", stdin);
    freopen("critice.out", "w", stdout);

    int j, i, x, y, c;

    scanf("%d%d", &n, &m);

    for(i=1; i<=m; ++i)
    {
        scanf("%d%d%d", &x, &y, &c);

        G[x].push_back(y);
        G[y].push_back(x);

        capacity[x][y] = c;
        capacity[y][x] = c;
        idx[x][y] = idx[y][x] = i;
    }

    source = 1;
    sink = n;

    while(firstBFS()) computeMaxFlow(sink, source);

    for(i=1; i<=n; ++i)
        for(j=1; j<=n; ++j)
            flow[i][j] = 0;

    while(secondBFS()) computeMaxFlow(source, sink);

    for(i=1; i<=k; ++i)
        for(j=1; j<=r; ++j)
            if(idx[left_t[i]][right_t[j]])
                ans[++a] = idx[left_t[i]][right_t[j]];

    printf("%d\n", a);

    sort(ans+1, ans+a+1);

    for(i=1; i<=a; ++i)
        printf("%d\n", ans[i]);

    return 0;
}