Pagini recente » Cod sursa (job #1762933) | Cod sursa (job #2472953) | Cod sursa (job #2037895) | Cod sursa (job #2855764) | Cod sursa (job #881391)
Cod sursa(job #881391)
#ifndef INFOARENA
#define DBG
#endif
#define nume "maxflow"
#include<cstdio>
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cassert>
#include<ctime>
#include<list>
#include<algorithm>
using namespace std;
using namespace __gnu_cxx;
//#define _PARSARE_
#ifdef _PARSARE_
#define DIM 8192
char ax[DIM+16];
int _idx;
template<class T>
inline void cit(T& x)
{
x=0;
while((ax[_idx]<'0' || ax[_idx]>'9') && (ax[_idx]!='-'))
if(++_idx==DIM)fread(ax, 1, DIM, stdin), _idx=0;
int neg=0;
if(ax[_idx]=='-') {
neg=1;
if(++_idx==DIM)fread(ax, 1, DIM, stdin),_idx=0;
}
while(ax[_idx]>='0' && ax[_idx]<='9') {
x=x*10+ax[_idx]-'0';
if(++_idx==DIM)fread(ax,1, DIM, stdin),_idx=0;
}
if(neg) x=-x;
}
#else
#ifndef ONLINE_JUDGE
ifstream fin (nume ".in");
#endif
#endif //_PARSARE_
#ifndef ONLINE_JUDGE
ofstream fout(nume ".out");
#endif
#ifdef DBG
#define fout cout
#endif
//// hash-uri
//#include<ext/hash_map>
//hash_multimap<int,int> H;
//hash_map<char*,int> H;
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
#define foreach(it, v) for (typeof((v).begin()) it = (v).begin(),stop=(v).end(); it != stop; ++it)
#define foreach_r(it, v) for (typeof((v).rbegin()) it = (v).rbegin(),stop=(v).rend(); it != stop; ++it)
template<class T> inline void Swap(T& a,T& b)
{
a^=b,b^=a,a^=b;
}
int n,m;
vector<int> G[2000+3];
int C[1003][1003];
int F[1003][1003];
bool viz[1003];
int flux[1003];
int tata[1003];
bool bfs()
{
memset(viz,0,sizeof(viz));
queue<int> Q;
Q.push(1);
viz[1]=true;
while(!Q.empty()) {
int nod=Q.front();
Q.pop();
foreach(it,G[nod]) {
if(!viz[*it] && C[nod][*it]-F[nod][*it]>0) {
flux[*it] = min(flux[nod], C[nod][*it]-F[nod][*it]);
viz[*it] = true;
Q.push(*it);
tata[*it] = nod;
if(*it == n) {
for (int i=n; i!=1; i=tata[i]) {
F[tata[i]][i] += flux[n];
F[i][tata[i]] -= flux[n];
}
return true;
}
}
}
}
// for (int i=n; i!=1; i=tata[i]) {
// F[tata[i]][i] += flux[n];
// F[i][tata[i]] -= flux[n];
// }
// return flux[n] != 0;
return false;
}
int main()
{
#ifdef _PARSARE_
#ifndef ONLINE_JUDGE
freopen(nume ".in","r",stdin);
#endif
cit(n);
#endif
fin>>n>>m;
for(int i=0; i<m; ++i) {
int x,y,c;
fin>>x>>y>>c;
G[x].push_back(y);
C[x][y]=c;
G[y].push_back(x);
// C[y][x]=0;
}
int maxflow=0;
while(true) {
memset(flux,0x3f,sizeof(flux));
if(!bfs()) break;
maxflow+=flux[n];
}
fout<<maxflow<<'\n';
#ifndef DBG
#ifndef ONLINE_JUDGE
fout.close();
#endif
#endif
return 0;
}