Cod sursa(job #918133)

Utilizator costyv87Vlad Costin costyv87 Data 18 martie 2013 17:32:17
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.96 kb
//HighFlow
#include <vector>
#include <queue>
#include <fstream>
#define NN 620
#define INF (1<<30)
using namespace std;
ifstream f("cmcm.in");
ofstream g("cmcm.out");


struct cp{int f,z,ind;
            cp() {};
            cp(int A,int C,int D)
            {f=A; z=C; ind=D; };};
int Dist[NN],Q[NN],R[NN],TT[NN];
bool in_q[NN];
queue <int> q;
int N,MM,E;
int flux,ans,sum;
cp a[NN][NN];
vector <int> M[620];
int st,dr;

inline void fa_muchie(int x,int y,int cost,int ind)
{
    a[x][y]=cp(1,cost,ind);
    a[y][x]=cp(0,-cost,-1);
    M[x].push_back(y);
    M[y].push_back(x);
}

void read()
{
    int i,x,y,z;
    f>>N>>MM>>E;

    st=1; dr=N+MM+2;
    for (i=1;i<=E;i++)
    {
        f>>x>>y>>z;
        fa_muchie(x+1,y+1+N,z,i);
    }
    for (i=1;i<=N;i++)
        fa_muchie(1,i+1,0,-1);
    for (i=1;i<=MM;i++)
        fa_muchie(i+1+N,dr,0,-1);
}

int bellman()
{
    int i,x,mn,it,j;


    for (i=1;i<=dr;i++)
        Dist[i]=INF;


    Dist[st]=0;
    q.push(st);

    while (!q.empty())
    {
        x=q.front();
        in_q[x]=false;
        q.pop();

        for (j=0;j<M[x].size();j++)
        {
            it=M[x][j];
            if (a[x][it].f && Dist[x]+a[x][it].z<Dist[it])
            {
                Dist[it]=Dist[x]+a[x][it].z ;
                TT[it]=x;
                if (!in_q[it])
                {
                    in_q[it]=true;
                    q.push(it);
                }
            }
        }
    }

    if (Dist[dr]==INF) return 0;

    for (x=dr;x!=st;x=TT[x])
    {
        a[TT[x]][x].f-=1;
        a[x][TT[x]].f+=1;
    }
    ans+=Dist[dr];
    flux+=1;
    return 1;
}

void solve()
{
    int i,j;

    while (bellman());
    g<<flux<<' '<<ans<<'\n';
    for (i=2;i<=N+1;i++)
        for (j=N+2;j<dr;j++)
            if (a[i][j].f==0 && a[i][j].ind>=1)
                g<<a[i][j].ind<<' ';

}



int main()
{
    read();
    solve();

	return 0;
}