Pagini recente » Cod sursa (job #884744) | Cod sursa (job #1622949) | Cod sursa (job #1964585) | Cod sursa (job #941056) | Cod sursa (job #2247340)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define DIM 353
#define INF 1e9
using namespace std;
ifstream in ("fmcm.in");
ofstream out("fmcm.out");
int nodeNum, edgeNum, source, destination, node1, node2, totalCost;
int frequency[DIM], father[DIM], dist[DIM];
int capacity[DIM][DIM], flow[DIM][DIM], cost[DIM][DIM];
vector<int> graph[DIM];
queue<int> q;
bitset<DIM> visited;
bool BF(){
while(!q.empty())
q.pop();
q.push(source);
for(int i = 1; i <= nodeNum; ++ i){
dist[i] = INF;
father[i] = 0;
}
dist[source] = 0;
visited.reset();
visited[source] = true;
bool withoutCycle = true;
while(!q.empty() && withoutCycle){
int currentNode = q.front();
q.pop();
visited[currentNode] = false;
for(auto nextNode : graph[currentNode]){
if(capacity[currentNode][nextNode] - flow[currentNode][nextNode] > 0 && dist[nextNode] > dist[currentNode] + cost[currentNode][nextNode]){
dist[nextNode] = dist[currentNode] + cost[currentNode][nextNode];
++ frequency[nextNode];
father[nextNode] = currentNode;
if(!visited[nextNode]){
visited[nextNode] = 1;
q.push(nextNode);
}
}
}
}
return father[destination];
}
int main()
{
in>>nodeNum>>edgeNum>>source>>destination;
for(int edgeCnt = 1; edgeCnt <= edgeNum; ++ edgeCnt){
in>>node1>>node2;
in>>capacity[node1][node2]>>cost[node1][node2];
cost[node2][node1] = -cost[node1][node2];
graph[node1].push_back(node2);
graph[node2].push_back(node1);
}
while(BF()){
int currentNode = destination;
int minFlow = INF;
while(currentNode != source){
minFlow = min(minFlow, capacity[father[currentNode]][currentNode] - flow[father[currentNode]][currentNode]);
currentNode = father[currentNode];
}
currentNode = destination;
while(currentNode != source){
flow[currentNode][father[currentNode]] -= minFlow;
flow[father[currentNode]][currentNode] += minFlow;
totalCost += cost[father[currentNode]][currentNode] * minFlow;
currentNode = father[currentNode];
}
}
out<<totalCost;
return 0;
}
//////////////////////////////////////
/*
ti se da posibilitatea sa alegi 0 sau 1
ai 10 alegeri
in fiecare alegere doar un raspuns este corect dar iti este necunoscut
care are probabilitatea mai mare de a avea mai multe raspunsuri corecte:
sa alegi aceeasi varianta pentru toate cele 10 incercari
sa alegi cu o sansa de 50% intre 0 si 1 de fiecare data(rand)
*/
//////////////////////////////////////