結果

問題 No.1911 Alternately Coloring
ユーザー ぷらぷら
提出日時 2022-04-22 22:50:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 552 ms / 4,000 ms
コード長 2,786 bytes
コンパイル時間 2,638 ms
コンパイル使用メモリ 212,928 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 09:31:07
合計ジャッジ時間 11,797 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 475 ms
4,380 KB
testcase_07 AC 352 ms
4,380 KB
testcase_08 AC 357 ms
4,376 KB
testcase_09 AC 440 ms
4,380 KB
testcase_10 AC 304 ms
4,376 KB
testcase_11 AC 399 ms
4,380 KB
testcase_12 AC 455 ms
4,376 KB
testcase_13 AC 410 ms
4,380 KB
testcase_14 AC 298 ms
4,384 KB
testcase_15 AC 371 ms
4,376 KB
testcase_16 AC 369 ms
4,376 KB
testcase_17 AC 378 ms
4,376 KB
testcase_18 AC 297 ms
4,376 KB
testcase_19 AC 339 ms
4,376 KB
testcase_20 AC 379 ms
4,380 KB
testcase_21 AC 216 ms
4,384 KB
testcase_22 AC 442 ms
4,380 KB
testcase_23 AC 552 ms
4,380 KB
testcase_24 AC 167 ms
4,376 KB
testcase_25 AC 186 ms
4,380 KB
testcase_26 AC 303 ms
4,380 KB
testcase_27 AC 365 ms
4,376 KB
testcase_28 AC 121 ms
4,380 KB
testcase_29 AC 305 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long inf = 1001001001001001001;

long long dist[1001][2][2];

using P = array<long long,4>;

bool chmin(long long &x,long long y) {
    if(x > y) {
        x = y;
        return true;
    }
    return false;
}

int main() {
    int N,M;
    cin >> N >> M;
    vector<vector<int>>road(N);
    for(int i = 0; i < M; i++) {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        road[u].push_back(v);
        road[v].push_back(u);
    }
    vector<int>R(N),B(N);
    for(int i = 0; i < N; i++) {
        cin >> R[i];
    }
    for(int i = 0; i < N; i++) {
        cin >> B[i];
    }
    vector<int>dist2(N,-1);
    dist2[0] = 0;
    queue<int>que;
    que.push(0);
    while (!que.empty()) {
        int x = que.front();
        que.pop();
        for(int i:road[x]) {
            if(dist2[i] == -1) {
                dist2[i] = dist2[x]^1;
                que.push(i);
            }
        }
    }
    long long a = 0,b = 0;
    for(int i = 0; i < N; i++) {
        if(dist2[i]) {
            a += R[i];
            b += B[i];
        }
        else {
            a += B[i];
            b += R[i];
        }
    }
    long long ans = max(a,b),sum = 0;
    for(int i = 0; i < N; i++) {
        sum += max(R[i],B[i]);
    }
    for(int i = 0; i < N; i++) {
        priority_queue<P,vector<P>,greater<P>>que2;
        for(int l = 0; l < N; l++) {
            for(int j = 0; j < 2; j++) {
                for(int k = 0; k < 2; k++) {
                    dist[l][j][k] = inf;
                }
            }
        }
        dist[i][0][0] = 0;
        que2.push({0,i,0,0});
        while (!que2.empty()) {
            P x = que2.top();
            que2.pop();
            if(dist[x[1]][x[2]][x[3]] != x[0]) {
                continue;
            }
            for(int k:road[x[1]]) {
                int c = 0;
                if((x[2]^x[3]) == 1) {
                    c = max(R[k],B[k])-R[k];
                }
                if((x[2]^x[3]) == 0) {
                    c = max(R[k],B[k])-B[k];
                }
                if(chmin(dist[k][x[2]^1][x[3]],x[0]+c)) {
                    que2.push({x[0]+c,k,x[2]^1,x[3]});
                }
                if(x[3] == 0) {
                    int c = 0;
                    if(x[2] == 1) {
                        c = max(R[k],B[k])-B[k];
                    }
                    if(x[2] == 0) {
                        c = max(R[k],B[k])-R[k];
                    }
                    if(chmin(dist[k][x[2]^1][1],x[0]+c)) {
                        que2.push({x[0]+c,k,x[2]^1,1});
                    }
                }
            }
        }
        ans = max(ans,sum-dist[i][1][0]);
        ans = max(ans,sum-dist[i][1][1]);
    }
    cout << ans << endl;
}
0