Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <map>
  4. #include <vector>
  5. #include <set>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <array>
  9. #include <numeric>
  10. #include <limits>
  11. #include <cstdlib>
  12. #include <fstream>
  13.  
  14. // BGL includes
  15. #include <boost/graph/adjacency_list.hpp>
  16. #include <boost/graph/push_relabel_max_flow.hpp>
  17. #include <boost/tuple/tuple.hpp>
  18.  
  19.  
  20. #define unless(X) if(!(X))
  21.  
  22. // BGL graph definitions
  23. // =====================
  24. // Graph Type with nested interior edge properties for Flow Algorithms
  25. typedef boost::adjacency_list_traits<boost::vecS, boost::vecS, boost::directedS> traits;
  26. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property,
  27.     boost::property<boost::edge_capacity_t, long,
  28.         boost::property<boost::edge_residual_capacity_t, long,
  29.             boost::property<boost::edge_reverse_t, traits::edge_descriptor> > > >   graph;
  30. // Interior Property Maps
  31. typedef boost::graph_traits<graph>::edge_descriptor         edge_desc;
  32. typedef boost::graph_traits<graph>::out_edge_iterator           out_edge_it;
  33.  
  34.  
  35. class edge_adder {
  36.   graph &G;
  37.  
  38.  public:
  39.   explicit edge_adder(graph &G) : G(G) {}
  40.  
  41.   void add_edge(int from, int to, long capacity) {
  42.     auto c_map = boost::get(boost::edge_capacity, G);
  43.     auto r_map = boost::get(boost::edge_reverse, G);
  44.     const auto e = boost::add_edge(from, to, G).first;
  45.     const auto rev_e = boost::add_edge(to, from, G).first;
  46.     c_map[e] = capacity;
  47.     c_map[rev_e] = 0; // reverse edge has no capacity!
  48.     r_map[e] = rev_e;
  49.     r_map[rev_e] = e;
  50.   }
  51. };
  52.  
  53.  
  54.  
  55.  
  56. std::tuple<std::vector<bool>, int, int> cut_set(graph G, int N, int src, int sink)
  57. {
  58.     int flow = boost::push_relabel_max_flow(G, src, sink);
  59.     auto rc_map = boost::get(boost::edge_residual_capacity, G);
  60.  
  61.     int card = 1;
  62.     // BFS to find vertex set S
  63.     std::vector<bool> vis(N, false); // visited flags
  64.     std::queue<int> Q; // BFS queue (from std:: not boost::)
  65.     vis[src] = true; // Mark the source as visited
  66.     Q.push(src);
  67.     while (!Q.empty()) {
  68.         const int u = Q.front();
  69.         Q.pop();
  70.         out_edge_it ebeg, eend;
  71.         for (boost::tie(ebeg, eend) = boost::out_edges(u, G); ebeg != eend; ++ebeg) {
  72.             const int v = boost::target(*ebeg, G);
  73.             // Only follow edges with spare capacity
  74.             if (rc_map[*ebeg] == 0 || vis[v]) continue;
  75.             vis[v] = true;
  76.             card++;
  77.             Q.push(v);
  78.         }
  79.     }
  80.     return std::make_tuple(vis, flow, card);
  81. }
  82.  
  83. void testCase() {
  84.   int n, m;
  85.   std::cin >> n >> m;
  86.   graph G(n);
  87.   edge_adder adder(G);
  88.  
  89.  
  90.     int u, v, c;
  91.   for (int i = 0; i < m; i++)
  92.   {
  93.         std::cin >> u >> v >> c;
  94.         adder.add_edge(u, v, c);
  95.   }
  96.  
  97.     int cost, nbFigures;
  98.     std::vector<bool> set;
  99.     std::tie(set, cost, nbFigures) = cut_set(G, n, 0, n-1);
  100.  
  101.     std::cout << cost << std::endl;
  102.     std::cout << nbFigures << " ";
  103.     for (int i = 0; i < n; i++)
  104.         if (set[i]) std::cout << i << " ";
  105.  
  106.   std::cout << std::endl;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. int main() {
  113.   std::ios_base::sync_with_stdio(false);
  114.  
  115.   int t; std::cin >> t;
  116.   for (int i = 0; i < t; i++)
  117.   {
  118.     testCase();
  119.   }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement