Pagini recente » Cod sursa (job #3361006) | Cod sursa (job #3361012) | Cod sursa (job #3361016) | Cod sursa (job #3361008) | Cod sursa (job #3361013)
#include <fstream>
#include <vector>
#include <climits>
#include <queue>
#include <algorithm>
#include <bitset>
using namespace std;
const int NMAX = 350;
using ll = long long;
const ll INF = LLONG_MAX;
const int FMAX = 10005;
ifstream cin("fmcm.in");
ofstream cout("fmcm.out");
struct muchie{
int nod;
int cost;
int flux, cap;
int pereche;
};
vector <vector <muchie>> v;
void addedge(int a, int b, int cap, int cost) {
v[a].push_back({b, cost, 0, cap, (int)v[b].size()});
v[b].push_back({a, -cost, 0, 0, (int)(v[a].size() - 1)});
}
int n, sursa, dest;
ll ans = 0;
ll p[NMAX + 2];
bitset <NMAX + 2> incoada;
void bellmanFord() { ///calc potentialele
for(int i = 1; i <= n; i++)
p[i] = INF;
queue <int> q;
incoada[sursa] = 1;
p[sursa] = 0;
q.push(sursa);
while(!q.empty()) {
int now = q.front();
incoada[now] = 0;
q.pop();
for(int i = 0; i < v[now].size(); i++) {
muchie x = v[now][i];
if(x.flux < x.cap && p[x.nod] > p[now] + x.cost) {
p[x.nod] = p[now] + x.cost;
if(!incoada[x.nod]) {
incoada[x.nod] = 1;
q.push(x.nod);
}
}
}
}
}
struct noduri{
ll dist;
int tata;
int edge;
}a[NMAX + 2];
struct prio{
int nod;
ll dist;
bool operator <(const prio& rhs) const { ///dist min
return dist > rhs.dist;
}
};
bool dijkstra() {
for(int i = 1; i <= n; i++) {
a[i].dist = INF;
a[i].tata = 0;
a[i].edge = -1;
}
a[sursa].dist = 0;
priority_queue <prio> pq;
pq.push({sursa, 0});
while(!pq.empty()) {
prio now = pq.top();
pq.pop();
if(a[now.nod].dist < now.dist) ///deja l-am analizat
continue;
// cout << "Suntem la " << now.nod << " " << now.dist << '\n';
for(int i = 0; i < v[now.nod].size(); i++) {
muchie x = v[now.nod][i];
if(x.flux < x.cap && a[x.nod].dist > now.dist + x.cost - p[x.nod] + p[now.nod]) {
a[x.nod].dist = now.dist + x.cost - p[x.nod] + p[now.nod];
a[x.nod].tata = now.nod;
a[x.nod].edge = i;
pq.push({x.nod, a[x.nod].dist});
}
}
}
return (a[dest].dist != INF);
}
int recons() {
int minn = FMAX;
int nod = dest;
while(nod != sursa) {
int urm = a[nod].tata, id = a[nod].edge;
minn = min(minn, v[urm][id].cap - v[urm][id].flux);
nod = urm;
}
return minn;
}
void change(int a, int id, int cat) {
v[a][id].flux += cat;
v[v[a][id].nod][v[a][id].pereche].flux -= cat;
}
void update(int add) {
int nod = dest;
while(nod != sursa) {
int urm = a[nod].tata, id = a[nod].edge;
change(urm, id, add);
nod = urm;
}
}
int main() {
int m;
cin >> n >> m >> sursa >> dest;
v.resize(n + 1);
for(int i = 1; i <= m; i++) {
int a, b, cap, cost;
cin >> a >> b >> cap >> cost;
addedge(a, b, cap, cost);
}
/*for(int i = 1; i <= n; i++) {
cout << "Nodul " << i << '\n';
for(auto x : v[i])
cout << x.nod << " " << x.cost << " " << x.flux << " " << x.cap << " " << x.pereche << '\n';
}*/
bellmanFord();
///sursa = 1, dest = n
while(dijkstra()) {
// cout << "o da\n";
int add = recons(); ///cat flux
ans += 1LL * add * (a[dest].dist + p[dest]); /// pt ca se ANULEAZA celelalte potentiale si il pastrezi doar pe ult
update(add);
///Updatezi potentialele
for(int i = 1; i <= n; i++)
p[i] += a[i].dist;
}
cout << ans;
return 0;
}