Advertisement
TwITe

Untitled

Jan 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. using ull = unsigned long long;
  5.  
  6. struct d {
  7.     int c = -10001;
  8. };
  9.  
  10. vector<int> color, prev1;
  11. vector<unordered_set<int>> g;
  12. unordered_map<int, unordered_map<int, d>> cost;
  13. int cycle_start, cycle_end;
  14.  
  15. int ans = -INT_MAX, cnt = 0;
  16. vector<bool> vis;
  17.  
  18. bool dfs1(int u, int n) {
  19.     vis[u] = true;
  20.     if (u == n) {
  21.         return true;
  22.     }
  23.     for (auto v : g[u]) {
  24.         if (!vis[v]) {
  25.             cnt += cost[u][v].c;
  26.             ans = max(ans, cnt);
  27.             return dfs1(v, n);
  28.         }
  29.     }
  30.     return false;
  31. }
  32.  
  33. bool dfs2 (int u) {
  34.     color[u] = 1;
  35.     for (auto v : g[u]) {
  36.         if (color[v] == 0) {
  37.             prev1[v] = u;
  38.             if (dfs2(v)) {
  39.                 return true;
  40.             }
  41.         }
  42.         else if (color[v] == 1) {
  43.             cycle_end = u;
  44.             cycle_start = v;
  45.             return true;
  46.         }
  47.     }
  48.     color[u] = 2;
  49.     return false;
  50. }
  51.  
  52. void solve() {
  53.     ios::sync_with_stdio(false);
  54.     cin.tie(NULL);
  55.  
  56.     int n, m;
  57.     cin >> n >> m;
  58.     g.resize(n);
  59.     color.resize(n);
  60.     vis.resize(n);
  61.     prev1.resize(n);
  62.  
  63.     bool lotknow = false;
  64.     for (int i = 0; i < m; i++) {
  65.         int a, b, c;
  66.         cin >> a >> b >> c;
  67.         --a, --b;
  68.         g[a].insert(b);
  69.         cost[a][b].c = max(cost[a][b].c, c);
  70.         if (a == b && cost[a][b].c > 0) {
  71.             lotknow = true;
  72.         }
  73.     }
  74.     cycle_start = -1;
  75.     if (!dfs1(0, n - 1)) {
  76.         cout << ":(";
  77.     }
  78.     else {
  79.         if (lotknow) {
  80.             cout << ":)";
  81.             return;
  82.         }
  83.         for (int i = 0; i < n - 1; i++) {
  84.             color.assign(n - 1, 0);
  85.             if (dfs2(i)) {
  86.                 vector<int> cycle_path;
  87.                 cycle_path.push_back(cycle_start);
  88.  
  89.                 for (int v = cycle_end; v != cycle_start; v = prev1[v]) {
  90.                     cycle_path.push_back(v);
  91.                 }
  92.  
  93.                 int sum = 0;
  94.  
  95.                 for (int i1 : cycle_path) {
  96.                     sum += i1;
  97.                 }
  98.                 if (sum <= 0) {
  99.                     cout << ans;
  100.                     return;
  101.                 }
  102.             }
  103.         }
  104.         if (cycle_start != -1) {
  105.             cout << ":)";
  106.             return;
  107.         }
  108.         else {
  109.             cout << ans;
  110.         }
  111.     }
  112. }
  113.  
  114. int main() {
  115.     solve();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement