Cod sursa(job #3228081)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 5 mai 2024 14:51:34
Problema Cuplaj maxim de cost minim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.07 kb
#include <bits/stdc++.h>
using namespace std;
#ifndef HOME
    ifstream in("cmcm.in");
    ofstream out("cmcm.out");
    #define cin in
    #define cout out
#endif
vector <int> v[351];
int cap[351][351], cost[351][351], n, fakedist[351], s = 1, d;
bool incoada[351];
int realdist[351];
int daddy[351];
int cst, flux;
map <pair<int, int>, int> indec;
bool dijkstra()
{
    int i;
    for(i=1; i<=n; i++)
    {
        fakedist[i]=realdist[i];
        realdist[i]=1e9;
    }
    realdist[s]=0;
    priority_queue <pair<int,int> > pq;
    pq.push({0,s});
    int flow=1e9;
    while(!pq.empty())
    {
        int x=pq.top().second;
        pq.pop();
        if(incoada[x])
            continue;
        incoada[x] = 1;
        for(auto it:v[x])
            if(cap[x][it]>0&&realdist[it]>realdist[x]+cost[x][it]+fakedist[x]-fakedist[it])
            {
                realdist[it]=realdist[x]+cost[x][it]+fakedist[x]-fakedist[it];
                daddy[it]=x;
                pq.push({-realdist[it],it});
            }
    }
    for(int i = 1; i <= n; i++)
        incoada[i] = 0;
    if(realdist[d]==1e9)
        return 0;
    for(int i = 1; i <= n; i++)
        realdist[i] += fakedist[i];
    for(i=d; i!=s; i=daddy[i])
        flow=min(flow,cap[daddy[i]][i]);
    for(i=d; i!=s; i=daddy[i])
    {
        cap[daddy[i]][i]-=flow;
        cap[i][daddy[i]]+=flow;
    }
    flux += flow;
    cst += flow*realdist[d];
    return 1;
}
void bellman()
{
    queue <int> q;
    for(int i=1; i<=n; i++)
        realdist[i]=1e9;
    realdist[s]=0;
    q.push(s);
    incoada[s]=1;
    while(!q.empty())
    {
        int x=q.front();
        incoada[x]=0;
        q.pop();
        for(auto it:v[x])
            if(cap[x][it]&&realdist[it]>realdist[x]+cost[x][it])
            {
                realdist[it]=realdist[x]+cost[x][it];
                if(!incoada[it])
                {
                    incoada[it]=1;
                    q.push(it);
                }
            }
    }
}
int main()
{
#ifdef HOME
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    int m,i,a,b,c,e;
    cin>>n>>m>>e;
    d = 2 + n + m;
    for(int i = 1; i <= n; i++)
    {
        v[1].push_back(1 + i);
        v[1 + i].push_back(1);
        cap[1][1 + i] = 1;
    }
    for(i=1; i<=e; i++)
    {
        cin>>a>>b>>c;
        indec[{a, b}] = i;
        a = 1 + a;
        b = 1 + n + b;
        cost[a][b]=c;
        cost[b][a]=-c;
        cap[a][b]=1;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for(int i = 1; i <= m; i++)
    {
        v[d].push_back(1 + n + i);
        v[1 + n + i].push_back(d);
        cap[1 + n + i][d] = 1;
    }
    int cn = n;
    n = 2 + n + m;
    bellman();
    pair <int,int> x;
    while(dijkstra()){}
    cout << flux << " " << cst << '\n';
    for(int i = 2; i <= cn + 1; i++)
        for(auto it:v[i])
            if(cap[i][it] == 0 && 1 + cn + 1 <= it && it <= 1 + cn + m)
                cout << indec[{i - 1, it - 1 - cn}] << " ";
    return 0;
}