Pagini recente » Cod sursa (job #1432826) | Cod sursa (job #320485) | Cod sursa (job #214295) | Cod sursa (job #1608490) | Cod sursa (job #2035122)
#include <bits/stdc++.h>
#define Nmax 351
#define tip pair <int,int>
using namespace std;
list <int> G[Nmax];
int cap[Nmax][Nmax];
int cost[Nmax][Nmax];
int c_bf[Nmax];
bitset <Nmax> inq;
int dist[Nmax];
int real_d[Nmax];
int t[Nmax];
queue <int> q;
int n,s,d,ans,minn;
priority_queue <tip, vector <tip>, greater <tip> > pq;
bool Dijkstra()
{
memset(dist,0x3f,sizeof(dist));
dist[s]=0;
real_d[s]==0;
pq.push({dist[s],s});
int x,dij_cst,new_dist;
list <int> :: iterator it;
while(!pq.empty())
{
x=pq.top().second;
dij_cst=pq.top().first;
pq.pop();
if(dist[x]==dij_cst)
for(it=G[x].begin();it!=G[x].end();it++)
if(cap[x][*it])
{
new_dist=dist[x]+cost[x][*it]+c_bf[x]-c_bf[*it];
if(new_dist<dist[*it])
{
dist[*it]=new_dist;
real_d[*it]=real_d[x]+cost[x][*it];
t[*it]=x;
pq.push({dist[*it],*it});
}
}
}
memcpy(c_bf,real_d,sizeof(dist));
if(dist[d]==0x3f3f3f3f) return false;
minn=0x3f3f3f3f;
x=d;
while(x!=s)
{
if(cap[t[x]][x]<minn)
minn=cap[t[x]][x];
x=t[x];
}
ans+=(minn*real_d[d]);
x=d;
while(x!=s)
{
cap[t[x]][x]-=minn;
cap[x][t[x]]+=minn;
x=t[x];
}
return true;
}
void BF()
{
memset(c_bf,0x3f,sizeof(c_bf));
c_bf[s]=0;
q.push(s);
inq[s]=true;
int x;
list <int> :: iterator it;
while(!q.empty())
{
x=q.front();
q.pop();
inq[x]=false;
for(it=G[x].begin();it!=G[x].end();it++)
if(cap[x][*it] and c_bf[*it]>c_bf[x]+cost[x][*it])
{
c_bf[*it]=c_bf[x]+cost[x][*it];
if(!inq[*it])
{
q.push(*it);
inq[*it]=true;
}
}
}
}
int main()
{
freopen("fmcm.in","rt",stdin);
freopen("fmcm.out","wt",stdout);
int m,i,j,z,cst,k;
scanf("%d%d%d%d",&n,&m,&s,&d);
for(k=1;k<=m;k++)
{
scanf("%d%d%d%d",&i,&j,&z,&cst);
G[i].push_back(j);
G[j].push_back(i);
cap[i][j]=z;
cost[i][j]=cst;
cost[j][i]=-cst;
}
BF();
ans=0;
while(Dijkstra());
printf("%d",ans);
return 0;
}