Pagini recente » Cod sursa (job #2794175) | Cod sursa (job #2968374) | Cod sursa (job #1760273) | Cod sursa (job #2687090) | Cod sursa (job #2956165)
#include <vector>
#include <queue>
#include <fstream>
#include <climits>
#include <iostream>
using namespace std;
ifstream fileIn("cuplaj.in");
ofstream fileOut("cuplaj.out");
class Graph {
protected:
int N;
int M;
vector<int> prev_in_bfs;
vector<vector<int>> list_adj;
//vector<bool> visited;
vector<vector<int>> capacities;
public:
void read();
void initialize(int k, int initial_capacity = 0);
int bfs(int s, int t);
int maxflow(int source, int target);
int getNoNodes(){
return N;
}
};
void Graph::initialize(int k, int initial_capacity) {
list_adj.resize(k);
//visited.resize(k);
prev_in_bfs.resize(k);
capacities.resize(k);
for(int i = 0; i < (int) capacities.size(); ++i) {
capacities[i].resize(k, initial_capacity);
}
}
void Graph:: read() {
fileIn >> N >> M;
initialize(N+1);
int a, b, c;
for(int i=1; i<= M; i++) {
fileIn >> a >> b >> c;
list_adj[a].push_back(b);
list_adj[b].push_back(a);
capacities[a][b] = c;
}
}
int Graph::bfs(int s, int t) {
//filling prev and visited vectors with default values
fill(prev_in_bfs.begin(), prev_in_bfs.end(), -2);
//fill(visited.begin(), visited.end(), false);
// queue for bfs
queue<pair<int,int>> q_bfs;
//visiting the start node and adding to the queue
q_bfs.push({s, INT_MAX});
//visited[s] = true;
prev_in_bfs[s] = -1;
int bottleneck;
while(!q_bfs.empty()) {
auto curr = q_bfs.front();
int curr_node = curr.first;
int curr_flow = curr.second;
q_bfs.pop();
for( auto node: list_adj[curr_node]) {
// for every adj node
int flow = capacities[curr_node][node];
if(prev_in_bfs[node] == -2 && flow > 0) { // if the node it s not visited in this bfs and the edge have capacity
//we visit it and set its prev node
//visited[node] = true;
prev_in_bfs[node] = curr_node;
//we calculate bottleneck as minimum on the current path from source to target
bottleneck = min(curr_flow, flow);
if (node == t) {
return bottleneck;
}
q_bfs.push({node, bottleneck});
}
}
}
return 0;
}
int Graph::maxflow(int source, int target) {
int flow = 0;
int bottleneck;
bottleneck = bfs(source,target);
while(bottleneck !=0) {
flow += bottleneck;
// traverse the path in reverse order to update capacity
for(int curr_node = target; curr_node != source; curr_node = prev_in_bfs[curr_node]) {
capacities[prev_in_bfs[curr_node]][curr_node] -= bottleneck; //decreasing forward edge capacity
capacities[curr_node][prev_in_bfs[curr_node]] += bottleneck; // increasing the reversed edge capacity
}
bottleneck = bfs(source, target);
}
return flow;
}
class Cuplaj: public Graph {
int E;
public:
void read();
void display_path();
};
void Cuplaj ::read() {
fileIn >> N >> M >> E;
initialize(N+ M + 2, 0);
int a, b;
for(int i = 1; i<= E; ++i) {
fileIn >> a >> b;
list_adj[0].push_back(a);
list_adj[a].push_back(0);
list_adj[a].push_back(N + b);
list_adj[N+b].push_back(a);
list_adj[N+M+1].push_back(N+b);
list_adj[N + b].push_back(N + M + 1);
capacities[0][a] = 1;
capacities[a][N + b] = 1;
capacities[N + b][N + M + 1] = 1;
}
}
void Cuplaj::display_path() {
int start = 0, target = N + M + 1;
fileOut << maxflow(start, target)<<'\n';
queue<int> q;
q.push(start);
for(auto elem_first_partition: list_adj[start]) {
for(auto elem_snd_partition: list_adj[elem_first_partition]){
if (elem_snd_partition == start) {
continue;
}
if (capacities[elem_first_partition][elem_snd_partition] == 0) {
fileOut << elem_first_partition << ' ' <<elem_snd_partition - N<< '\n';
capacities[elem_first_partition][elem_snd_partition]++;
break;
}
}
}
}
int main() {
Cuplaj my_graph;
my_graph.read();
my_graph.display_path();
return 0;
}