Cod sursa(job #531551)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 9 februarie 2011 21:11:18
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <cstdio>
#include <vector>
using namespace std;
#define nmax 1010
#define f first
#define s second

vector <pair<int, int> > g[nmax];
int n, m, c[nmax][nmax], f[nmax][nmax], q[nmax], t[nmax], p[10*nmax], u[nmax], nr, s[nmax];

int bf()
{
	int i, j, nod, x, h=1;
	for (i=1; i<=n; i++) u[i]=0;
	q[1]=1;
	u[1]=1;
	for (i=1; i<=h; i++)
	{
		nod=q[i];
		if (nod!=n)
			for (j=0; j<g[nod].size(); j++)
			{
				x=g[nod][j].f;
				if (!u[x] && c[nod][x]!=f[nod][x])
				{
					q[++h]=x;
					u[x]=1;
					t[x]=nod;
				}
			}
	}
	return u[n];
}

void df(int nod)
{
	u[nod]=1;
	int i, x;
	for (i=0; i<g[nod].size(); i++)
	{
		x=g[nod][i].f;
		if (!u[x])
		{
			if (f[x][nod]==c[x][nod] || f[nod][x]==c[nod][x])p[g[nod][i].s]++;
			if (f[nod][x]!=c[nod][x] && f[x][nod]!=c[x][nod])
				df(x);
		}
	}
}

int main()
{
	freopen("critice.in","r",stdin);
	freopen("critice.out","w",stdout);
	scanf("%d %d",&n, &m);
	int i, nod, x, y, fm, z;
	for (i=1; i<=m; i++)
	{
		scanf("%d %d %d",&x, &y, &z);
		g[x].push_back(make_pair(y, i));
		g[y].push_back(make_pair(x, i));
		c[x][y] +=z;
		c[y][x] +=z;
	}
	while (bf())
		for (i=0; i<g[n].size(); i++)
		{
			nod=g[n][i].f;
			if (u[nod] && f[nod][n]!=c[nod][n])
			{
				t[n]=nod;
				fm=1<<30;
				nod=n;
				while (t[nod])
				{
					x=c[t[nod]][nod]-f[t[nod]][nod];
					if (x<fm) fm=x;
					nod=t[nod];
				}
				if (fm)
				{
					nod=n;
					while (t[nod])
					{
						f[t[nod]][nod] += fm;
						f[nod][t[nod]] -= fm;
						nod=t[nod];
					}
				}
			}
		}
	for (i=1; i<=n; i++) u[i]=0;
	df(1);
	for (i=1; i<=n; i++) u[i]=0;
	df(n);
	for (i=1; i<=m; i++)
		if (p[i]==2) nr++;
	printf("%d\n",nr);
	for (i=1; i<=m; i++) 
		if (p[i]==2) printf("%d\n",i);
}