Cod sursa(job #526900)

Utilizator cosmyoPaunel Cosmin cosmyo Data 29 ianuarie 2011 19:02:19
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.7 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#define f first
#define s second
using namespace std;
const int N = 5005;
const int INF = 0x3f3f3f3f;
int f[105][105], n, k, S, D, t[105], v[105], tt[105], st[105][105];
vector<int >  a[105];
vector<int> c[105][105];
pair<int, int> P[N];

int BFS() {
	int i, k, j;
	int cd[105];
	for(i = 0; i <= 100;++i)
		v[i] = tt[i] = 0, t[i] = -1;
	cd[0] = 1;
	cd[1] = 0;
	v[0] = 1;
	for(j = 1; j <= cd[0]; ++j) {
		k = cd[j];
		if(k == 100)
			return 1;
		for(i = 0; i < a[k].size(); ++i) 
			if(!v[a[k][i]] && f[k][a[k][i]] < c[k][a[k][i]].size()) {
				cd[++cd[0]] = a[k][i];
				t[a[k][i]] = k;
				v[a[k][i]] = 1;
			}
	}
  return v[100];
}

int min(int a, int b) {
	return a < b ? a : b;
}

int main() {
	freopen("ghizi.in", "r", stdin);
	freopen("ghizi.out", "w", stdout);
	int i, j, x, y, r = 0 , w;
	int sol[N * 5], nr = 0;
	scanf("%d %d", &n, &w);
	for(i = 1; i <= n; ++i) 
		scanf("%d %d", &x, &y),	a[x].push_back(y),	c[x][y].push_back(i), a[y].push_back(x);
	for(x = 0;x < w &&  BFS() ;) {
		for(j = 0; j < a[100].size(); ++j) 
			if(v[a[100][j]] && f[a[100][j]][100] < c[a[100][j]][100].size()) {
				k = 100;
				r = INF;
				t[100] = a[100][j];
				while(t[k] != -1) {
					r = min(r, c[t[k]][k].size() - f[t[k]][k]);
					k = t[k];
				}
				if(x + r > w)
					r = w - x;
				if(r) {
					k = 100;
					while(t[k] != -1) {
						f[t[k]][k] += r;
						f[k][t[k]] -= r;
						for(i = st[t[k]][k]; i < c[t[k]][k].size() && i < st[t[k]][k] + r; ++i)
							sol[++nr] = c[t[k]][k][i];
						st[t[k]][k] = i;
						k = t[k];
					}
					x += r;
				}
			}

	}
	k = 0;
	printf("%d \n",  nr);
	sort(sol + 1, sol + nr + 1);
	for(i = 1; i <= nr; ++i)
		printf("%d ", sol[i]);
	return 0;
}