Pagini recente » Cod sursa (job #2260188) | Cod sursa (job #2330227) | Cod sursa (job #1668732) | Cod sursa (job #3213591) | Cod sursa (job #2166070)
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
FILE *fin = freopen("cc.in", "r", stdin);
FILE *fout = freopen("cc.out", "w", stdout);
const int INF = 0x7fffffff;
const int NMax = 102;
int N, S, D, sol;
int carry[NMax][NMax], cost[NMax][NMax], dist[NMax], fromWhere[NMax];
bool used[NMax];
vector < int > v[NMax];
queue < int > q;
inline bool BellmanFord(){
for (int i = S; i <= D; i++){
dist[i] = INF;
used[i] = 0;
}
q.push(S);
used[S] = 1;
dist[S] = 0;
while (!q.empty()){
int node = q.front();
q.pop();
used[node] = 0;
vector < int > :: iterator it;
for (it = v[node].begin(); it != v[node].end(); it ++)
if (carry[node][*it] > 0 && dist[*it] > dist[node] + cost[node][*it]){
dist[*it] = dist[node] + cost[node][*it];
fromWhere[*it] = node;
if (!used[*it]){
used[*it] = 1;
q.push(*it);
}
}
}
if (dist[D] != INF){
for (int i = D; i != S; i = fromWhere[i]){
carry[fromWhere[i]][i] --;
carry[i][fromWhere[i]] ++;
sol += cost[fromWhere[i]][i];
}
return 1;
}
return 0;
}
int main(){
scanf("%d", &N);
S = 0;
D = 2 * N + 1;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++){
scanf("%d", &cost[i][j + N]);
cost[j + N][i] = - cost[i][j + N];
carry[i][j + N] = 1;
v[i].push_back(j + N);
v[j + N].push_back(i);
}
for (int i = 1; i <= N; i++){
v[S].push_back(i);
v[i].push_back(S);
carry[S][i] = 1;
}
for (int i = N + 1; i <= N + N; i++){
v[D].push_back(i);
v[i].push_back(D);
carry[i][D] = 1;
}
while (BellmanFord());
printf("%d\n", sol);
}