Cod sursa(job #582835)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 16 aprilie 2011 05:27:16
Problema Cc Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include<cstdio>
#include<vector>
#include<bitset>
#include<utility>
#include<queue>
using namespace std;
#define mp make_pair
#define pb push_back
#define NM 201
#define fs first
#define sc second
#define OO 1<<25
#define sh short int
queue<sh >q;
vector<pair<sh, sh> >a[NM+1];
bitset<NM+1>viz;
sh N,x,y,cost,dest,t[NM+1];
int dist[NM+1],f[NM+1][NM+1],c[NM+1][NM+1];
inline int minim(int a , int b)
{
	if (a<b)
		return a ;
	return b;
}
inline void citire()
{
	freopen("cc.in","r",stdin);
	freopen("cc.out","w",stdout);
	scanf("%hd",&N);
	sh i,j;
	for (i=1; i<=N; ++i)
		for (j=1; j<=N; ++j)
		{
			scanf("%hd",&cost);
			a[i].pb(mp(j+N,cost));
			a[j+N].pb(mp(i,-cost));
			c[i][j+N]=1;
		}
	dest=(N<<1)+1;
	for (i=1; i<=N; ++i)
	{
		a[0].pb(mp(i,0));
		a[i].pb(mp(0,0));
		c[0][i]=1;
		a[N+i].pb(mp(dest,0));
		a[dest].pb(mp(N+i,0));
		c[N+i][dest]=1;
	}
}
inline int bf()
{
	for (int i=1; i<=dest; ++i)
	{
		dist[i]=OO;
		t[i]=-1;
		viz[i]=0;
	}
	viz[0]=1;
	q.push(0);
	int p=1;
	dist[0]=0;
	while (p)
	{
		x=q.front();
		--p;
		q.pop();
		sh g=a[x].size();
		viz[x]=0;
		for (sh i=0; i<g; ++i)
		{
			y=a[x][i].fs;
			if (c[x][y]>f[x][y] && dist[y]>dist[x]+a[x][i].sc)
			{
				dist[y]=dist[x]+a[x][i].sc;
				t[y]=x;
				if (!viz[y])
				{
					viz[y]=1;
					q.push(y);
					++p;
				}
			}
		}
		
	}
	if (dist[dest]==OO)
		return 0;
	int flux=OO;
	for (sh i=dest; i; i=t[i])
		flux=minim(flux,1-f[t[i]][i]);
	if (!flux)
		return 0;
	for (sh i=dest; i; i=t[i])
	{
		f[t[i]][i]+=flux;
		f[i][t[i]]-=flux;
	}
	return flux*dist[dest];
}
inline void solve()
{
	int imp=1,rez=0;
	while (imp)
	{
		imp=bf();
		rez+=imp;
	}
	printf("%d ",rez);
}
int main()
{
	citire();
	solve();
	return 0;
}