Pagini recente » Cod sursa (job #1863414) | Cod sursa (job #1543584) | Cod sursa (job #1000331) | Cod sursa (job #40632) | Cod sursa (job #2752997)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
const int nmax=350, inf=(1<<30)-1;
vector <int> g[nmax+1];
int f[nmax+1][nmax+1], c[nmax+1][nmax+1], t[nmax+1];
queue <int> q;
int d[nmax+1];
struct str{
int x,y;
str(int x,int y);
};
str::str(int x, int y){
this->x=x;
this->y=y;
}
struct cmp{
bool operator()(str x, str y){
return x.y>y.y;
}
};
priority_queue <str, vector<str>, cmp> h;
int main(){
int n,m,sursa,destinatie;
fin>>n>>m>>sursa>>destinatie;
for(int i=1;i<=m;i++){
int x,y,z,t;
fin>>x>>y>>z>>t;
g[x].push_back(y);
g[y].push_back(x);
f[x][y]=z;
c[x][y]=t;
c[y][x]=-t;
}
for(int i=1;i<=n;i++){
d[i]=inf;
}
d[sursa]=0;
q.push(sursa);
while(q.empty()==0){
int x=q.front();
q.pop();
for(int i=0;i<int(g[x].size());i++){
int xn=g[x][i];
if(f[x][xn]>0&&d[xn]>d[x]+c[x][xn]){
d[xn]=d[x]+c[x][xn];
q.push(xn);
}
}
}
int dif=0,sol=0;
while(d[destinatie]!=inf){
for(int x=1;x<=n;x++){
for(int i=0;i<int(g[x].size());i++){
int xn=g[x][i];
if(d[x]!=inf&&d[xn]!=inf){
c[x][xn]=c[x][xn]+d[x]-d[xn];
}
}
}
dif+=d[destinatie];
for(int i=1;i<=n;i++){
d[i]=inf;
}
d[sursa]=0;
h.push(str(sursa,0));
while(h.empty()==0){
int x=h.top().x;
int y=h.top().y;
h.pop();
if(y==d[x]){
for(int i=0;i<int(g[x].size());i++){
int xn=g[x][i];
if(f[x][xn]>0&&d[xn]>d[x]+c[x][xn]){
t[xn]=x;
d[xn]=d[x]+c[x][xn];
h.push(str(xn,d[xn]));
}
}
}
}
if(d[destinatie]!=inf){
int mini=inf;
for(int i=destinatie;i!=sursa;i=t[i]){
if(mini>f[t[i]][i]){
mini=f[t[i]][i];
}
}
for(int i=destinatie;i!=sursa;i=t[i]){
f[t[i]][i]-=mini;
f[i][t[i]]+=mini;
}
sol+=(d[destinatie]+dif)*mini;
}
}
fout<<sol<<"\n";
return 0;
}