Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Hist {
  6.   int node, par;
  7. };
  8.  
  9. const int kMod = 1e9 + 7;
  10.  
  11. int inv(int x) {
  12.   int e = kMod - 2;
  13.   int r = 1;
  14.   while (e) {
  15.     if (e % 2) r = 1LL * r * x % kMod;
  16.     x = 1LL * x * x % kMod;
  17.     e /= 2;
  18.   }
  19.   return r;
  20. }
  21. int Out(string s) {
  22.   int ret = 0;
  23.   for (auto c : s) {
  24.     ret = 10LL * ret + c - '0';
  25.     ret %= kMod;
  26.   }
  27.   return ret;
  28. }
  29.  
  30.  
  31. int main() {
  32.   int t; cin >> t;
  33.   for (int tt = 1; tt <= t; ++tt) {
  34.     int n, m; cin >> n >> m;
  35.     vector<vector<pair<int, int>>> graph(n);
  36.     vector<vector<int>> trans(n);
  37.     vector<int> avail(n, false);
  38.  
  39.     for (int i = 0; i < m; ++i) {
  40.       int a, b, c; cin >> a >> b >> c;
  41.       graph[a].emplace_back(b, c);
  42.       trans[b].push_back(a);
  43.     }
  44.     graph[1].emplace_back(1, 0);
  45.  
  46.     vector<int> q;
  47.     auto push = [&](int node) {
  48.       if (avail[node]) return;
  49.       avail[node] = true;
  50.       q.push_back(node);
  51.     };
  52.     push(1);
  53.     for (int i = 0; i < (int)q.size(); ++i)
  54.       for (auto vec : trans[q[i]])
  55.         push(vec);
  56.     assert(avail[0]);
  57.  
  58.     string sol = "";
  59.    
  60.     vector<bool> seen(n, false);
  61.  
  62.     vector<Hist> hist;
  63.     hist.push_back(Hist{0, -1});
  64.     int old_sz = 0;
  65.  
  66.     for (int it = 0; it < 4 * n; ++it) {
  67.       fill(seen.begin(), seen.end(), false);
  68.       int lim = hist.size();
  69.      
  70.       int min_cost = 10;
  71.       for (int i = old_sz; i < lim; ++i) {
  72.         for (auto p : graph[hist[i].node]) {
  73.           if (!avail[p.first]) continue;
  74.           min_cost = min(min_cost, p.second);
  75.         }
  76.       }
  77.      
  78.       assert(min_cost < 10);
  79.       sol += min_cost + '0';
  80.  
  81.       for (int i = old_sz; i < lim; ++i) {
  82.         for (auto p : graph[hist[i].node]) {
  83.           if (!avail[p.first]) continue;
  84.           if (min_cost == p.second && !seen[p.first]) {
  85.             seen[p.first] = true;
  86.             hist.push_back(Hist{p.first, i});
  87.           }
  88.         }
  89.       }
  90.       old_sz = lim;
  91.     }
  92.  
  93.     vector<int> path;
  94.     for (int cur = hist.size() - 1; cur != -1; cur = hist[cur].par) {
  95.       path.push_back(hist[cur].node);
  96.     }
  97.     reverse(path.begin(), path.end());
  98.  
  99.     int period_starts_at = -1, period_stops_at = -1;
  100.     vector<int> order(n, -1);
  101.     for (int i = 0; i < 4 * n; ++i) {
  102.       if (order[path[i]] != -1) {
  103.         period_starts_at = order[path[i]];
  104.         period_stops_at = i;
  105.         break;
  106.       }
  107.       order[path[i]] = i;
  108.     }
  109.  
  110.     string non_period = sol.substr(0, period_starts_at);
  111.     string period = sol.substr(period_starts_at,
  112.       period_stops_at - period_starts_at);
  113.  
  114.     int up = (Out(non_period + period) - Out(non_period)) % kMod;
  115.     int down = Out(string(period.size(), '9') + string(non_period.size(), '0'));
  116.     int ret = 1LL * up * inv(down) % kMod;
  117.     if (ret < 0) ret += kMod;
  118.     cout << "Case #" << tt << ": " << ret << '\n';
  119.   }
  120.   return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement