結果

問題 No.3751 Nonopoly
コンテスト
ユーザー marc2825
提出日時 2026-08-08 18:52:03
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,277 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,124 ms
コンパイル使用メモリ 219,756 KB
実行使用メモリ 22,360 KB
最終ジャッジ日時 2026-10-02 20:54:04
合計ジャッジ時間 7,864 ms
ジャッジサーバーID
(参考情報)
judge4_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 9 TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

struct Edge {
    int to;
    long long w;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<vector<Edge>> g(N);

    struct E {
        int u, v;
        long long w;
    };

    vector<E> edges(M);

    for (auto &[u, v, w] : edges) {
        cin >> u >> v >> w;
        u--, v--;

        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }

    // -1: 未取得, 0: Alice, 1: Bob
    vector<int> owner(N, -1);

    for (int turn = 0; turn < N; ++turn) {
        int player = turn % 2;

        long long best = -1;
        int bestV = -1;

        for (int v = 0; v < N; ++v) {
            if (owner[v] != -1) continue;

            long long gain = 0;

            for (auto [to, w] : g[v]) {
                if (owner[to] == player) {
                    gain += w;
                }
            }

            if (gain > best) {
                best = gain;
                bestV = v;
            }
        }

        owner[bestV] = player;
    }

    long long ans = 0;

    for (auto [u, v, w] : edges) {
        if (owner[u] != owner[v]) continue;

        if (owner[u] == 0) ans += w;
        else ans -= w;
    }

    cout << ans << '\n';
}
0