結果

問題 No.1911 Alternately Coloring
ユーザー SSRSSSRS
提出日時 2022-04-22 21:45:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 543 ms / 4,000 ms
コード長 2,003 bytes
コンパイル時間 2,527 ms
コンパイル使用メモリ 220,504 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 08:06:41
合計ジャッジ時間 11,800 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 451 ms
4,376 KB
testcase_07 AC 450 ms
4,380 KB
testcase_08 AC 365 ms
4,380 KB
testcase_09 AC 472 ms
4,376 KB
testcase_10 AC 400 ms
4,376 KB
testcase_11 AC 319 ms
4,376 KB
testcase_12 AC 404 ms
4,376 KB
testcase_13 AC 333 ms
4,376 KB
testcase_14 AC 271 ms
4,376 KB
testcase_15 AC 343 ms
4,376 KB
testcase_16 AC 345 ms
4,376 KB
testcase_17 AC 342 ms
4,376 KB
testcase_18 AC 211 ms
4,376 KB
testcase_19 AC 234 ms
4,376 KB
testcase_20 AC 267 ms
4,376 KB
testcase_21 AC 137 ms
4,376 KB
testcase_22 AC 518 ms
4,376 KB
testcase_23 AC 543 ms
4,376 KB
testcase_24 AC 220 ms
4,376 KB
testcase_25 AC 236 ms
4,380 KB
testcase_26 AC 279 ms
4,376 KB
testcase_27 AC 290 ms
4,376 KB
testcase_28 AC 77 ms
4,376 KB
testcase_29 AC 207 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<int>> E(N);
  for (int i = 0; i < M; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> R(N);
  for (int i = 0; i < N; i++){
    cin >> R[i];
  }
  vector<int> B(N);
  for (int i = 0; i < N; i++){
    cin >> B[i];
  }
  long long sum = 0;
  for (int i = 0; i < N; i++){
    sum += max(R[i], B[i]);
  }
  long long ans = 0;
  for (int i = 0; i < 2; i++){
    for (int j = 0; j < N; j++){
      vector<vector<long long>> d(2, vector<long long>(N, -1));
      priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<tuple<long long, int, int>>> pq;
      pq.push(make_tuple(0, i, j));
      while (!pq.empty()){
        long long c = get<0>(pq.top());
        int t = get<1>(pq.top());
        int v = get<2>(pq.top());
        pq.pop();
        if (d[t][v] == -1){
          d[t][v] = c;
          for (int w : E[v]){
            if (d[1 - t][w] == -1){
              if (t == 0){
                pq.push(make_tuple(c + max(R[v], B[v]) - R[v], 1 - t, w));
              }
              if (t == 1){
                pq.push(make_tuple(c + max(R[v], B[v]) - B[v], 1 - t, w));
              }
            }
          }
        }
      }
      if (d[1 - i][j] != -1){
        ans = max(ans, sum - d[1 - i][j]);
      }
    }
  }
  if (ans != 0){
    cout << ans << endl;
  } else {
    vector<int> c(N, -1);
    c[0] = 0;
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()){
      int v = Q.front();
      Q.pop();
      for (int w : E[v]){
        if (c[w] == -1){
          c[w] = 1 - c[v];
          Q.push(w);
        }
      }
    }
    long long ans1 = 0, ans2 = 0;
    for (int i = 0; i < N; i++){
      if (c[i] == 0){
        ans1 += R[i];
        ans2 += B[i];
      }
      if (c[i] == 1){
        ans1 += B[i];
        ans2 += R[i];
      }
    }
    cout << max(ans1, ans2) << endl;
  }
}
0