Cod sursa(job #794218)

Utilizator costyv87Vlad Costin costyv87 Data 5 octombrie 2012 23:02:52
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.72 kb
#include <cstdio>
#include <vector>
#define nm 1010
#define df(x,y) c[x][y]-fl[x][y]
using namespace std;

struct cp{int x,y;} ax;
int q[nm],fl[nm][nm],c[nm][nm],tt[nm];
bool bf[nm];
short ok[nm];
vector <int> a[nm];
vector <cp> muc;
vector <int> rez;
int n,m,i,j,nd,x,y,z,mn,flow;

FILE *f,*g;


int bfs()
{
    int i,j,nd,x;

    q[0]=q[1]=1;

    for (i=1;i<=n;i++)
        bf[i]=false;
    bf[1]=1;

    for (i=1;i<=q[0];i++)
    {
        nd=q[i];
        if (nd==n)
            continue;
        for (j=0;j<a[nd].size();j++)
        {
            x=a[nd][j];
            if (bf[x] || df(nd,x)==0)
                continue;

            q[++q[0]]=x;
            tt[x]=nd;
            bf[x]=true;
        }

    }

    return bf[n];
}


void bfs2()
{
    int i,j,nd,x;
    q[0]=1;
    q[1]=1;

    for (i=1;i<=n;i++)
        bf[i]=false;
    bf[1]=1;
    ok[1]=1;

    for (i=1;i<=q[0];i++)
    {
        nd=q[i];
        for (j=0;j<a[nd].size();j++)
        {
            x=a[nd][j];
            if (bf[x] || c[nd][x]==0 || df(nd,x)==0)
                continue;
            ok[x]=1;
            q[++q[0]]=x;
            bf[x]=true;
        }
    }

    q[0]=1;
    q[1]=n;

    for (i=1;i<=n;i++)
        bf[i]=false;
    bf[n]=1;
    ok[n]=2;

    for (i=1;i<=q[0];i++)
    {
        nd=q[i];
        for (j=0;j<a[nd].size();j++)
        {
            x=a[nd][j];
            if (bf[x] || c[x][nd]==0 || df(x,nd)==0 )
                continue;
            ok[x]=2;
            q[++q[0]]=x;
            bf[x]=true;
        }
    }
}

int main()
{
    f=fopen("critice.in","r");
    g=fopen("critice.out","w");

    fscanf(f,"%d%d",&n,&m);

    for (i=1;i<=m;i++)
    {
        fscanf(f,"%d%d%d",&x,&y,&z);
        ax.x=x; ax.y=y;
        muc.push_back(ax);
        a[x].push_back(y);
        a[y].push_back(x);
        c[x][y]=z;
        c[y][x]=z;
    }


    for (flow=0;bfs();)
        for (i=0;i<a[n].size();i++)
        {
            nd=a[n][i];
            if (df(nd,n)==0 || !bf[nd])
                continue;

            tt[n]=nd;

            for (nd=n,mn=df(tt[nd],nd);nd!=1;nd=tt[nd])
                mn=min(mn,df(tt[nd],nd));

            for (nd=n;nd!=1;nd=tt[nd])
            {
                fl[tt[nd]][nd]+=mn;
                fl[nd][tt[nd]]-=mn;
            }
            flow+=mn;
        }

    bfs2();

    for (i=0;i<m;i++)
    {
        if ( (ok[muc[i].x]==1 && ok[muc[i].y]==2)
            || (ok[muc[i].x]==2 && ok[muc[i].y]==1))
            rez.push_back(i+1);
    }

    fprintf(g,"%d\n",rez.size());
    for (i=0;i<rez.size();i++)
        fprintf(g,"%d\n",rez[i]);


    fclose(g);
    return 0;
}