Pagini recente » Cod sursa (job #1499618) | Cod sursa (job #1840530) | Cod sursa (job #2670016) | Cod sursa (job #1100828) | Cod sursa (job #1374286)
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
#define Nmax 355
#define inf 0x3f3f3f3f
using namespace std;
ifstream in("fmcm.in");
ofstream out("fmcm.out");
int D[Nmax], D2[Nmax];
struct cmp
{
//bool operator() (const int &x, const int &y)
bool operator() (const pair<int, int> &x, const pair<int, int> &y)
{
//return D2[x] > D2[y];
return D2[x.first] > D2[y.first];
}
};
//priority_queue<int, vector<int>, cmp> PQ;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater <pair <int, int> > > PQ;
vector<int> G[Nmax];
int Capacity[Nmax][Nmax], Cost[Nmax][Nmax], parent[Nmax], realD[Nmax];
bool dijkstra(int source, int destination)
{
bool inQueue[Nmax];
memset(inQueue, false, sizeof(inQueue));
memset(D2, inf, sizeof(D2));
inQueue[source] = true;
D2[source] = 0;
PQ.push(make_pair(0, source));
//PQ.push(source);
while (!PQ.empty())
{
//int node = PQ.top();
int cost = PQ.top().first;
int node = PQ.top().second;
PQ.pop();
//inQueue[node] = false;
if (node == destination) continue;
if (D2[node] != cost) continue;
for (int i = 0; i < G[node].size(); i++)
{
int nextNode = G[node][i];
int newCost = D2[node] + D[node] - D[nextNode] + Cost[node][nextNode];
if (Capacity[node][nextNode] && newCost < D2[nextNode])
{
parent[nextNode] = node;
D2[nextNode] = newCost;
realD[nextNode] = realD[node] + Cost[node][nextNode];
//if (!inQueue[nextNode])
//{
// PQ.push(nextNode);
PQ.push(make_pair(D2[nextNode], nextNode));
//inQueue[nextNode] = true;
//}
}
}
}
memcpy(D, realD, sizeof(D));
realD[source] = 0;
if (D2[destination] != inf) return true;
return false;
}
void bellmanFord(int source)
{
queue<int> Q;
bool inQueue[Nmax];
memset(D, inf, sizeof(D));
memset(inQueue, false, sizeof(inQueue));
inQueue[source] = true;
D[source] = 0;
Q.push(source);
while (!Q.empty())
{
int node = Q.front();
Q.pop();
for (int i = 0; i < G[node].size(); i++)
{
int nextNode = G[node][i];
int cost = Cost[node][nextNode];
if (Capacity[node][nextNode] && D[node] + cost < D[nextNode])
{
D[nextNode] = D[node] + cost;
if (!inQueue[nextNode])
{
Q.push(nextNode);
inQueue[nextNode] = true;
}
}
}
inQueue[node] = false;
}
}
int main()
{
int N, M, source, destination;
in >> N >> M >> source >> destination;
for (int i = 1; i <= M; i++)
{
int x, y, c, z;
in >> x >> y >> c >> z;
G[x].push_back(y);
G[y].push_back(x);
Capacity[x][y] = c;
Cost[x][y] = z;
Cost[y][x] = -z;
}
bellmanFord(source);
int totalCost = 0;
while (dijkstra(source, destination))
{
int node = destination, flow = inf, cost = 0;
while (node != source)
{
flow = min(flow, Capacity[parent[node]][node]);
node = parent[node];
}
node = destination;
while (node != source)
{
Capacity[parent[node]][node] -= flow;
Capacity[node][parent[node]] += flow;
cost += flow * Cost[parent[node]][node];
node = parent[node];
}
totalCost += cost;
}
out << totalCost << "\n";
}