답안 #708976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
708976 2023-03-13T03:23:53 Z null_awe Graph (BOI20_graph) C++14
0 / 100
1 ms 340 KB
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

#define ll long long
#define pii pair<int, int>

const double EPS = 0.000001;

struct line {

  int m, b;

  line() {}

  line(int m, int b) : m(m), b(b) {}

  double combine(line other) {
    int mm = m - other.m, bb = other.b - b;
    if (mm == 0 && bb) {
      cout << "NO\n";
      exit(0);
    }
    if (mm == 0) return INT_MAX;
    return (double) bb / mm;
  }
};

int main() {
  int n, m; cin >> n >> m;
  vector<vector<pii>> adj(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c; cin >> a >> b >> c; --a, --b;
    adj[a].push_back({b, c}), adj[b].push_back({a, c});
  }
  vector<line> lines(n); lines[0] = line(1, 0);
  vector<bool> vis(n);
  queue<int> q;
  vis[0] = true, q.push(0);
  double ans = 0;
  while (q.size()) {
    int v = q.front(); q.pop();
    for (pii p : adj[v]) {
      int u = p.first, c = p.second;
      line change(-lines[v].m, c - lines[v].b);
      if (!vis[u]) {
        vis[u] = true, q.push(u);
        lines[u] = change;
      } else {
        ans = lines[u].combine(change);
        if (ans < INT_MAX - 1) break;
      }
    }
  }
  if (ans > INT_MAX - 1) {
    ll mn = LLONG_MAX;
    int val = 0;
    for (line l : lines) {
      int t = -l.b / l.m;
      ll sum = 0;
      for (line _l : lines) sum += abs(_l.m * t + _l.b);
      if (sum < mn) mn = sum, ans = t;
    }
  }
  vector<double> values(n); values[0] = ans;
  vis.assign(n, false);
  q = queue<int>();
  vis[0] = true, q.push(0);
  while (q.size()) {
    int v = q.front(); q.pop();
    for (pii p : adj[v]) {
      int u = p.first, c = p.second;
      // cout << v << ' ' << u << '\n';
      if (vis[u]) continue;
      vis[u] = true, q.push(u);
      values[u] = c - values[v];
    }
  }
  cout << "YES\n";
  for (double val : values) cout << val << ' ';
  cout << '\n';
  return 0;
}

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:59:9: warning: unused variable 'val' [-Wunused-variable]
   59 |     int val = 0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Runtime error 1 ms 340 KB Execution killed with signal 8
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Runtime error 1 ms 340 KB Execution killed with signal 8
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Runtime error 1 ms 340 KB Execution killed with signal 8
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Runtime error 1 ms 340 KB Execution killed with signal 8
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Runtime error 1 ms 340 KB Execution killed with signal 8
6 Halted 0 ms 0 KB -