Cod sursa(job #2696106)

Utilizator Harsa_AndreiHarsa Andrei Harsa_Andrei Data 15 ianuarie 2021 12:39:14
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <bits/stdc++.h>
#define MAX 1001
using namespace std;
ifstream in("critice.in");
ofstream out("critice.out");
int N,M,S,F;
vector<int> Graph[MAX];
int capacitate[MAX][MAX];
int flow[MAX][MAX];
int muchii[MAX][MAX];
int tata[MAX];
int bfs()
{
    for(int i=1; i<=N; i++)
        tata[i]=0;
   queue<int> que;
    que.push(S);
    tata[S]=S;
   while(!que.empty())
    {
        int cur = que.front();
        que.pop();
        for(auto next : Graph[cur])
        {
            if(!tata[next] && (capacitate[cur][next]-flow[cur][next]>0))
            {
                tata[next] = cur;
                if(next!=F)
                   que.push(next);
            }
        }
    }
    if(tata[F])
        return 1;

    return 0;
}
int Karp()
{
    int maxflow =0;
    while(bfs())
    {
        for(auto vecin : Graph[F])
            if(tata[vecin] && capacitate[vecin][F]-flow[vecin][F] >0)
            {
                int currentFlow = 2e9;
                tata[F] = vecin;
                int current = F;
                int previous;
                while(current!=S)
                {
                    previous = tata[current];
                    currentFlow = min(currentFlow, capacitate[previous][current]-flow[previous][current]);
                    current = previous;
                }
                current=F;
                if(currentFlow>0)
                    while(current!=S)
                    {
                        previous = tata[current];
                        flow[previous][current]+=currentFlow;
                        flow[current][previous]-=currentFlow;
                        current = previous;
                    }
                maxflow+=currentFlow;
            }
    }
    return maxflow;
}
int main()
{
    int x,y,z;
    in>>N>>M;
    S=1;
    F=N;
    for(int i=1; i<=M; i++)
    {
        in>>x>>y>>z;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
        capacitate[x][y]=capacitate[y][x]= z;
        muchii[x][y]=muchii[y][x]=i;
    }
    Karp();
    set<int> rez;
    for(int i=1; i<=N; i++)
        if(tata[i])
            for(auto j : Graph[i])
                if(!tata[j] && capacitate[i][j]-flow[i][j]==0 )
                    rez.insert(muchii[i][j]);
    out<<rez.size()<<'\n';
    for(auto temp: rez)
        out<<temp<<'\n';

    return 0;
}