Cod sursa(job #996897)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 12 septembrie 2013 21:18:52
Problema Critice Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#define NM 1010

using namespace std;

ifstream f("critice.in");
ofstream g("critice.out");

int N, M;
int Cap[NM][NM], Flow[NM][NM], Id[NM][NM];
vector<int> G[NM];
vector<int> ANS;
queue<int> Q;
int T[NM];
bool PathS[NM], PathD[NM];

bool BF (int S, bool Path[])
{
    memset(Path, 0, NM*sizeof(int));
    Path[S]=1;
    Q.push(S);

    int node;
    vector<int>::iterator it;

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

        for (it=G[node].begin(); it!=G[node].end(); ++it)
            if (Path[*it]==0 && Cap[node][*it]>Flow[node][*it])
            {
                T[*it]=node;
                Path[*it]=1;
                Q.push(*it);
            }
    }

    return Path[N];
}

int main ()
{
    f >> N >> M;
    for (int i=1; i<=M; i++)
    {
        int a, b, c;
        f >> a >> b >> c;
        Cap[a][b]=Cap[b][a]=c;
        G[a].push_back(b);
        G[b].push_back(a);
        Id[a][b]=Id[b][a]=i;
    }

    while (BF(1, PathS))
    {
        int FMIN=999999;

        for (int i=N; i!=1; i=T[i])
            FMIN=min(FMIN, Cap[T[i]][i]-Flow[T[i]][i]);

        for (int i=N; i!=1; i=T[i])
        {
            Flow[T[i]][i]+=FMIN;
            Flow[i][T[i]]-=FMIN;
        }
    }

    BF(1, PathS);
    BF(N, PathD);

    for (int i=1; i<=N; i++)
        for (vector<int>::iterator it=G[i].begin(); it!=G[i].end(); ++it)
            if (Flow[i][*it]>0 && Flow[i][*it]==Cap[i][*it] && PathS[i]==1 && PathD[*it]==1)
                ANS.push_back(Id[i][*it]);

    sort(ANS.begin(), ANS.end());

    g << ANS.size() << '\n';
    for (int i=0; i<ANS.size(); i++)
        g << ANS[i] << '\n';

    f.close();
    g.close();

    return 0;
}