Pagini recente » Cod sursa (job #1450402) | Cod sursa (job #2908473) | Cod sursa (job #3137786) | Cod sursa (job #2930910) | Cod sursa (job #2960575)
#include <bits/stdc++.h>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
vector<vector<int>>adj(1200);
vector<int>costBellmanFord(1200);
vector<int>costDjikstra(1200);
vector<int>costuri(1200);
int fluxx,MaxFLUX,n,m,i,j,k,s,d,a[1001][1001],mat[1001][1001],fth[1001],cost[1001][1001],x,y,z,flux,FLUX,COST;
void bFord()
{
queue<int>q;
vector<bool>in_queue(500,false);
costBellmanFord.assign(sizeof(costBellmanFord),10e7);
q.push(s);
in_queue[s] = true;
costBellmanFord[s] = 0;
while(!q.empty())
{
auto nod = q.front();
q.pop();
in_queue[nod] = false;
for(int i=0;i<adj[nod].size();i++)
{
auto next_node = adj[nod][i];
auto cost_next_node = cost[nod][next_node];
auto flux_next_node = a[nod][next_node];
if(flux_next_node)
{
if(costBellmanFord[next_node] > costBellmanFord[nod] + cost_next_node)
{
costBellmanFord[next_node] = costBellmanFord[nod] + cost_next_node;
if(in_queue[next_node] == false)
{
q.push(next_node);
in_queue[next_node] = true;
}
}
}
}
}
}
bool Djikstra()
{
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
costDjikstra.assign(500,10e6);
costDjikstra[s] = 0;
costuri[s] = 0;
q.push({0,s});
while(!q.empty())
{
auto pair = q.top();
q.pop();
auto cost_node = pair.first;
auto nodd = pair.second;
if(costDjikstra[nodd] == cost_node)
{
for(int j=0;j<adj[nodd].size();j++)
{
auto next_node = adj[nodd][j];
auto cost_next_node = adj[nodd][next_node];
auto flux_next_node = a[nodd][next_node];
if(flux_next_node)
{
if(costDjikstra[next_node] > costDjikstra[nodd] + cost_next_node + costBellmanFord[nodd] - costBellmanFord[next_node])
{
costDjikstra[next_node] = costDjikstra[nodd] + cost_next_node + costBellmanFord[nodd] - costBellmanFord[next_node];
q.push({costDjikstra[next_node],next_node});
costuri[next_node] = costuri[nodd] + cost_next_node;
fth[next_node] = nodd;
}
}
}
}
}
for(int i=1;i<=n;i++)
{
costBellmanFord[i] = costuri[i];
}
if(costDjikstra[d] == 10e7)
return false;
int flux_now = 10e7;
int cost_now = 0;
int noddd = d;
while(noddd)
{
flux_now = min(flux_now,a[fth[noddd]][noddd]);
cost_now += cost[fth[noddd]][noddd];
noddd = fth[noddd];
}
noddd = d;
while(noddd != s)
{
a[fth[noddd]][noddd] -= flux_now;
a[noddd][fth[noddd]] += flux_now;
noddd = fth[noddd];
}
COST += flux_now * costuri[d];
FLUX += flux_now;
return true;
}
int main() {
f>>n>>m>>s>>d;
for(int i=0;i<m;i++)
{
f>>x>>y>>flux>>z;
adj[x].push_back(y);
adj[y].push_back(x);
a[x][y] = flux;
cost[x][y] = z;
cost[y][x] = -z;
}
bFord();
while(Djikstra())
{
g<<COST;
}
return 0;
}