제출 #161954

#제출 시각아이디문제언어결과실행 시간메모리
161954SamAndCities (BOI16_cities)C++17
100 / 100
3684 ms49880 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, K = 5;
const long long INF = 1000000009000000009;
struct ban
{
    int x;
    long long d;
    ban(){}
    ban(int x, long long d)
    {
        this->x = x;
        this->d = d;
    }
};
bool operator<(const ban& a, const ban& b)
{
    return a.d > b.d;
}

int n, k, m;
int u[K];
vector<ban> a[N];

long long dp[N][1 << K];
bool c[N];
void djk(int x)
{
    memset(c, false, sizeof c);
    priority_queue<ban> q;
    for (int i = 1; i <= n; ++i)
        q.push(ban(i, dp[i][x]));
    while (1)
    {
        ban t;
        do
        {
            if (q.empty())
            {
                return;
            }
            t = q.top();
            q.pop();
        } while (c[t.x]);
        c[t.x] = true;
        dp[t.x][x] = t.d;
        for (int i = 0; i < a[t.x].size(); ++i)
        {
            ban h = a[t.x][i];
            h.d += t.d;
            if (!c[h.x])
                q.push(h);
        }
    }
}

int main()
{
    scanf("%d%d%d", &n, &k, &m);
    for (int i = 0; i < k; ++i)
        scanf("%d", &u[i]);
    for (int i = 0; i < m; ++i)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        a[x].push_back(ban(y, z));
        a[y].push_back(ban(x, z));
    }
    for (int x = 0; x < (1 << k); ++x)
    {
        for (int i = 1; i <= n; ++i)
            dp[i][x] = INF;
    }
    for (int x = 1; x < (1 << k); ++x)
    {
        int qq = 0, ii;
        for (int i = 0; i < k; ++i)
        {
            if ((x & (1 << i)))
            {
                ++qq;
                ii = i;
            }
        }
        if (qq == 1)
        {
            dp[u[ii]][x] = 0;
        }
        for (int i = 1; i <= n; ++i)
        {
            for (int y = 0; y < k; ++y)
            {
                if (!(x & (1 << y)))
                    continue;
                int z = (x ^ (1 << y));
                dp[i][x] = min(dp[i][x], dp[i][z] + dp[i][(1 << y)]);
            }
        }
        djk(x);
    }
    long long ans = INF;
    for (int i = 1; i <= n; ++i)
        ans = min(ans, dp[i][(1 << k) - 1]);
    printf("%lld\n", ans);
    return 0;
}

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

cities.cpp: In function 'void djk(int)':
cities.cpp:47:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < a[t.x].size(); ++i)
                         ~~^~~~~~~~~~~~~~~
cities.cpp: In function 'int main()':
cities.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &n, &k, &m);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &u[i]);
         ~~~~~^~~~~~~~~~~~~
cities.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &x, &y, &z);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#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...