Pagini recente » Cod sursa (job #515781) | Cod sursa (job #3180905) | Cod sursa (job #2923608) | Cod sursa (job #1789204) | Cod sursa (job #3191100)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cc.in");
ofstream g("cc.out");
int main() {
int n, c, aux;
f >> n;
vector<vector<int>> costs(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) {
f >> c;
costs[i].push_back(c);
}
}
vector<int> matching(n, 0);
for(int i = 0; i < n; i++)
matching[i] = i;
bool ok = true;
while(ok) {
ok = false;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j) {
//incercam sa schimba matching ai i -> matching[j] si j -> matching[i]
if (costs[i][matching[i]] + costs[j][matching[j]] > costs[i][matching[j]] + costs[j][matching[i]]) {
aux = matching[i];
matching[i] = matching[j];
matching[j] = aux;
ok = true;
}
}
}
int s = 0;
for(int i = 0; i < n; i++)
s += costs[i][matching[i]];
g << s;
return 0;
}