Pagini recente » Cod sursa (job #3260698) | Cod sursa (job #860616) | album2 | Cod sursa (job #2062790) | Cod sursa (job #770028)
Cod sursa(job #770028)
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define MAX 355
#define INF 0xfffffff
vector<int>g[MAX];
set<int>s1,s2;
int n,m,s,d,c[MAX][MAX],p[MAX][MAX],f[MAX][MAX],dist[MAX],tt[MAX];
bool was[MAX],e[MAX][MAX];
bool drum(){
int x,y;
memset(was,0,sizeof(was));
for(int i=1;i<=n;i++) dist[i] = INF;
dist[s] = 0;
for(int k=1;k<=n;k++)
{
x = 1; while( was[x] )x++;
for(int i=x+1;i<=n;i++) if( dist[i] < dist[x] && !was[i] )x = i;
was[x] = 1;
for(int i=0;i<g[x].size();i++)
{
y = g[x][i];
if( dist[y] > dist[x] + p[x][y] && c[x][y] > f[x][y] && e[x][y])
{
dist[y] = dist[x] + p[x][y];
tt[y] = x;
}
}
}
// for(int i=1;i<=n;i++) printf("%d ",dist[i]);
// return 0;
if( dist[d] < INF/2 )return 1; else return 0;
}
void rezolva(){
int flux, cost = 0;
while( drum() )
{
flux = INF;
for(int x = d;x != s;x = tt[x]) flux = min(flux, c[tt[x]][x] - f[tt[x]][x]);
for(int x = d;x != s;x = tt[x])
{
f[tt[x]][x] += flux;
f[x][tt[x]] -= flux;
cost += flux * p[tt[x]][x];
}
}
printf("%d\n",cost);
}
int main(){
int x,y,z,w;
freopen("fmcm.in","r",stdin);
freopen("fmcm.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&s,&d);
while(m--)
{
scanf("%d %d %d %d",&x,&y,&z,&w);
g[x].push_back(y);
g[y].push_back(x);
e[x][y] = 1;
e[y][x] = 1;
c[x][y] = z;
p[x][y] = w;
p[y][x] = -w;
}
rezolva();
return 0;
}