Pagini recente » Cod sursa (job #1491655) | Cod sursa (job #546805) | Cod sursa (job #430474) | Cod sursa (job #968773) | Cod sursa (job #1957312)
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define MAXN 1001
#define DIM 8192
using namespace std;
class Reader
{
public:
Reader(const char* filename)
{
if(filename[0]=='.'||filename[0]==0)
{
cout<<"Check INFILE.\n";
return;
}
try
{
f.open(filename);
pos = 0;
if(f.fail())throw 1;
f.read(buffer,DIM);
}
catch(int e)
{
cout<<"No INFILE.\n";
delete this;
}
}
inline Reader& operator >>(int& x)
{
x=0;
sgn=1;
while((buffer[pos]<'0'||buffer[pos]>'9')&&buffer[pos]!='-')
if(++pos==DIM)
f.read(buffer,DIM),pos=0;
if(buffer[pos]=='-')
{
sgn=-1;
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
while(buffer[pos]>='0'&&buffer[pos]<='9')
{
x=x*10+buffer[pos]-'0';
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
x*=sgn;
return (*this);
}
inline Reader& operator >>(char* c)
{
aux = 0;
while(isspace(buffer[pos]))
if(++pos==DIM)
f.read(buffer,DIM),pos=0;
while(!isspace(buffer[pos]))
{
c[aux++]=buffer[pos];
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
return (*this);
}
~Reader()
{
f.close();
}
private:
ifstream f;
int pos,aux,sgn;
char buffer[DIM];
};
Reader fin("maxflow.in");
ofstream g("maxflow.out");
int n,m,c[MAXN][MAXN],f[MAXN][MAXN],i,j,x,y,z,t[MAXN],flux;
vector<int> G[MAXN];
vector<int>::iterator it;
bool bfs()
{
queue<int> q;
q.push(1);
memset(t,0,sizeof(t));
t[1]=-1;
while(!q.empty())
{
x=q.front();
q.pop();
for(it=G[x].begin();it!=G[x].end();it++)
if(!t[*it]&&c[x][*it]>f[x][*it])
{
t[*it]=x;
q.push(*it);
if(*it==n) return 1;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
fin>>c[x][y];
G[x].pb(y);
G[y].pb(x);
}
while(bfs())
for(it=G[n].begin();it!=G[n].end();it++)
{
if(!t[*it]||c[*it][n]<=f[*it][n]) continue;
x=c[*it][n]-f[*it][n];
j=*it;
while(t[j]!=-1)
{
x=min(x,c[t[j]][j]-f[t[j]][j]);
j=t[j];
}
if(!x)continue;
f[*it][n]+=x;
f[n][*it]-=x;
j=*it;
while(t[j]!=-1)
{
f[t[j]][j]+=x;
f[j][t[j]]-=x;
j=t[j];
}
flux+=x;
}
g<<flux;
return 0;
}