This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 2e5;
int n, m;
vector< pair<int, long double> > g[N+10];
int used[N+10];
long double cost[N+10];
long double sum;
void dfs(int v){
used[v] = 1;
sum+= abs(cost[v]);
for(auto [to, c] : g[v]){
if(used[to] == 0){
cost[to] = c - cost[v];
dfs(to);
}else{
}
}
}
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long double ans;
cin >> n >> m;
for(int i = 0;i < m; i++){
int a, b, c; cin >> a >> b >> c;
g[a].push_back({b, (long double)c * 10.0});
g[b].push_back({a, (long double)c * 10.0});
}
vector<long double> how(n + 1);
int answer = 0;
for(int start = -1000; start <= 1000; start++){
for(int i = 1;i <= n; i++){
for(int j = 1;j <= n; j++) used[j] = 0;
sum = 0;
cost[i] = start;
dfs(i);
int ok = 1;
for(int v = 1; v <= n && ok; v++){
for(auto [to, c] : g[v]){
if(cost[v] + cost[to] != c){
ok = 0;
break;
}
}
}
if((!answer or ans > sum) && ok){
ans = sum;
answer = 1;
for(int j = 1;j <= n; j++) how[j] = cost[j];
}
}
}
if(!answer){
cout << "NO";
return 0;
}
cout << "YES" << '\n';
// cout << fixed << setprecision(6);
for(int i = 1;i <= n; i++) cout << how[i] / 10 << ' ';
return 0;
}
Compilation message (stderr)
Graph.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
28 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |