Pagini recente » Cod sursa (job #1139831) | Cod sursa (job #2852731) | Cod sursa (job #263779) | Cod sursa (job #1971009) | Cod sursa (job #1467844)
#include <fstream>
#include <cstring>
#define Max 1000000000
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
struct coada
{
int nod,cost,come;
} co[55000];
struct noduri
{
int cost,nod;
} tmp;
vector<noduri> qw[1010],maxx[1010];
int viz[5010];
int in,s,sf,nod,cost,i,n,m,j,x,y,c;
void _think(int last)
{
int cost= co[last].cost;
while(co[last].come!=0)
{
int prev=co[last].come;
for(i=0; i<qw[co[prev].nod].size(); i++)
if(co[last].nod==qw[co[prev].nod][i].nod)
{
qw[co[prev].nod][i].cost+=cost;
qw[co[last].nod][i].cost-=cost;
}
last=prev;
}
}
bool bfs()
{
in=1;
sf=0;
co[++sf].nod=1;
co[sf].come=0;
co[sf].cost=Max;
memset(viz,0,sizeof(viz));
while(in<=sf)
{
nod=co[in].nod;
cost=co[in].cost;
for(i=0; i<maxx[nod].size(); i++)
{
if(qw[nod][i].cost<maxx[nod][i].cost && viz[maxx[nod][i].nod]==0)
{
viz[qw[nod][i].nod]==1;
co[++sf].nod=qw[nod][i].nod;
co[sf].come=in;
co[sf].cost= cost < (maxx[nod][i].cost-qw[nod][i].cost) ? cost : (maxx[nod][i].cost-qw[nod][i].cost);
if(maxx[nod][i].nod==n)
{
s+=co[sf].cost;
_think(sf);
return true;
}
}
}
in++;
}
return false;
}
int main()
{
fin>>n>>m;
for(i=1; i<=m; i++)
{
fin>>x>>y>>c;
tmp.cost=c;
tmp.nod=y;
maxx[x].push_back(tmp);
tmp.cost=0;
qw[x].push_back(tmp);
tmp.nod=x;
maxx[y].push_back(tmp);
qw[y].push_back(tmp);
}
while(bfs());
fout<<s;
return 0;
}