Pagini recente » Cod sursa (job #29483) | Cod sursa (job #1505142) | Cod sursa (job #2302051) | Cod sursa (job #1093536) | Cod sursa (job #1482030)
#include <bits/stdc++.h>
using namespace std;
#define MAXN 355
int n , m , s , D ,aaa;
int x , y ;
int cap[MAXN][MAXN];
int cst[MAXN][MAXN];
int f , fcst ;
int old_d[MAXN];
int inQ[MAXN];
int d[MAXN];
int r_d[MAXN];
int p[MAXN];
vector<int> con[MAXN];
vector<int> :: iterator it;
queue<int> q;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > h;
void bellmanford(){
memset(old_d , 0x3f , sizeof(old_d));
old_d [s] = 0;
inQ [s] = 1;
q.push(s);
for(; !q.empty(); q.pop() ){
int i = q.front();
inQ[i]= 0;
for(it = con[i].begin() ; it != con[i].end(); ++it)
if( cap[i][*it] ){
if( old_d[i] + cst[i][*it] >= old_d[*it] ) continue;
old_d[*it] = old_d[i] + cst[i][*it];
if( inQ[*it] ) continue;
inQ[*it] = 1;
q.push(*it);
}
}
}
bool dijkstra(){
memset(d , 0x3f , sizeof(d));
r_d[s] = 0;
d[s] = 0;
h.push( make_pair( d[s] , s ) );
for( ; !h.empty(); ){
int cost = h.top().first , nod = h.top().second;
h.pop();
if( d[nod] != cost ) continue;
for(it = con[nod].begin() ; it != con[nod].end(); ++it ){
aaa = *it;
if( cap[nod][*it] ){
int new_d = d[nod] + cst[nod][*it] + old_d[nod] - old_d[*it];
if( new_d < d[*it] ){
d[*it] = new_d;
r_d[*it] = r_d[nod] + cst[nod][*it];
p[*it] = nod;
h.push( make_pair(d[*it] , *it) );
}
}
}
}
memcpy(old_d, r_d, sizeof(d));
if (d[D] == 0x3f3f3f3f)
return false;
int Min = 0x3f3f3f3f, curCst = 0;
for (int aux = D; aux != s; aux = p[aux])
Min = min(Min, cap[p[aux]][aux]),
curCst += cst[p[aux]][aux];
f += Min;
fcst += Min * r_d[D];
for (int aux = D; aux != s; aux = p[aux])
cap[p[aux]][aux] -= Min,
cap[aux][p[aux]] += Min;
return true;
}
int main()
{
freopen("fmcm.in" , "r" , stdin);
freopen("fmcm.out", "w" , stdout);
scanf("%d %d %d %d",&n,&m,&s,&D);
for( ; m ; --m ){
scanf("%d %d ", &x,&y);
con[x].push_back(y);
con[y].push_back(x);
scanf("%d %d ",&cap[x][y] , &cst[x][y]);
cst[y][x] = -cst[x][y];
}
bellmanford();
while(true){
if( dijkstra() ) continue;
break;
}
printf("%d\n", fcst);
return 0;
}