Cod sursa(job #3188163)

Utilizator MateitzaMatei Dinu Mateitza Data 2 ianuarie 2024 00:14:12
Problema Cc Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

ifstream inputFile("cc.in");
ofstream outputFile("cc.out");

const int N = 351;
const int inf = 125000000;
vector<int> graph[N];
queue<int> bfsQueue;
int capacity[N][N], flow[N][N], distanceFromSource[N], parent[N], cost[N][N];
int source, destination, numberOfNodes;
bool visited[N];

bool bfs(int start) {
    for (int i = 1; i <= 2 * numberOfNodes + 1; i++) {
        visited[i] = parent[i] = 0;
        distanceFromSource[i] = inf;
    }
    distanceFromSource[start] = 0;
    visited[start] = 1;
    bfsQueue.push(start);

    while (!bfsQueue.empty()) {
        int currentNode = bfsQueue.front();
        bfsQueue.pop();
        visited[currentNode] = 0;

        for (int i = 0; i < graph[currentNode].size(); i++) {
            int neighborNode = graph[currentNode][i];

            if (capacity[currentNode][neighborNode] - flow[currentNode][neighborNode] > 0) {
                if (distanceFromSource[currentNode] + cost[currentNode][neighborNode] < distanceFromSource[neighborNode]) {
                    distanceFromSource[neighborNode] = distanceFromSource[currentNode] + cost[currentNode][neighborNode];
                    parent[neighborNode] = currentNode;

                    if (!visited[neighborNode]) {
                        bfsQueue.push(neighborNode);
                        visited[neighborNode] = 1;
                    }
                }
            }
        }
    }
    return distanceFromSource[destination] != inf;
}

int main() {
    int m, x, y, c, z, totalCost = 0, i;
    inputFile >> numberOfNodes;

    for (i = 1; i <= numberOfNodes; i++)
        for (int j = 1; j <= numberOfNodes; j++) {
            inputFile >> cost[i][numberOfNodes + j];
            graph[i].push_back(numberOfNodes + j);
            graph[numberOfNodes + j].push_back(i);
            cost[numberOfNodes + j][i] = -cost[i][numberOfNodes + j];
            capacity[i][numberOfNodes + j] = 1;
        }

    for (i = 1; i <= numberOfNodes; i++) {
        graph[0].push_back(i);
        capacity[0][i] = 1;
        graph[numberOfNodes + i].push_back(2 * numberOfNodes + 1);
        capacity[numberOfNodes + i][2 * numberOfNodes + 1] = 1;
    }

    source = 0, destination = 2 * numberOfNodes + 1;

    while (bfs(source) == 1) {
        int currentNode = destination;
        int minFlow = inf;

        while (currentNode != source) {
            minFlow = min(minFlow, capacity[parent[currentNode]][currentNode] - flow[parent[currentNode]][currentNode]);
            currentNode = parent[currentNode];
        }

        currentNode = destination;
        while (currentNode != source) {
            flow[parent[currentNode]][currentNode] += minFlow;
            flow[currentNode][parent[currentNode]] -= minFlow;
            currentNode = parent[currentNode];
        }

        totalCost += distanceFromSource[destination] * minFlow;
    }

    outputFile << totalCost;
    return 0;
}