Cod sursa(job #3251363)

Utilizator not_anduAndu Scheusan not_andu Data 25 octombrie 2024 20:25:37
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.57 kb
#include <bits/stdc++.h>

using namespace std;

#define INFILE "cmcm.in"
#define OUTFILE "cmcm.out"

const int N_MAX = 600;
const int INF = 1e9;

struct Node {

public:
    int node;
    int cost;

    Node(){}
    Node(int _node, int _cost) : node(_node), cost(_cost) {}

    bool operator<(const Node &other) const {
        return this -> cost > other.cost;
    }

};

int n, m, e, destinatie;
int cost;
int dist[N_MAX + 5];
int parent[N_MAX + 5];
priority_queue<Node> q;
unordered_set<int> visitedEdges;
int indice[N_MAX + 5][N_MAX + 5];
bool capacity[N_MAX + 5][N_MAX + 5];
vector<pair<int, int> > adj[N_MAX + 5];

bool dijkstra(){

    memset(dist, 0x3f3f3f3f, sizeof dist);
    dist[1] = 0;

    q.push(Node(1, 0));
    while(!q.empty()){
        int node = q.top().node;
        int cost = q.top().cost;
        q.pop();
        if(cost != dist[node]) continue;
        for(auto &to : adj[node]){
            if(capacity[node][to.first] && dist[node] + to.second < dist[to.first]){
                dist[to.first] = dist[node] + to.second;
                parent[to.first] = node;
                q.push(Node(to.first, dist[to.first]));
            }
        }
    }

    return (dist[destinatie] < INF);

}

void solve(){

    cin >> n >> m >> e;
    destinatie = n + m + 2;
    for(int i = 1; i <= e; ++i){
        int node, to, cost; cin >> node >> to >> cost;
        ++node;
        to += n + 1;
        adj[node].push_back(make_pair(to, cost));
        adj[to].push_back(make_pair(node, -cost));
        indice[node][to] = i;
        indice[to][node] = -i;
        capacity[node][to] = true;
    }

    for(int i = 2; i <= n + 1; ++i){
        adj[1].push_back(make_pair(i, 0));
        adj[i].push_back(make_pair(1, 0));
        capacity[1][i] = true;
    }

    for(int i = n + 2; i < destinatie; ++i){
        adj[i].push_back(make_pair(destinatie, 0));
        adj[destinatie].push_back(make_pair(i, 0));
        capacity[i][destinatie] = true;
    }

    while(dijkstra()){
        for(int i = destinatie; i != 1; i = parent[i]){
            capacity[parent[i]][i] = false;
            capacity[i][parent[i]] = true;
            int aux = indice[parent[i]][i];
            if(aux > 0) visitedEdges.insert(aux);
            else visitedEdges.erase(-aux);
        }
        cost += dist[destinatie];
    }

    cout << visitedEdges.size() << " " << cost << '\n';
    for(auto &to : visitedEdges) cout << to << " ";
    cout << '\n';

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    solve();
    return 0;
}