Cod sursa(job #295979)

Utilizator tudalexTudorica Constantin Alexandru tudalex Data 3 aprilie 2009 21:34:42
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

const int n_max = 1024;
const int m_max = n_max*10;
int a[n_max][n_max],
    v[n_max][n_max],
    b[m_max][2],
    viz[n_max];
queue <int > s;
vector < int > sol;
int dev, sup, n, m;
int bf()
{
	int x;
	s.push(dev);
	memset(viz,0xff,sizeof(viz));
	while (!s.empty())
	{
		x = s.front();
		if (x == sup)
			break;
		for (int i = 1; i <= v[x][0]; ++ i)
			if (a[x][v[x][i]] > 0 && viz[v[x][i]] == -1)
			{
				s.push(v[x][i]);
				viz[v[x][i]] = x;
			}
		s.pop();
	}
	while (!s.empty())
		s.pop();
	if (x != sup)
		return 0;
	int k = x;
	int m = 1<<30;
	while (k != dev)
	{
		m = min(m, a[viz[k]][k]);
		k = viz[k];
	}
	while (x != dev)
	{
		a[viz[x]][x]-=m;
		a[x][viz[x]]+=m;
		x = viz[x];
	}
	return 1;
}
void bfs(int start, int mark) // A more special approach to Bread First Search
{
	viz[start] = mark;
	s.push(start);
	while (!s.empty())
	{
		int x = s.front();
		for (int i = 1; i <= v[x][0]; ++ i)
			if (a[x][v[x][i]] > 0 && ! viz[v[x][i]])
			{
				s.push(v[x][i]);
				viz[v[x][i]] = mark;
			}
		s.pop();
	}
}

int main()
{
	freopen("critice.in","r",stdin);
	freopen("critice.out","w",stdout);
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; ++ i)
	{
		int x, y, z;
		scanf("%d %d %d", &x, &y, &z);
		a[x][y] += z;
		a[y][x] += z;
		b[i][0] = x;
		b[i][1] = y;
		v[x][++v[x][0]] = y;
		v[y][++v[y][0]] = x;
	}
	dev = 1;
	sup = n;
	while (bf())
		;
	memset(viz,0,sizeof(viz));
	bfs(1,1);
	bfs(n,2);
	for (int i = 1; i <= m; ++ i)
	{
		int x = b[i][0], y = b[i][1];
		if (a[x][y] != a[y][x] && (a[x][y] == 0 || a[y][x] == 0) && (viz[x] != 0 && viz[y] != 0 && viz[x]!=viz[y]))
				sol.push_back(i);
	}
	printf("%d\n", sol.size());
	for (int i = 0; i < sol.size(); ++ i)
		printf("%d\n", sol[i]);
	return 0;
}