Advertisement
Sander447

test_case_generator.cpp

Jun 19th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. // Author: Sander de Beet
  2.  
  3. #include <bits/stdc++.h>  // This will work only for g++ compiler.
  4.  
  5. #define int long long
  6.  
  7. #define f0r(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
  8. #define f1r(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
  9. #define f0rr(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
  10. #define f1rr(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
  11.  
  12. // short hand for usual tokens
  13. #define pb push_back
  14. #define fi first
  15. #define se second
  16.  
  17. // to be used with algorithms that processes a container Eg: find(all(c),42)
  18. #define all(x) (x).begin(), (x).end() //Forward traversal
  19. #define rall(x) (x).rbegin, (x).rend() //reverse traversal
  20.  
  21. // find if a given value is present in a container. Container version. Runs in log(n) for set and map
  22. #define present(c,x) ((c).find(x) != (c).end())
  23.  
  24. // find version works for all containers. This is present in std namespace.
  25. #define cpresent(c,x) (find(all(c),x) != (c).end())
  26.  
  27. #define uni(x) (x).erase(unique(all(x)), (x).end())
  28. #define debug(x) cout<<#x<<" -> "<<x<<'\n'
  29.  
  30.  
  31. using namespace std;
  32.  
  33. // Shorthand for commonly used types
  34. typedef vector<int> vi;
  35. typedef vector<vi> vvi;
  36. typedef vector<string> vs;
  37. typedef pair<int, int> ii;
  38. typedef vector<ii> vii;
  39. typedef long long ll;
  40. typedef vector<ll> vll;
  41. typedef vector<vll> vvll;
  42. typedef long double ld;
  43.  
  44. const ld  pi   = 4.0*atanl(1.0);
  45. const int iinf = 1e9 + 10;
  46. const ll  inf  = 1e18 + 10;
  47. const int mod  = 1000000007;
  48. const ld  prec = .00000000001;
  49.  
  50. void fast_IO() {
  51.     ios::sync_with_stdio(false);
  52.     cin.tie(NULL); cout.tie(NULL);
  53. }
  54.  
  55. void file() {
  56.     auto a = freopen("a.in",  "r", stdin);
  57.     auto b = freopen("a.out", "w", stdout);
  58.     if(!a || !b) cout << "uh oh" << endl;
  59. }
  60.  
  61.  
  62. void sparse() {
  63.     int iterations = 16;
  64.  
  65.     for (int i = 0; i <= iterations; ++i) {
  66.         std::ofstream outfile ("./sparse/sparsetest" + to_string(i) + ".txt");
  67.         int vertices = pow(2, i);
  68.         int edges = vertices - 1;
  69.         outfile << vertices << " " << edges << "\n";
  70.  
  71.         int root = rand() % vertices;
  72.         vector<int> tree = {root};
  73.         vector<int> graph(vertices);
  74.         iota(all(graph), 0);
  75.         auto it = find(all(graph), root);
  76.         graph.erase(it);
  77.    
  78.         for (int j = 0; j < edges; ++j) {
  79.             int from_idx = rand() % tree.size();
  80.             int to_idx = rand() % graph.size();
  81.             int from = tree[from_idx];
  82.             int to = graph[to_idx];
  83.             int cost = rand() % 100;
  84.  
  85.             outfile << from << " " << to << " " << cost << "\n";
  86.             tree.pb(to);
  87.             graph.erase(begin(graph) + to_idx);
  88.         }
  89.  
  90.         outfile.close();
  91.     }
  92. }
  93.  
  94.  
  95. void dense() {
  96.     int iterations = 20;
  97.  
  98.     for (int i = 0; i <= iterations; ++i) {
  99.         std::ofstream outfile ("./dense/densetest" + to_string(i) + ".txt");
  100.         int vertices = pow(2, i);
  101.         int edges = vertices * 20;
  102.         outfile << vertices << " " << edges << "\n";
  103.  
  104.         int root = rand() % vertices;
  105.         vector<int> tree = {root};
  106.         vector<int> graph(vertices);
  107.         iota(all(graph), 0);
  108.         auto it = find(all(graph), root);
  109.         graph.erase(it);
  110.    
  111.         for (int j = 0; j < vertices - 1; ++j) {
  112.             int from_idx = rand() % tree.size();
  113.             int to_idx = rand() % graph.size();
  114.             int from = tree[from_idx];
  115.             int to = graph[to_idx];
  116.             int cost = rand() % 100;
  117.             outfile << from << " " << to << " " << cost << "\n";
  118.             tree.pb(to);
  119.             graph.erase(begin(graph) + to_idx);
  120.         }
  121.  
  122.         if (vertices != 1) {
  123.             for (int j = 0; j < edges - (vertices + 1); ++j) {
  124.                 while (1) {
  125.                     int from_idx = rand() % vertices;
  126.                     int to_idx = rand() % vertices;
  127.                     if (from_idx == to_idx) continue;
  128.                     int cost = rand() % 100;
  129.                     outfile << from_idx << " " << to_idx << " " << cost << "\n";
  130.                     break;
  131.                 }
  132.             }
  133.         }
  134.  
  135.         outfile.close();
  136.     }
  137. }
  138.  
  139.  
  140. void sparse_to_dense() {
  141.     int iterations = 16;
  142.  
  143.     for (int i = 1; i <= iterations; ++i) {
  144.         std::ofstream outfile ("./sparse_to_dense/densetest" + to_string(i) + ".txt");
  145.         int vertices = pow(2, 16);
  146.         int edges = vertices * i;
  147.         outfile << vertices << " " << edges << "\n";
  148.  
  149.         int root = rand() % vertices;
  150.         vector<int> tree = {root};
  151.         vector<int> graph(vertices);
  152.         iota(all(graph), 0);
  153.         auto it = find(all(graph), root);
  154.         graph.erase(it);
  155.    
  156.         for (int j = 0; j < vertices - 1; ++j) {
  157.             int from_idx = rand() % tree.size();
  158.             int to_idx = rand() % graph.size();
  159.             int from = tree[from_idx];
  160.             int to = graph[to_idx];
  161.             int cost = rand() % 100;
  162.             outfile << from << " " << to << " " << cost << "\n";
  163.             tree.pb(to);
  164.             graph.erase(begin(graph) + to_idx);
  165.         }
  166.  
  167.         if (vertices != 1) {
  168.             for (int j = 0; j < edges - (vertices + 1); ++j) {
  169.                 while (1) {
  170.                     int from_idx = rand() % vertices;
  171.                     int to_idx = rand() % vertices;
  172.                     if (from_idx == to_idx) continue;
  173.                     int cost = rand() % 100;
  174.                     outfile << from_idx << " " << to_idx << " " << cost << "\n";
  175.                     break;
  176.                 }
  177.  
  178.             }
  179.         }
  180.  
  181.         outfile.close();
  182.     }
  183. }
  184.  
  185.  
  186. signed main() {
  187.     fast_IO();
  188.     // file();
  189.     cout.precision(10);
  190.     cout << fixed;
  191.  
  192.     /* Choose what scenario of test cases you want to generate. */
  193.     // sparse();
  194.     // dense();
  195.     sparse_to_dense();
  196.  
  197.     return 0;
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement