Mai intai trebuie sa te autentifici.
Cod sursa(job #1290568)
Utilizator | Data | 11 decembrie 2014 15:03:59 | |
---|---|---|---|
Problema | Flux maxim | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.63 kb |
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "maxflow";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = 666013;
const int NMAX = 1000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;
int N, M, maxFlow;
int source, sink;
vector<int> V[NMAX];
int F[NMAX][NMAX];
int C[NMAX][NMAX];
bitset<NMAX> viz;
deque<int> Q;
int dad[NMAX];
int bfs() {
int x;
Q.clear();
viz = 0;
Q.push_back(source);
viz[source] = 1;
while(!Q.empty()) {
x = Q.front();
Q.pop_front();
if(x == sink)
return 1;
for(auto y : V[x])
if(F[x][y] < C[x][y] && !viz[y]) {
Q.push_back(y);
viz[y] = 1;
dad[y] = x;
}
}
return 0;
}
int main() {
int x, y, z, v;
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
scanf("%d%d", &N, &M);
while(M--) {
scanf("%d%d%d", &x, &y, &z);
V[x].push_back(y);
V[y].push_back(x);
C[x][y] = z;
}
source = 1;
sink = N;
while(bfs())
for(auto x : V[sink])
if(viz[x] && F[x][sink] < C[x][sink]) {
dad[sink] = x;
v = INF;
for(y = sink; y != source; y = dad[y])
v = min(v, C[dad[y]][y] - F[dad[y]][y]);
for(y = sink; y != source; y = dad[y]) {
F[dad[y]][y] += v;
F[y][dad[y]] -= v;
}
maxFlow += v;
}
printf("%d\n", maxFlow);
return 0;
}