Cod sursa(job #968594)

Utilizator dropsdrop source drops Data 2 iulie 2013 12:50:12
Problema Cuplaj maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.91 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <ctime>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
ifstream ff("cmcm.in");
ofstream gg("cmcm.out");
#define max 606

int n, m, s, d, cc[max][max], zz[max][max], rr[max][max], tt[max];
int qq[2][max], ww[max], dd[max], oo[max][max];
vector<int> vv[max], aa;

bool bel(){
	int x0=0, x1=1, q0=0, q1=0, x, y, l;
	memset(ww, 0, sizeof(ww));
	for(int i=1;i<=d;i++) dd[i]=0xfffffff;
	dd[s]=0;
	ww[s]=1;
	qq[x0][q0++]=s;
	for(int k=1;k<=d;k++){
		while(q0>0){
			x=qq[x0][--q0];
			//cout << x <<"\n";
			if(x==d) continue;
			l=vv[x].size();
			for(int i=0;i<l;i++){
				y=vv[x][i];
				if(dd[y]>dd[x]+zz[x][y] && rr[x][y]<cc[x][y]){
					dd[y]=dd[x]+zz[x][y];
					tt[y]=x;
					if(ww[y]!=k)qq[x1][q1++]=y;
					ww[y]=k;
				}
			}
		}
		x0^=1; x1^=1; swap(q0, q1);
	}
	return ww[d];
}

void flu(){
	int r=0, c;
	while(bel()){
		c=0xfffffff;
		for(int x=d;x!=s;x=tt[x]) c=min(c, cc[tt[x]][x]-rr[tt[x]][x]);
		if(c!=0){
			for(int x=d;x!=s;x=tt[x]){
				rr[tt[x]][x] += c;
				rr[x][tt[x]] -= c;
				r += c * zz[tt[x]][x];
			}
		}
	}
	c=0;
	for(int i=s+1;i<d-1;i++)
	for(int j=i+1;j<d;j++)
		if(rr[i][j]){
			c++;
			aa.push_back(oo[i][j]);
		}
	gg << c << " " << r << "\n";
	for(int i=0;i<(int)aa.size();i++) gg << aa[i] << " ";
}

int main(){
	int a, b, c, r;
	ff >> n >> m >> r;
	s=1; d=n+m+2;
	for(int i=0;i<r;i++){
		ff >> a >> b >> c;
		a++;
		b+=n+1;
		vv[a].push_back(b);
		vv[b].push_back(a);
		oo[a][b]=i+1;
		cc[a][b]=1;
		zz[a][b]=c;
		zz[b][a]=-c;
		if(!cc[s][a]){
			cc[s][a]=1;
			vv[s].push_back(a);
			vv[a].push_back(s);
		}
		if(!cc[b][d]){
			cc[b][d]=1;
			vv[b].push_back(d);
			vv[d].push_back(b);
		}
	}
	flu();
}