Pagini recente » Cod sursa (job #1783671) | Cod sursa (job #1605923) | Cod sursa (job #1547700) | Cod sursa (job #390508) | Cod sursa (job #3186046)
# include <iostream>
# include <fstream>
# include <queue>
# include <algorithm>
using namespace std;
ifstream fin("cc.in");
ofstream fout("cc.out");
const int INF = 1e9;
const int N_MAX = 300;
int tata[N_MAX], graf[N_MAX][N_MAX], cost[N_MAX][N_MAX], val[N_MAX], S = 1, D, NMAX;
struct elem {
int cost, tata, nod;
elem(int _cost, int _nod, int _tata) : nod(_nod), cost(_cost), tata(_tata) {}
};
bool cmp(const elem& x, const elem& y) {
return x.cost > y.cost;
}
void update(int q) {
}
bool dijkstra() {
priority_queue<elem, vector<elem>, decltype(&cmp)> q(cmp);
fill(val, val + N_MAX, INF);
q.push(elem(0, S, -1));
val[S] = 0;
while (!q.empty()) {
elem top_node = q.top();
q.pop();
int c = top_node.cost;
int nod = top_node.nod;
int t = top_node.tata;
if (c > val[nod]) continue;
tata[nod] = t;
if (nod == D) return true;
for (int i = 0; i <= NMAX; i++) {
int new_cost = c + cost[nod][i];
if (graf[nod][i] > 0 && new_cost < val[i]) {
val[i] = new_cost;
q.push(elem(new_cost, i, nod));
}
}
}
return false;
}
int main() {
int m;
fin >> m;
S = 1, D = NMAX = 2 * m + 2;
for (int i = 2; i <= m + 1; i++) {
graf[1][i] = graf[i + m][2 * m + 2] = 1;
}
for (int i = 2; i < m + 2; i++) {
for (int j = m + 2; j < 2 * m + 2; j++) {
fin >> cost[i][j];
cost[j][i] = -cost[i][j];
graf[i][j] = 1;
}
}
while (dijkstra()) {
for (int i = D; tata[i] != -1; i = tata[i]) {
graf[tata[i]][i] -= 1;
graf[i][tata[i]] += 1;
}
}
int rez = 0;
for (int i = 2; i < m + 2; i++) {
for (int j = m + 2; j < 2 * m + 2; j++) {
if (graf[i][j] == 0) {
rez += cost[i][j];
}
}
}
fout << rez;
return 0;
}