結果

問題 No.2630 Colorful Vertices and Cheapest Paths
ユーザー ramdosramdos
提出日時 2024-02-16 22:14:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 378 ms / 2,500 ms
コード長 4,729 bytes
コンパイル時間 4,891 ms
コンパイル使用メモリ 272,440 KB
実行使用メモリ 83,968 KB
最終ジャッジ日時 2024-02-16 22:15:00
合計ジャッジ時間 11,616 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
22,016 KB
testcase_01 AC 378 ms
43,904 KB
testcase_02 AC 363 ms
76,544 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 137 ms
13,440 KB
testcase_07 AC 295 ms
82,816 KB
testcase_08 AC 277 ms
73,984 KB
testcase_09 AC 311 ms
83,968 KB
testcase_10 AC 373 ms
83,968 KB
testcase_11 AC 374 ms
83,968 KB
testcase_12 AC 378 ms
83,968 KB
testcase_13 AC 373 ms
83,968 KB
testcase_14 AC 371 ms
83,968 KB
testcase_15 AC 274 ms
6,548 KB
testcase_16 AC 271 ms
6,548 KB
testcase_17 AC 272 ms
6,548 KB
testcase_18 AC 167 ms
26,496 KB
testcase_19 AC 291 ms
65,152 KB
testcase_20 AC 237 ms
54,144 KB
testcase_21 AC 314 ms
83,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region おまじないα ver1 .08
// #define _CRT_SECURE_NO_WARNINGS
// #pragma warning(disable : 4996)
//  using mint = modint1000000007;
//  using mint = modint998244353;
#pragma region 変化しないおまじない
#include "bits/stdc++.h"
#if __has_include("atcoder/all")
#include <atcoder/all>
using namespace atcoder;
#endif
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()                    // sortなどの引数を省略
#define max3(x, y, z) max(x, max(y, z))                  // 3変数max
#define min3(x, y, z) min(x, min(y, z))                  // 3変数min
#define chmin(m, v) m = min((m), (v))                    // 最小値を更新
#define chmax(m, v) m = max((m), (v))                    // 最大値を更新
#define rep(i, n) repi(i, 0, n)                          // 0-N rep
#define reps(i, n) repi(i, 1, n + 1)                     // 1-N
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); i++) // a-b
#define prif(x)        \
    if (x)             \
    {                  \
        cout << "Yes"; \
    }                  \
    else               \
    {                  \
        cout << "No";  \
    }
#define P pair<ll, ll> // typedefだと予測が効かないっぽい?
#ifdef _MSC_FULL_VER   // 手元なら
#define dout cout
#else
#define dout   \
    if (false) \
    cout
#endif
double dist(double x1, double y1, double x2, double y2)
{
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
ll idist(ll x1, ll y1, ll x2, ll y2)
{
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
ll digitsum(ll N)
{
    while (N >= 10)
    {
        int tmp = 0;
        while (N > 0)
        {
            tmp += (N % 10);
            N /= 10;
        }
        N = tmp;
    }
    return N;
}
struct qwqw
{
    qwqw()
    {
        cin.tie(0);
        cout.tie(0);
        ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} aaaaaaa;
#pragma endregion
#pragma endregion
struct UF
{
    vector<ll> par;
    UF(ll N) : par(N)
    {
        for (ll i = 0; i < N; i++)
            par[i] = i;
    }
    int root(int x)
    {
        if (par[x] == x)
            return x;
        return par[x] = root(par[x]);
    }
    void unite(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry)
            return;
        par[rx] = ry;
    }
    bool same(int x, int y)
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
template <typename T>
bool next_combination(const T first, const T last, int k)
{
    const T subset = first + k;
    // empty container | k = 0 | k == n
    if (first == last || first == subset || last == subset)
    {
        return false;
    }
    T src = subset;
    while (first != src)
    {
        src--;
        if (*src < *(last - 1))
        {
            T dest = subset;
            while (*src >= *dest)
            {
                dest++;
            }
            iter_swap(src, dest);
            rotate(src + 1, dest + 1, last);
            rotate(subset, subset + (last - dest) - 1, last);
            return true;
        }
    }
    // restore
    rotate(first, subset, last);
    return false;
}
signed main()
{
    ll N, M;
    cin >> N >> M;
    vector<pair<ll, ll>> v(M);
    rep(i, M)
    {
        ll s, t;
        
        cin >> s >> t;
        s--;
        t--;
        v[i] = make_pair(s, t);
    }
    vector<ll> c(N);
    rep(i, N)
    {
        cin >> c[i];
        c[i]--;
    }
    vector<ll> w(10);
    rep(i, 10)
    {
        cin >> w[i];

    }
    ll Q;
    cin >> Q;
    vector<pair<ll, ll>> query(Q);
    rep(i, Q)
    {
        ll x, y;
        cin >> x >> y;
        x--;
        y--;
        query[i] = make_pair(x, y);
    }
    vector<ll> ans(Q,1e17);
    vector<UF> u(1024,UF(N));
    for (ll bit = 0; bit < (1 << 10); ++bit)
    {
        vector<ll> S;
        ll score = 0;
        for (int i = 0; i < 10; ++i)
        {
            if (bit & (1 << i))
            {
                S.push_back(i);
                score += w[i];
            }
        }
        rep(i,M){
            if(bit & (1 << c[v[i].first]) && bit & (1 << c[v[i].second])){
                u[bit].unite(v[i].first,v[i].second);
            }
        }
        
        rep(i,Q){
            if(u[bit].same(query[i].first,query[i].second)){
                ans[i] = min(ans[i],score);
            }
        }
    }
    rep(i,Q){
        if(ans[i] == 1e17){
            cout << -1 << endl;
        }else{
            cout << ans[i] << endl;
        }
    }
}
0