Pagini recente » Cod sursa (job #2000545) | Cod sursa (job #1366253) | Cod sursa (job #643956) | Cod sursa (job #211528) | Cod sursa (job #1515633)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define DIM 355
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
int N, M, S, D, minim, Cost;
bitset <DIM> inQueue;
vector <int> v[DIM];
int C[DIM][DIM];
int F[DIM][DIM];
int cost[DIM][DIM];
int Di[DIM];
int T[DIM];
queue <int> Q;
int BF(){
inQueue.reset();
inQueue[S]=1;
for(int i=1;i<=N;i++)
Di[i]=0x3f3f3f3f;
Di[S]=0;
Q.push(S);
while(!Q.empty()){
int node = Q.front();
Q.pop();
inQueue[node] = 0;
for(std::vector <int>::iterator it=v[node].begin();it!=v[node].end();it++){
int vec = *it;
if(C[node][vec] > F[node][vec] && Di[vec] > Di[node] + cost[node][vec]){
Di[vec] = Di[node] + cost[node][vec];
T[vec] = node;
if(!inQueue[vec]){
inQueue[vec] = 1;
Q.push(vec);
}
}
}
}
if(Di[D] != 0x3f3f3f3f)
return 1;
return 0;
}
int main(){
fin >> N >> M >> S >> D;
for(int i=1;i<=M;i++){
int x,y,z,w;
fin >> x >> y >> z >> w;
C[x][y] = z;
cost[x][y] = w;
cost[y][x] = -w;
v[x].push_back(y);
v[y].push_back(x);
}
while(BF()){
minim = 0x3f3f3f3f;
int U = D;
while(U!=S){
if(minim > C[T[U]][U] - F[T[U]][U])
minim = C[T[U]][U] - F[T[U]][U];
U = T[U];
}
U = D;
while(U!=S){
Cost += minim * cost[T[U]][U];
F[T[U]][U] += minim;
F[U][T[U]] -= minim;
U = T[U];
}
}
fout << Cost << "\n";
}