Pagini recente » Cod sursa (job #1205484) | Cod sursa (job #2967907) | Cod sursa (job #1443328) | Cod sursa (job #61198) | Cod sursa (job #3188650)
#include <stdio.h>
#include <string.h>
#include <queue>
#include <bitset>
#include <algorithm>
#include <assert.h>
#define MAX_NODES 256
#define LARGE_NUMBER 0x3f3f3f3f
using namespace std;
const char INPUT_FILE[] = "cc.in";
const char OUTPUT_FILE[] = "cc.out";
// Representing the cost, capacity, and flow between nodes
int cost[MAX_NODES][MAX_NODES], capacity[MAX_NODES][MAX_NODES], flow[MAX_NODES][MAX_NODES], potential[MAX_NODES], parent[MAX_NODES];
// Function to implement Bellman-Ford algorithm
bool bellmanFord(int numNodes) {
int currentNode, nextNode;
bitset<MAX_NODES> visited;
queue<int> bfsQueue;
// Start with the source node
bfsQueue.push(0);
// Initialize potential and visited arrays
potential[0] = 0;
visited[0] = true;
// Perform BFS to find the shortest path in terms of potential
while (!bfsQueue.empty()) {
currentNode = bfsQueue.front();
bfsQueue.pop();
// Explore neighbors of the current node
for (nextNode = 0; nextNode <= 2 * numNodes + 1; ++nextNode) {
if (capacity[currentNode][nextNode] > flow[currentNode][nextNode] &&
cost[currentNode][nextNode] + potential[currentNode] < potential[nextNode]) {
potential[nextNode] = cost[currentNode][nextNode] + potential[currentNode];
parent[nextNode] = currentNode;
// If the neighbor is not visited, mark it as visited and enqueue it
if (!visited[nextNode]) {
visited[nextNode] = true;
bfsQueue.push(nextNode);
}
}
}
// Mark the current node as not visited for the next iteration
visited[currentNode] = false;
}
// Return true if there is an augmenting path
return potential[2 * numNodes + 1] != LARGE_NUMBER;
}
// Function to calculate maximum flow
int calculateMaxFlow(int numNodes) {
int totalCost = 0, currentNode, minFlow;
while (bellmanFord(numNodes)) {
minFlow = LARGE_NUMBER;
// Find the minimum flow in the augmenting path
for (currentNode = 2 * numNodes + 1; currentNode; currentNode = parent[currentNode]) {
minFlow = min(minFlow, capacity[parent[currentNode]][currentNode] - flow[parent[currentNode]][currentNode]);
}
// Update the flow along the augmenting path
for (currentNode = 2 * numNodes + 1; currentNode; currentNode = parent[currentNode]) {
flow[parent[currentNode]][currentNode] += minFlow;
flow[currentNode][parent[currentNode]] -= minFlow;
}
// Update the total cost
totalCost += potential[2 * numNodes + 1] * minFlow;
}
// Return the total cost of the maximum flow
return totalCost;
}
int main() {
int i, j, numNodes; // Declare numNodes here
freopen(INPUT_FILE, "r", stdin);
scanf("%d", &numNodes);
// Fill in the cost matrix based on the input
for (i = 1; i <= numNodes; ++i) {
for (j = 1; j <= numNodes; ++j) {
scanf("%d", &cost[i][numNodes + j]);
cost[numNodes + j][i] = -cost[i][numNodes + j];
capacity[i][numNodes + j] = 1;
}
}
fclose(stdin);
// Set the capacities for source and sink nodes
for (i = 1; i <= numNodes; ++i) capacity[0][i] = 1;
for (i = numNodes + 1; i <= 2 * numNodes; ++i) capacity[i][2 * numNodes + 1] = 1;
// Calculate and print the maximum flow
freopen(OUTPUT_FILE, "w", stdout);
printf("%d\n", calculateMaxFlow(numNodes));
fclose(stdout);
return 0;
}