Cod sursa(job #2585893)

Utilizator RedXtreme45Catalin RedXtreme45 Data 19 martie 2020 15:06:53
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.74 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define Nmax 1010
#define Mmax 10010
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
int n,m,c[Nmax][Nmax],f[Nmax][Nmax],ap[Nmax],venire[Nmax],min1;
bool ap1[Nmax],ap2[Nmax];
pair<int,int> v[Mmax];
vector <int> G[Nmax],sol;
int BFS()
{
    memset(ap,0,sizeof ap);
    queue <int> q;
    q.push(1);
    ap[1]=1;
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        if (x==n)
            continue;
        for (auto i:G[x])
        {
            if (f[x][i]!=c[x][i] && !ap[i])
            {
                ap[i]=1;
                venire[i]=x;
                q.push(i);
            }
        }
    }
    return ap[n];
}
void BFS1()
{
    queue <int> q;
    q.push(1);
    ap1[1]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(auto i:G[x])
            if(!ap1[i] && f[x][i]<c[x][i])
            {
                ap1[i]=1;
                q.push(i);
            }
    }
}

void BFS2()
{
    queue <int> q;
    q.push(n);
    ap2[n]=1;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(auto i:G[x])
            if(!ap2[i] && -f[x][i]<c[x][i])
            {
                ap2[i]=1;
                q.push(i);
            }
    }
}
int main()
{
    int i,a,j;
    fin>>n>>m;
    for (i=1; i<=m; i++)
    {
        fin>>v[i].first>>v[i].second>>a;
        G[v[i].first].push_back(v[i].second);
        G[v[i].second].push_back(v[i].first);
        c[v[i].first][v[i].second]=c[v[i].second][v[i].first]=a;
    }
    while (BFS())
    {
        for (auto i:G[n])
        {
            if (f[i][n]!=c[i][n] && ap[i])
            {
                venire[n]=i;
                min1=INF;
                j=n;
                while (j!=1)
                {
                    min1=min(min1,c[venire[j]][j]-f[venire[j]][j]);
                    j=venire[j];
                }
                if (min1!=0)
                {
                    j=n;
                    while (j!=1)
                    {
                        f[venire[j]][j]+=min1;
                        f[j][venire[j]]-=min1;
                        j=venire[j];
                    }
                }
            }
        }
    }
    BFS1();
    BFS2();
    for (i=1; i<=m; i++)
    {
        int x1=v[i].first;
        int x2=v[i].second;
        if ((ap1[x1] && !ap1[x2] && !ap2[x1] && ap2[x2]) || (ap2[x1] && !ap2[x2] && !ap1[x1] && ap1[x2]))
        {
            sol.push_back(i);
        }

    }
    fout<<sol.size()<<"\n";
    for (auto i:sol)
        fout<<i<<"\n";
    return 0;
}