Pagini recente » Istoria paginii runda/sanki | Cod sursa (job #1459652) | Cod sursa (job #2677819) | Cod sursa (job #2581352) | Cod sursa (job #1419447)
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <limits>
#define pb push_back
#define mp make_pair
#define INF numeric_limits<int>::max()
#define NM 100
using namespace std;
ifstream in("cc.in");
ofstream out("cc.out");
vector< vector<int> > a(300);
queue<int> q;
int n,m,flow[300][300],cap[300][300],cost[300][300],pre[300],dist[300],tcost,maxflow;
bool use[300];
const int S=201,D=202;
bool bellman()
{
for(int i=1;i<=203;i++)
{
pre[i]=0;
use[i]=false;
dist[i]=INF;
}
dist[S]=0;
pre[S]=S;
use[S]=true;
q.push(S);
while(!q.empty())
{
int x=q.front();q.pop();
use[x]=false;
for(vector<int>::iterator i=a[x].begin();i!=a[x].end();i++)
if(dist[*i] > dist[x]+cost[x][*i] && cap[x][*i]-flow[x][*i] > 0)
{
pre[*i]=x;
dist[*i]=dist[x]+cost[x][*i];
if(use[*i]==false)
{
use[*i]=true;
q.push(*i);
}
}
}
return pre[D];
}
int main()
{
in>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int z,x,y;
in>>z;
x=i;y=j+NM;
a[x].pb(y);
a[y].pb(x);
cost[x][y]=z;
cost[y][x]=-z;
cap[x][y]=1;
}
for(int i=1;i<=n;i++)
{
a[S].pb(i);
a[i].pb(S);
cap[S][i]=1;
a[i+NM].pb(D);
a[D].pb(i+NM);
cap[i+NM][D]=1;
}
for(;bellman();)
{
int mx=INF;
for(int x=D;pre[x]!=x;x=pre[x])
mx=min(mx,cap[pre[x]][x]-flow[pre[x]][x]);
for(int x=D;pre[x]!=x;x=pre[x])
{
tcost+=cost[pre[x]][x]*mx;
flow[pre[x]][x]+=mx;
flow[x][pre[x]]-=mx;
}
maxflow+=mx;
}
out<<tcost<<'\n';
return 0;
}