제출 #1278886

#제출 시각아이디문제언어결과실행 시간메모리
1278886phuocrucppCities (BOI16_cities)C++20
100 / 100
3151 ms75004 KiB
/*ㅤ∧_∧
 ( ・∀・)
 ( つ┳⊃
ε (_)へ⌒ヽフ
 (  ( ・ω・)
 ◎―◎   ⊃  ⊃
BePhuongSuperSuwi
From TK4 - CHT
ㅤㅤ/ ⌒\____
  /・   )  \
 /ノへ ノ    /|
ノ    \\ |/_/_/*/


#include<bits/stdc++.h>
#define task "main"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define int long long
#define pb push_back
#define fi first
#define se second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii, ii>
#define base 341
#define MASK(i) (1ll << i)
#define oo 1e18
#define isOn(x,i) ((x) & MASK(i))
#define bitOn(x,i) ((x) | MASK(i))
#define bitOff(x,i) ((x) & ~MASK(i))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
using namespace std;
//using namespace __gnu_pbds;
const int maxn = 1e5 + 5;

int n, k, m, c[maxn];
vector <ii> g[maxn];

namespace giai1{
    int dist[5][maxn];

    void djk(int st) {
        priority_queue <ii, vector <ii>, greater <ii>> q;
        for (int i = 1; i <= n; i++) dist[st][i] = oo;
        dist[st][c[st]] = 0;
        q.push({0, c[st]});

        while(!q.empty()) {
            int cost = q.top().fi, z = q.top().se;
            q.pop();
            if (cost > dist[st][z]) continue;

            for (auto e : g[z]) {
                int x = e.fi, y = e.se;
                if (dist[st][x] > dist[st][z] + y) {
                    dist[st][x] = dist[st][z] + y;
                    q.push({dist[st][x], x});
                }
            }
        }
    }

    void solve() {
        for (int i = 1; i <= k; i++)
            djk(i);

        int mini = oo;
        for (int x = 1; x <= n; x++) {
            int sum = 0;
            for (int i = 1; i <= k; i++) {
                if (dist[i][x] == oo) {
                    sum = oo;
                    break;
                }
                sum += dist[i][x];
            }
            mini = min(mini, sum);
        }
        cout << mini;
    }
}

namespace giai2{
    int dp[(1ll << 5) + 5][maxn];

    void solve() {
        memset(dp, 0x3f, sizeof dp);

        for (int i = 1; i <= k; i++) {
            dp[(1ll << (i - 1))][c[i]] = 0;
        }

        priority_queue <ii, vector <ii>, greater <ii>> q;
        for (int mask = 0; mask < (1ll << k); mask++) {
            for (int i = 1; i <= n; i++) {

                for(int mask2 = mask; (mask2^mask)<mask2;mask2=(mask2-1)&mask) {
                    dp[mask][i] = min(dp[mask][i], dp[mask2][i] + dp[mask ^ mask2][i]);
                    if (dp[mask][i] != oo) {
                        q.push({dp[mask][i], i});
                    }
                }
            }

            while(!q.empty()) {
                int z = q.top().se, cost = q.top().fi;
                q.pop();
                if (cost > dp[mask][z]) continue;

                for (auto e : g[z]) {
                    int x = e.fi, y = e.se;
                    if (dp[mask][x] > dp[mask][z] + y) {
                        dp[mask][x] = dp[mask][z] + y;
                        q.push({dp[mask][x], x});
                    }
                }
            }
        }

        int res = oo;
        for (int i = 1; i <= n; i++)
            res = min(res, dp[(1ll << k) - 1][i]);
        cout << res;
    }
}

main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen(task".inp","r")) {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> n >> k >> m;
    for (int i = 1; i <= k; i++) cin >> c[i];
    for (int i = 1; i <= m; i++) {
        int l, r, z;
        cin >> l >> r >> z;
        g[l].pb({r, z});
        g[r].pb({l, z});
    }

    giai2::solve();
//    if (k <= 3) giai1::solve();
  //  else giai2::solve();
}

컴파일 시 표준 에러 (stderr) 메시지

cities.cpp:130:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  130 | main() {
      | ^~~~
cities.cpp: In function 'int main()':
cities.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...