Cod sursa(job #1885236)

Utilizator antanaAntonia Boca antana Data 19 februarie 2017 19:13:11
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.24 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <algorithm>

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

using namespace std;

vector< pair<int, int> >edges;
vector <int> G[MAXN];

int n, m, maxflow, source, sink, dad[MAXN], flow[MAXN][MAXN], capacity[MAXN][MAXN];
int a, q[MAXN], ans[MAXM];
char seen1[MAXN], seen2[MAXN];

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

    left = right = 0;
    memset(seen1, 0, sizeof seen1);

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

    return seen1[sink];
}

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

    memset(seen2, 0, sizeof seen2);
    left = right = 0;
    q[right++] = sink;
    seen2[sink] = 2;

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

    return seen2[source];
}

inline void computeMaxFlow(int t, int lt, int f)
{
    int i, node, minflow;
    for(i=0; i<G[t].size(); ++i)
    {
        node = G[t][i];
        if(f == 1) if(!seen1[node]) continue;
        if(f == 2) if(!seen2[node]) continue;
        if(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);
        edges.push_back({x, y});

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

    source = 1;
    sink = n;

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

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

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

    for(i=0; i<m; ++i)
        if((seen1[edges[i].first] && seen2[edges[i].second]) || (seen2[edges[i].first] && seen1[edges[i].second]))
            ans[++a] = i+1;

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

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

    return 0;
}