Cod sursa(job #2752160)

Utilizator flibiaVisanu Cristian flibia Data 16 mai 2021 22:01:01
Problema Algoritmul Bellman-Ford Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.23 kb
// g++ -std=c++17 -DLOCAL a.cpp -o ex && ./ex >tst.out 2>&1
#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
    return '"' + s + '"';
}
 
string to_string(const char* s) {
    return to_string((string) s);
}
 
string to_string(bool b) {
    return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
    string res = "";
    for (size_t i = 0; i < N; i++) {
        res += static_cast<char>('0' + v[i]);
    }
    return res;
}
 
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << "   "; }
void debug_out_nl() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

template <typename Head, typename... Tail>
void debug_out_nl(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out_nl(T...);
}
 
#ifdef LOCAL
#define dbg(...) cerr << "#" << #__VA_ARGS__ << ":", debug_out(__VA_ARGS__)
#define nl(...) cerr << "#" << #__VA_ARGS__ << ":", debug_out_nl(__VA_ARGS__)
#else
#define dbg(...) 69
#define nl(...) 42
#endif

#define ll long long
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll st, ll dr) {
    assert(st <= dr);
    return st + rng() % (dr - st + 1);
}

const int MOD = 1e9 + 7;
const int N = 5e4 + 10;

int n, m;
vector<pair<int, int> > v[N];
int cnt[N];
int dp[N];
int pv[N];

void solve(int test, istream &cin, ostream &cout) {
    cin >> n >> m;
    for (int i = 1, x, y, c; i <= m; i++) {
        cin >> x >> y >> c;
        v[x].push_back({y, c});
    }

    for (int i = 1; i <= n; i++) {
        dp[i] = 1e9;
    }

    dp[1] = 0;

    queue<int> q;
    q.push(1);
    bool foundCycle = false;
    int target = -1;
    while (!q.empty() && !foundCycle) {
        auto dad = q.front();
        q.pop();

        for (auto x : v[dad]) {
            int son = x.first;
            int cost = x.second;
            int newCost = dp[dad] + cost;
            
            if (newCost < dp[son]) {
                if (cnt[son] == n) {
                    foundCycle = true;
                    target = son;
                    break;
                } else {
                    dp[son] = newCost;
                    pv[son] = dad;
                    cnt[son]++;
                    q.push(son);
                }
            }
        }
    }   

    if (foundCycle) {
        cout << "Ciclu negativ!";
        return;
    }

    for (int i = 2; i <= n; i++) {
        cout << dp[i] << ' ';
    }

    // cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}

int main() {
    ifstream cin("bellmanford.in");
    ofstream cout("bellmanford.out");
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    bool multiTest = false;

    int t;    
    if (multiTest) {
        cin >> t;
    } else {
        t = 1;
    }

    for (int test = 1; test <= t; test++) {
        solve(test, cin, cout);
    }

    return 0;
}