Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAXN 100001
- #define MAXM 200001
- using namespace std;
- struct edge{
- int nod, c;
- edge(int nod, int c):nod(nod), c(c) {}
- };
- vector<vector<edge>> G;
- char viz[MAXN];
- int n, m;
- int dg;
- int bfs(int nod, int caut, int cost) {
- //cout << "vizitez nodul " << nod << "cu costul " << cost << " caut " << caut << endl;
- if(nod == caut) {
- //cout << "am gasit cu costul " << cost << endl;
- dg = cost;
- }
- for(edge i : G[nod]) {
- if(viz[i.nod] == 0) {
- viz[i.nod] = 1;
- bfs(i.nod, caut, cost + i.c);
- }
- }
- return 0;
- }
- int main() {
- cin >> n >> m;
- G.resize(n+1);
- for(int i = 0; i < m; ++i) {
- int l, r, d;
- cin >> l >> r >> d;
- memset(viz, 0, MAXN);
- viz[l] = 1;
- //cout << "bfs pt " << l << endl;
- dg = 0;
- bfs(l, r, 0);
- //cout << "dg: " << dg << endl;
- //cout << "viz\n";
- if(dg != d && dg != 0) {
- cout << "No\n";
- return 0;
- }
- G[l].push_back(edge(r, d));
- G[r].push_back(edge(l, -d));
- }
- cout << "Yes\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement