Pagini recente » Cod sursa (job #2677014) | Cod sursa (job #1357009) | Cod sursa (job #39486) | Cod sursa (job #1090547) | Cod sursa (job #612951)
Cod sursa(job #612951)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define max_n 351
#define max_m 12501
#define INF 0x3f3f3f3f
#define nod first
#define cst second
int n,m,i,c[max_n][max_n],f[max_n][max_n];
int dist[max_n],t[max_n];
int flux,costM,s,d;
int x,y,cost,cp,r;
bool ok[max_n];
vector < pair<int,int> > g[max_n];
ifstream in("fmcm.in");
ofstream out("fmcm.out");
bool Bellman(int s,int d) {
queue < int > q;
vector < pair<int,int> >::iterator it;
int i,j,cost;
memset(ok,false,sizeof(ok));
memset(dist,INF,sizeof(dist));
q.push(s);
ok[s]=true;
dist[s]=0;
while (!q.empty()) {
x=i=q.front();
q.pop();
ok[x]=false;
for (it=g[x].begin(); it!=g[x].end(); it++) {
j=(*it).nod;
cost=(*it).cst;
if (dist[j]>dist[i]+cost && c[i][j]>f[i][j]) {
dist[j]=dist[i]+cost;
t[j]=i;
if (ok[j]==false) {
ok[j]=true;
q.push(j);
}
}
}
}
return dist[d]!=INF;
}
int main() {
in >> n >> m >> s >> d;
for (i=1; i<=m; i++) {
in >> x >> y >> cp >> cost;
c[x][y]=cp;
g[x].push_back(make_pair(y,cost));
g[y].push_back(make_pair(x,-cost));
}
flux=costM=0;
for (; Bellman(s,d); costM+=r*dist[d]) {
r=INF;
for (i=d; i!=s; i=t[i])
r=min(r,c[t[i]][i]-f[t[i]][i]);
for (i=d; i!=s; i=t[i]) {
f[t[i]][i]+=r;
f[i][t[i]]-=r;
}
}
out << costM << '\n';
return 0;
}