Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ull = unsigned long long;
- struct d {
- int c = -10001;
- };
- vector<int> color;
- vector<unordered_set<int>> g;
- unordered_map<int, unordered_map<int, d>> cost;
- int ans = 0;
- bool dfs(int u, int n) {
- color[u] = 1;
- if (u == n) {
- return false;
- }
- for (auto v : g[u]) {
- if (color[v] == 0) {
- ans += cost[u][v].c;
- return dfs(v, n);
- }
- else if (color[v] == 1 && v != u) {
- return true;
- }
- }
- color[u] = 2;
- return false;
- }
- void solve() {
- ios::sync_with_stdio(false);
- cin.tie(NULL);
- int n, m;
- cin >> n >> m;
- g.resize(n);
- color.resize(n);
- for (int i = 0; i < m; i++) {
- int a, b, c;
- cin >> a >> b >> c;
- --a, --b;
- if (a != b) {
- g[a].insert(b);
- cost[a][b].c = max(cost[a][b].c, c);
- }
- }
- if (dfs(0, n - 1)) {
- cout << ":)"/* << ans*/;
- }
- else {
- cout << ans;
- }
- }
- int main() {
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment