Pagini recente » Cod sursa (job #2124948) | Cod sursa (job #323801) | Cod sursa (job #2855675) | Cod sursa (job #582037) | Cod sursa (job #3136851)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
const int maxn = 1005, maxm = 5005, inf = 1 << 30;
int s, d;
int n, m, x, y, z, ans;
vector <int> nod[maxn];
// queue <int> q;
int cost[maxn];
class cmp {
public:
bool operator()(int a, int b) {
return cost[a] > cost[b];
}
};
priority_queue <int, vector<int>, cmp> q;
int bestcost = 0;
bool is[maxn];
int p[maxn];
int c[maxn][maxn], us[maxn][maxn], costs[maxn][maxn];
int dd[maxn], dp2[maxn];
bool dijkstra() {
memset(is, false, sizeof(is));
memset(cost, 1<<30, sizeof(cost));
while(!q.empty()) {
q.pop();
}
q.push(s);
is[s] = true;
cost[s] = 0;
while(!q.empty()) {
int x = q.top();
q.pop();
if(x == n) { continue; }
for(auto u : nod[x]) {
if(((is[u] == false || cost[x] + costs[x][u] + dd[x] - dd[u] < cost[u]) && us[x][u] != c[x][u])) {
p[u] = x;
if(is[u] == false)
q.push(u);
is[u] = true;
cost[u] = cost[x] + costs[x][u] + dd[x] - dd[u];
dp2[u] = dp2[x] + costs[x][u];
}
}
}
// if(cost[d] < bestcost)
// bestcost = cost[d];
return is[d];
}
void spfa() {
queue<int> Q;
is[s] = 1;
memset(dd, 0x3F, sizeof(dd));
dd[s] = 0;
Q.emplace(s);
while (!Q.empty()) {
int node = Q.front();
Q.pop();
is[node] = 0;
for (auto x : nod[node])
if (c[node][x] && dd[node] + costs[node][x] < dd[x]) {
dd[x] = dd[node] + costs[node][x];
if (!is[x]) {
is[x] = 1;
Q.emplace(x);
}
}
}
}
int main()
{
int i;
f >> n >> m >> s >> d;
for(i = 1; i <= m; i ++) {
int cst;
f >> x >> y >> z >> cst;
nod[x].push_back(y);
nod[y].push_back(x);
c[x][y] = z;
costs[x][y] = cst;
costs[y][x] = -cst;
}
spfa();
while(dijkstra()) {
// for(auto u : nod[d]) {
// if(us[u][d] == c[u][d] || is[u] == false) {
// continue;
// }
// p[d] = u;
// int minim = inf;
// for(int nd = d; nd != s; nd = p[nd]) {
// minim = min(minim, c[p[nd]][nd] - us[p[nd]][nd]);
// }
// for(int nd = d; nd != s; nd = p[nd]) {
// us[p[nd]][nd] += minim;
// us[nd][p[nd]] -= minim;
// }
// ans += minim;
// bestcost += minim * cost[d];
// }
int minim = inf;
for(int nd = d; nd != s; nd = p[nd]) {
minim = min(minim, c[p[nd]][nd] - us[p[nd]][nd]);
}
for(int nd = d; nd != s; nd = p[nd]) {
us[p[nd]][nd] += minim;
us[nd][p[nd]] -= minim;
}
ans += minim;
bestcost += minim * dp2[d];
memcpy(dd, dp2, sizeof(dp2));
}
// g << ans << '\n';
g << bestcost << '\n';
f.close(); g.close();
return 0;
}