Pagini recente » Cod sursa (job #2148714) | Cod sursa (job #1133787) | Cod sursa (job #3217540) | Cod sursa (job #2955150) | Cod sursa (job #968548)
Cod sursa(job #968548)
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <ctime>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
ifstream ff("fmcm.in");
ofstream gg("fmcm.out");
#define max 352
vector<int> vv[max];
int n, m, s, d, cc[max][max], zz[max][max], qq[2][max], q0, q1, ww[max];
int dd[max], tt[max], rr[max][max], aa[max], bb[max];
bool bel(){
int x0=0, x1=1, a, b, l;
q0=q1=0;
memset(ww, 0, sizeof(ww));
for(int i=1;i<=n;i++) dd[i]=0xfffffff;
qq[x0][q0++]=s;
ww[s]=1;
dd[s]=0;
for(int k=1;k<=n;k++){
while(q0>0){
a=qq[x0][--q0];
if(a==d) continue;
l=vv[a].size();
for(int i=0;i<l;i++){
b=vv[a][i];
if(dd[b]>dd[a]+zz[a][b] && rr[a][b]<cc[a][b]){
dd[b]=dd[a]+zz[a][b];
if(ww[b]!=k) qq[x1][q1++]=b;
ww[b]=k;
tt[b]=a;
}
}
}
x0^=1; x1^=1; swap(q0, q1);
}
return ww[d];
}
struct per{ int v, x; per(int v0=0, int x0=0){v=v0;x=x0;}
bool operator()(const per &a, const per &b){ return a.v>b.v; }
};
priority_queue<per, vector<per>, per> hh;
bool dij(){
per a;
int x, y, c, v;
for(int i=1;i<=n;i++) aa[i]=0xfffffff;
aa[s]=0; bb[s]=0;
hh.push(per(0,s));
while(!hh.empty()){
a=hh.top(); hh.pop();
x=a.x;
c=a.v;
if(aa[x]!=c) continue;
if(x==d) continue;
for(int i=0;i<(int)vv[x].size();i++){
y=vv[x][i];
if(rr[x][y]<cc[x][y]){
v=aa[x]+zz[x][y]+dd[x]-dd[y];
if(v<aa[y]){
aa[y]=v;
tt[y]=x;
bb[y]=bb[x]+zz[x][y];
hh.push(per(aa[y], y));
}
}
}
}
memcpy(dd, bb, sizeof(bb));
return aa[d]!=0xfffffff;
}
void flu(){
int r=0, c;
bel();
while(dij()) {
c=0xfffffff;
for(int x=d;x!=s;x=tt[x])c=min(c, cc[tt[x]][x]-rr[tt[x]][x]);
if(c!=0){
for(int x=d;x!=s;x=tt[x]){
rr[tt[x]][x] += c;
rr[x][tt[x]] -= c;
r+=c*zz[tt[x]][x];
}
}
}
gg << r << "\n";
}
int main(){
int a, b, c, z;
ff >> n >> m >> s >> d;
for(int i=0;i<m;i++){
ff >> a >> b >> c >> z;
vv[a].push_back(b);
vv[b].push_back(a);
cc[a][b]=c;
zz[a][b]=z;
zz[b][a]=-z;
}
flu();
}