Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define vi vector<int>
- #define vll vector<long long int>
- int n, m;
- bool bfs(int s, int t,vi parent, vector<vector<int>> &residual_graph)
- {
- vector<bool> visited(n, false);
- vi queue;
- queue.push_back(s);
- visited[s] = true;
- parent[s] = -1;
- while(!queue.empty()){
- int u = queue[queue.size()-1];
- queue.pop_back();
- for(int v=0;v<n;v++){
- if(visited[v] == false && residual_graph[u][v]>0){
- if(v==t){
- parent[v] = u;
- return true;
- }
- queue.push_back(v);
- parent[v] = u;
- visited[v] = true;
- }
- }
- }
- return false;
- }
- int ffs(int s, int t, vi parent, vector<vector<int>> &residual_graph)
- {
- int max_flow = 0;
- while (bfs(s, t,parent,residual_graph))
- {
- int path_flow = INT_MAX;
- int v = t;
- while (v != s)
- {
- int u = parent[v];
- path_flow = std::min(path_flow, residual_graph[u][v]);
- v = parent[v];
- }
- v = t;
- while (v != s)
- {
- ll u = parent[v];
- residual_graph[u][v] -= path_flow;
- residual_graph[v][u] -= path_flow;
- v = parent[v];
- }
- max_flow += path_flow;
- }
- return max_flow;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- std::ios::sync_with_stdio(false);
- cin >> n >> m;
- vector<vector<int>> graph(n, vector<int>(n, 0));
- int u, v, c;
- vi parent(n, -1);
- for (int i = 0; i < m; i++)
- {
- cin >> u >> v >> c;
- graph[u - 1][v - 1] = c;
- }
- vi pairs;
- int t, s, d;
- cin >> t;
- for (int i = 0; i < t; i++)
- {
- cin >> s >> d;
- vector<vector<int>> residual_graph = graph;
- cout << ffs(s - 1, d - 1, parent, residual_graph);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement