Cod sursa(job #2696383)

Utilizator bogdan.gusuleacGusuleac Bogdan bogdan.gusuleac Data 15 ianuarie 2021 19:36:50
Problema Cuplaj maxim de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.58 kb
#include<fstream>
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;

#define MAXSIZE 610

fstream fin("cmcm.in", ios::in);
fstream fout("cmcm.out", ios::out);

int n,m,e,D,S = 1;
vector<int> v[MAXSIZE],w[MAXSIZE];

int anterior[MAXSIZE];
int C[MAXSIZE][MAXSIZE],F[MAXSIZE][MAXSIZE];

int cost[MAXSIZE][MAXSIZE];
int cuplaj[MAXSIZE];

bitset<MAXSIZE> used;

queue<int> cd;
bitset<MAXSIZE> B;
int d[MAXSIZE];

void Bellmanford()
{
    int i,s;
    while(!cd.empty())
    {
        s=cd.front();
        cd.pop();
        B[s]=0;
        for(i=0; i<(int)v[s].size(); i++)
            if(F[s][v[s][i]]<C[s][v[s][i]])
                if(d[v[s][i]]>d[s]+cost[s][v[s][i]])
                {
                    anterior[v[s][i]]=s;
                    d[v[s][i]]=d[s]+cost[s][v[s][i]];
                    if(!B[v[s][i]])
                        cd.push(v[s][i]),B[v[s][i]]=1;
                }
    }
}

int Flux()
{
    for(int i = 1; i <= D; i++)
    {
        d[i] = INT_MAX;
        anterior[i] = 0;
    }

    d[S]=0;
    cd.push(S);
    Bellmanford();

    if(d[D]== INT_MAX)
        return 0;

    int nod=D;
    while(anterior[nod])
    {
        F[anterior[nod]][nod]+=1;
        F[nod][anterior[nod]]-=1;
        nod=anterior[nod];
    }

    return d[D];
}

int main()
{
    fin>>n>>m>>e;
    D = n + m + 2;

    int a,b,c;
    for(int i = 1; i <= e; i++)
    {
        fin>>a>>b>>c;
        a += 1;
        b += n+1;

        if(!used[a])
        {
            v[S].push_back(a);
            v[a].push_back(S);
            w[a].push_back(0);
            used[a]=1;
        }

        if(!used[b])
        {
            v[b].push_back(D);
            v[D].push_back(b);
            w[b].push_back(0);
            used[b]=1;
        }

        v[a].push_back(b);
        v[b].push_back(a);
        w[a].push_back(i);
        w[b].push_back(i);

        C[S][a]=1;
        C[b][D]=1;
        C[a][b]=1;

        cost[a][b]=c;
        cost[b][a]=-c;
    }

    int flux = 0,cost = 0;
    while(cost = Flux())
        flux += cost;

    int cj = 0;
    for(int i = 2; i <= n + 1; i++)
        for(int j = 0; j < (int)v[i].size(); j++)
            if(F[i][v[i][j]] && v[i][j]!=1)
            {
                cuplaj[i - 1] = w[i][j];
                cj++;
                break;
            }

    fout<<cuplaj<<" "<<flux<<"\n";
    for(int i = 1; i <= n; i++)
        if(cuplaj[i])
            fout<<cuplaj[i]<<" ";

    fout<<"\n";

    fin.close();
    fout.close();

    return 0;
}