Pagini recente » Clasament drum-bugetat | test.cex2 | Cod sursa (job #766115) | Cod sursa (job #840747) | Cod sursa (job #757917)
Cod sursa(job #757917)
#include <queue>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
typedef pair<int, int> pr;
const int N_MAX=361;
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 f[N_MAX], d[N_MAX];
bool isInPQue[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;
}
f[start]=-2;
d[start]=0;
for(pQ.push(start); !pQ.empty(); )
{
x=pQ.top(); pQ.pop();
isInPQue[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])
{
f[it->first]=x;
d[it->first]=d[x]+it->second;
if(false == isInPQue[it->first])
{
pQ.push(it->first);
isInPQue[it->first]=true;
}
}
}
}
return -1 != f[end];
}
int main()
{
long long int sum;
int N, M, s, e, x, y, c, cMin;
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));
}
for(sum=0; findPath(s, e, N); sum+=1LL*cMin*d[e])
{
cMin=oo;
for(x=e; -2 != f[x]; x=f[x])
cMin=_min(cMin, C[f[x]][x] - F[f[x]][x]);
for(x=e; -2 != f[x]; x=f[x])
{
F[f[x]][x]+=cMin;
F[x][f[x]]-=cMin;
}
}
out<<sum<<'\n';
return EXIT_SUCCESS;
}