Pagini recente » Cod sursa (job #2298986) | Cod sursa (job #1323987) | Cod sursa (job #2262793) | Cod sursa (job #2230612) | Cod sursa (job #1238059)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "fmcm.in";
const char outfile[] = "fmcm.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 355;
const int oo = 0x3f3f3f3f;
typedef vector<pair<int, int> > Graph[MAXN];
typedef vector<pair<int, int> > :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
Graph G;
int C[MAXN][MAXN], N, M, Source, Sink, Father[MAXN], dp[MAXN];
bitset <MAXN> inQ;
queue <int> Q;
inline bool BF(Graph &G, int Source, int Sink) {
Q.push(Source);
inQ[Source] = 1;
memset(dp, oo, sizeof(dp));
dp[Source] = 0;
while(!Q.empty()) {
int Node = Q.front();
Q.pop();
inQ[Node] = 0;
for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
if(C[Node][it->first] > 0 && dp[it->first] > dp[Node] + it->second) {
dp[it->first] = dp[Node] + it->second;
Father[it->first] = Node;
if(inQ[it->first])
continue;
Q.push(it->first);
inQ[it->first] = 1;
}
}
return dp[Sink] != oo;
}
inline int getMinCostMaxFlow(Graph &G, int Source, int Sink) {
int minCostMaxFlow = 0;
while(BF(G, Source, Sink)) {
int bottleNeck = oo;
for(int i = Sink ; i != Source ; i = Father[i])
bottleNeck = min(bottleNeck, C[Father[i]][i]);
if(!bottleNeck)
continue;
for(int i = Sink ; i != Source ; i = Father[i]) {
C[Father[i]][i] -= bottleNeck;
C[i][Father[i]] += bottleNeck;
}
minCostMaxFlow += bottleNeck * dp[Sink];
}
return minCostMaxFlow;
}
const int lim = (1 << 20);
char buff[lim];
int pos;
inline void get(int &x) {
x = 0;
char sgn = '+';
while(!('0' <= buff[pos] && buff[pos] <= '9')) {
sgn = buff[pos];
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
}
while('0' <= buff[pos] && buff[pos] <= '9') {
x = x * 10 + buff[pos] - '0';
if(++ pos == lim) {
fread(buff, 1, lim, stdin);
pos = 0;
}
}
if(sgn == '-')
x = -x;
}
int main() {
freopen(infile, "r", stdin);
get(N);
get(M);
get(Source);
get(Sink);
//fin >> N >> M >> Source >> Sink;
for(int i = 1 ; i <= M ; ++ i) {
int x, y, z, c;
get(x);
get(y);
get(z);
get(c);
//fin >> x >> y >> z >> c;
G[x].push_back(make_pair(y, c));
G[y].push_back(make_pair(x,-c));
C[x][y] = z;
}
fout << getMinCostMaxFlow(G, Source, Sink) << '\n';
fin.close();
fout.close();
return 0;
}