Advertisement
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, prev1;
- vector<unordered_set<int>> g;
- unordered_map<int, unordered_map<int, d>> cost;
- int cycle_start, cycle_end;
- int ans = -INT_MAX, cnt = 0;
- vector<bool> vis;
- bool dfs1(int u, int n) {
- vis[u] = true;
- if (u == n) {
- return true;
- }
- for (auto v : g[u]) {
- if (!vis[v]) {
- cnt += cost[u][v].c;
- ans = max(ans, cnt);
- return dfs1(v, n);
- }
- }
- return false;
- }
- bool dfs2 (int u) {
- color[u] = 1;
- for (auto v : g[u]) {
- if (color[v] == 0) {
- prev1[v] = u;
- if (dfs2(v)) {
- return true;
- }
- }
- else if (color[v] == 1) {
- cycle_end = u;
- cycle_start = v;
- 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);
- vis.resize(n);
- prev1.resize(n);
- bool lotknow = false;
- for (int i = 0; i < m; i++) {
- int a, b, c;
- cin >> a >> b >> c;
- --a, --b;
- g[a].insert(b);
- cost[a][b].c = max(cost[a][b].c, c);
- if (a == b && cost[a][b].c > 0) {
- lotknow = true;
- }
- }
- cycle_start = -1;
- if (!dfs1(0, n - 1)) {
- cout << ":(";
- }
- else {
- if (lotknow) {
- cout << ":)";
- return;
- }
- for (int i = 0; i < n - 1; i++) {
- color.assign(n - 1, 0);
- if (dfs2(i)) {
- vector<int> cycle_path;
- cycle_path.push_back(cycle_start);
- for (int v = cycle_end; v != cycle_start; v = prev1[v]) {
- cycle_path.push_back(v);
- }
- int sum = 0;
- for (int i1 : cycle_path) {
- sum += i1;
- }
- if (sum <= 0) {
- cout << ans;
- return;
- }
- }
- }
- if (cycle_start != -1) {
- cout << ":)";
- return;
- }
- else {
- cout << ans;
- }
- }
- }
- int main() {
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement