Cod sursa(job #582579)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 15 aprilie 2011 16:33:31
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include<cstdio>
#include<vector>
#include<bitset>
#include<queue>
#include<utility>
using namespace std;
#define NM 351
#define I inline
#define fn freopen
#define sh short int
#define mp make_pair
#define OO 1<<25
#define fs first
#define sc second
#define pb push_back
sh N,M,S,D,x,y;
vector<pair <sh, sh> > a[NM];
bitset<NM>viz;
int dist[NM],c[NM][NM],f[NM][NM];
sh t[NM];
queue<sh >q;
inline int minim(int a , int b)
{
	if (a>b) return b;
	return a;
}
I void citire()
{
	fn("fmcm.in","r",stdin);
	fn("fmcm.out","w",stdout);
	scanf("%hd%hd%hd%hd",&N,&M,&S,&D);
	sh cost, cap;
	while (M--)
	{
		scanf("%hd%hd%hd%hd",&x,&y,&cap,&cost);
		a[x].pb(mp(y,cost));
		a[y].pb(mp(x,-cost));
		c[x][y]=cap;
	}
}
I int bf()
{
	for (int i=1; i<=N; ++i)
	{
		dist[i]=OO;
		t[i]=0;
		viz[i]=0;
	}
	q.push(S);
	viz[S]=1;
	dist[S]=0;
	int p=1;
	while (p)
	{
		x=q.front();
		viz[x]=0;
		--p;
		q.pop();
		int g=a[x].size();
		for (int i=0; i<g; ++i)
		{
			y=a[x][i].fs;
			if (c[x][y]-f[x][y]>0 && dist[y]>dist[x]+a[x][i].sc)
			{
				dist[y]=dist[x]+a[x][i].sc;
				t[y]=x;
				if (!viz[y])
				{
					q.push(y);
					++p;
					viz[y]=1;
				}
			}
		}
	}
	if (dist[D]==OO)
		return 0;
	int flux=OO;
	for (int i=D; i!=S; i=t[i])
		flux=minim(flux, c[t[i]][i]-f[t[i]][i]);
	if (flux==0)
		return 0;
	for (int i=D; i!=S; i=t[i])
	{
		f[t[i]][i]+=flux;
		f[i][t[i]]-=flux;
	}
	return flux*dist[D];
}
I void flux_max_cost_min()
{
	int imp=1;
	int flux=0;
	while (imp)
	{
		imp=bf();
		flux+=imp;
	}
	printf("%d",flux);
}
int main()
{
	citire();
	flux_max_cost_min();
	return 0;
}