TwITe

Untitled

Jan 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 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;
  11. vector<unordered_set<int>> g;
  12. unordered_map<int, unordered_map<int, d>> cost;
  13.  
  14. int ans = 0;
  15.  
  16. bool dfs(int u, int n) {
  17.     color[u] = 1;
  18.     if (u == n) {
  19.         return false;
  20.     }
  21.     for (auto v : g[u]) {
  22.         if (color[v] == 0) {
  23.             ans += cost[u][v].c;
  24.             return dfs(v, n);
  25.         }
  26.         else if (color[v] == 1 && v != u) {
  27.             return true;
  28.         }
  29.     }
  30.     color[u] = 2;
  31.     return false;
  32. }
  33.  
  34. void solve() {
  35.     ios::sync_with_stdio(false);
  36.     cin.tie(NULL);
  37.  
  38.     int n, m;
  39.     cin >> n >> m;
  40.     g.resize(n);
  41.     color.resize(n);
  42.  
  43.     for (int i = 0; i < m; i++) {
  44.         int a, b, c;
  45.         cin >> a >> b >> c;
  46.         --a, --b;
  47.         if (a != b) {
  48.             g[a].insert(b);
  49.             cost[a][b].c = max(cost[a][b].c, c);
  50.         }
  51.     }
  52.     if (dfs(0, n - 1)) {
  53.         cout << ":)"/* << ans*/;
  54.     }
  55.     else {
  56.         cout << ans;
  57.     }
  58. }
  59.  
  60. int main() {
  61.     solve();
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment