Pagini recente » Cod sursa (job #181501) | Cod sursa (job #2564392) | Cod sursa (job #417838) | Cod sursa (job #1007134) | Cod sursa (job #975839)
Cod sursa(job #975839)
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
#include <algorithm>
#define newn a[x][i]
using namespace std;
const int N = 355;
const int oo = 0x3f3f3f3f;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
int n, m, s, d, sol, c[N][N], cst[N][N], oldd[N], dist[N], real[N], t[N];
typedef pair <int, int> nod;
priority_queue < nod, vector<nod>, greater<nod> > heap;
vector <int> a[N];
inline bool Dijkstra()
{
memset(dist, oo, sizeof(dist));
dist[s] = real[s] = 0;
heap.push(nod(0, s));
while(!heap.empty())
{
int x = heap.top().second, val = heap.top().first; heap.pop();
if(dist[x] != val) continue;
for(unsigned i=0; i<a[x].size(); i++)
if(c[x][newn])
{
int cost = dist[x] + cst[x][newn] + oldd[x] - oldd[newn];
if(cost < dist[newn])
{
t[newn] = x;
dist[newn] = cost;
real[newn] = real[x] + cst[x][newn];
heap.push(nod(dist[newn], newn));
}
}
}
memcpy(oldd, real, sizeof(oldd));
return (dist[d] != oo);
}
void Update_flux()
{
int fmin = oo;
for(int j=d; j!=s; j=t[j])
fmin = min(fmin, c[t[j]][j]);
for(int j=d; j!=s; j=t[j])
{
c[t[j]][j] -= fmin;
c[j][t[j]] += fmin;
}
sol += fmin * real[d];
}
int main()
{
fin>>n>>m>>s>>d;
while(m--)
{
int x, y, f, z;
fin>>x>>y>>f>>z;
a[x].push_back(y);
a[y].push_back(x);
c[x][y] = f;
cst[x][y] = z;
cst[y][x] = -z;
}
while(Dijkstra())
Update_flux();
fout<<sol;
return 0;
}