Pagini recente » Cod sursa (job #23647) | Cod sursa (job #1558710) | Cod sursa (job #902646) | Cod sursa (job #2511924) | Cod sursa (job #1155021)
#include <stdio.h>
#include <vector>
#include <queue>
#define NMAX 505
#define INF 0x3f3f3f3f
using namespace std;
vector<int> G[NMAX];
queue<int> Q;
bool viz[NMAX];
int N,M,START,DEST;
int A[NMAX][NMAX],F[NMAX][NMAX],C[NMAX][NMAX];
int D[NMAX],P[NMAX];
int OK,MIN;
long long VAL;
void read()
{
scanf("%d %d %d %d\n",&N,&M,&START,&DEST);
for (int i=1;i<=M;i++)
{
int x,y,c,cost;
scanf("%d %d %d %d\n",&x,&y,&c,&cost);
G[x].push_back(y);
G[y].push_back(x);
A[x][y]=cost;
A[y][x]=-cost;
C[x][y]=c;
}
}
int BF()
{
for (int i=1;i<=N;i++)
{
D[i]=INF;
P[i]=0;
viz[i]=0;
}
Q.push(START);
D[START]=0;
viz[START]=1;
while (!Q.empty())
{
int node=Q.front();
Q.pop();
viz[node]=0;
for (int i=0;i<G[node].size();i++)
if (F[node][G[node][i]]<C[node][G[node][i]] && D[node]+A[node][G[node][i]]<D[G[node][i]])
{
D[G[node][i]]=D[node]+A[node][G[node][i]];
P[G[node][i]]=node;
if (!viz[G[node][i]])
{
viz[G[node][i]]=1;
Q.push(G[node][i]);
}
}
}
if (D[DEST]!=INF)
{
OK=1;
MIN=INF;
for (int i=DEST;i!=START;i=P[i])
MIN=min(MIN,C[P[i]][i]-F[P[i]][i]);
for (int i=DEST;i!=START;i=P[i])
{
F[P[i]][i]+=MIN;
F[i][P[i]]-=MIN;
}
return MIN*D[DEST];
}
return 0;
}
void solve()
{
OK=1;
while (OK)
{
OK=0;
VAL+=BF();
}
printf("%lld\n",VAL);
}
int main()
{
freopen ("fmcm.in","r",stdin);
freopen ("fmcm.out","w",stdout);
read();
solve();
fclose(stdin);
fclose(stdout);
return 0;
}