Pagini recente » Cod sursa (job #1693236) | Cod sursa (job #633400) | Cod sursa (job #649290) | Cod sursa (job #1703138) | Cod sursa (job #701983)
Cod sursa(job #701983)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N=351,inf=1<<30;
int cap[N][N],cost[N][N],flux[N][N],dist[N],T[N],n;
class compare
{
public:
bool operator()(const int &a,const int &b)
{
return dist[a]<dist[b];
}
};
queue <int> Q;
vector<int> a[N];
ifstream in("fmcm.in");
ofstream out("fmcm.out");
inline int dif(int x,int y)
{
return cap[x][y]-flux[x][y];
}
bool bf(int x,int D)
{
for (int i=1;i<=n;i++)
dist[i]=inf;
while (!Q.empty())
Q.pop();
dist[x]=0;
Q.push(x);
while (!Q.empty())
{
x=Q.front();Q.pop();
if (x==D)
continue;
for (vector<int>::iterator i=a[x].begin();i!=a[x].end();i++)
if (dif(x,*i)>0 && dist[x]+cost[x][*i]<dist[*i])
{
dist[*i]=dist[x]+cost[x][*i];
T[*i]=x;
Q.push(*i);
}
}
return dist[D]!=inf;
}
int flow(int S,int D)
{
int C=0,M;
while (bf(S,D))
{
M=inf;
for (int i=D;i!=S;i=T[i])
M=min(M,dif(T[i],i));
if (!M)
break;
for (int i=D;i!=S;i=T[i])
{
flux[T[i]][i]+=M;
flux[i][T[i]]-=M;
}
C+=M*dist[D];
}
return C;
}
int main()
{
int m,x,y,c,z,S,D;
in>>n>>m>>S>>D;
while (m--)
{
in>>x>>y>>c>>z;
a[x].push_back(y);
a[y].push_back(x);
cap[x][y]=c;
cost[x][y]=z;
cost[y][x]=-z;
}
out<<flow(S,D)<<"\n";
return 0;
}