Pagini recente » Cod sursa (job #2940220) | Cod sursa (job #40615) | Cod sursa (job #2202978) | Cod sursa (job #2878715) | Cod sursa (job #757683)
Cod sursa(job #757683)
#include <queue>
#include <fstream>
#include <cstdlib>
using namespace std;
typedef pair<int, int> pr;
const int N_MAX=1011;
const int oo=1<<29;
vector<pr> G[N_MAX];
vector<pr>::const_iterator it, iend;
int C[N_MAX][N_MAX], F[N_MAX][N_MAX];
int d[N_MAX], f[N_MAX];
bool isInQue[N_MAX];
struct cmp {
bool operator() (const int& x, const int& y) const {return d[x] > d[y];}
};
priority_queue<int, vector<int>, cmp> pQ;
inline int _min(int x, int y) {return x <= y ? x : y;}
bool findPath(int start, int end, int N)
{
int x;
for(x=1; x <= N; ++x)
f[x]=-1, d[x]=oo, isInQue[x]=false;
f[start]=-2; d[start]=0;
for(pQ.push(start); !pQ.empty();)
{
x=pQ.top(); pQ.pop();
isInQue[x]=false;
for(it=G[x].begin(), iend=G[x].end(); it <iend; ++it)
{
if(start == it->first)
continue;
if(d[it->first] > d[x] + it->second && C[x][it->first] > F[x][it->first])
{
d[it->first]=it->second + d[x];
f[it->first]=x;
if(false == isInQue[it->first])
{
pQ.push(it->first);
isInQue[it->first]=true;
}
}
}
}
return -1 != f[end] && oo != d[end];
}
int main()
{
int N, M, s, e, x, y, c, cMin, sum=0;
ifstream in("fmcm.in");
ofstream out("fmcm.out");
for(in>>N>>M>>s>>e; M; --M)
{
in>>x>>y;
in>>C[x][y]>>c;
G[x].push_back(pr(y, c));
G[y].push_back(pr(x, -c));
}
while(findPath(s, e, N))
{
cMin=oo;
for(x=N; -2 != f[x]; x=f[x])
cMin=_min(cMin, C[f[x]][x] - F[f[x]][x]);
for(x=N; -2 != f[x]; x=f[x])
{
F[f[x]][x]+=cMin;
F[x][f[x]]-=cMin;
}
sum+=cMin*d[e];
}
out<<sum<<'\n';
return EXIT_SUCCESS;
}