Pagini recente » Cod sursa (job #1953983) | Cod sursa (job #2922758) | Cod sursa (job #712328) | Cod sursa (job #2038838) | Cod sursa (job #1452267)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
//max flow ver 1.0
struct edge
{
int from;
int to;
int cap;
int flow;
int revid;
int resid()
{
return cap - flow;
}
};
#define pb push_back
typedef vector<int> vi;
vector<edge> *graph;
int sz = 0;
int source, sink;
void init(int n, int s, int t)
{
sz = n;
graph = new vector<edge>[sz+1];
for (int i=0; i<=sz; i++) graph[i].clear();
source = s;
sink = t;
}
void addedge(int from, int to, int cap)
{
edge e;
e.from = from;
e.to = to;
e.cap = cap;
e.flow = 0;
e.revid = graph[to].size();
graph[from].pb(e);
e.revid = graph[from].size()-1;
swap (e.from,e.to);
e.cap = 0;
graph[to].pb(e);
}
bool found = 0;
int foundc = 0;
bool *touched;
void dfspath(int x, int cap)
{
if (found) return;
if (x==sink)
{
foundc = cap;
found = 1;
return ;
}
touched[x] = 1;
for (auto &i:graph[x])
{
if (touched[i.to]) continue;
if (i.resid()>0) dfspath(i.to,min(cap,i.resid()));
if (found)
{
i.flow+=foundc;
graph[i.to][i.revid].flow-=foundc;
break;
}
}
}
void initdfs()
{
touched = new bool[sz+1];
for (int i =0; i<=sz; i++ )
touched[i] = 0;
found = 0;
foundc=0;
}
const int inf = 1000000001;
int maxflow()
{
while (true)
{
initdfs();
dfspath(source,inf);
if (!found) break;
}
int flow = 0;
for (auto i:graph[source])
flow+=i.flow;
return flow;
}
///////////
#ifdef home
ifstream fin("input.txt");
ofstream fout("output.txt");
#else
ifstream fin("ssm.in");
ofstream fout("ssm.out");
#endif
int n,m;
int main()
{
fin>>n>>m;
init(n,1,n);
for (int i=0; i<m; i++)
{
int x,y,z;
fin>>x>>y>>z;
addedge(x,y,z);
}
int rez = maxflow();
fout<<rez;
return 0;
}