답안 #148103

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
148103 2019-08-31T13:55:13 Z WhipppedCream Cities (BOI16_cities) C++17
100 / 100
2692 ms 44920 KB
//Power Of Ninja Go
#include <bits/stdc++.h>
//#ifdef atom #else #endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
const int maxn = 1e5+5;
vii adj[maxn];
ll dp[32][maxn];
int main()
{
    int n, k, m; scanf("%d %d %d", &n, &k, &m);
    vi imp; imp.resize(k);
    for(int i = 0; i< k; i++) scanf("%d", &imp[i]);
    for(int i = 0; i< m; i++)
    {
        int u, v, w; scanf("%d %d %d", &u, &v, &w);
        adj[u].pb(ii(v, w));
        adj[v].pb(ii(u, w));
    }
    for(int a = 0; a< 32; a++)
    {
        for(int u = 1; u<= n; u++) dp[a][u] = 4e18;
    }
    for(int i = 0; i< k; i++)
    {
        dp[1<<i][imp[i]] = 0;
    }
    for(int bit = 1; bit< (1<<k); bit++)
    {
        for(int part = 0; part< bit; part++)
        {
            if((part | bit) != bit) continue;
            int rem = bit-part;
            for(int u = 1; u<= n; u++)
                dp[bit][u] = min(dp[bit][u], dp[part][u]+dp[rem][u]);
        }
        priority_queue< pair<ll, int> > pq;
        for(int i = 1; i<= n; i++)
        {
            if(dp[bit][i] != 4e18)
            {
                pq.push(make_pair(-dp[bit][i], i));
            }
        }
        while(!pq.empty())
        {
            ll w = -pq.top().X;
            int u = pq.top().Y;
            pq.pop();
            if(dp[bit][u] != w) continue;
            for(auto edge : adj[u])
            {
                int v = edge.X, w = edge.Y;
                if(dp[bit][v]> dp[bit][u]+w)
                {
                    dp[bit][v] = dp[bit][u]+w;
                    pq.push(make_pair(-dp[bit][v], v));
                }
            }
        }
    }
    ll res = 4e18;
    for(int i = 1; i<= n; i++) res = min(res, dp[(1<<k)-1][i]);
    cout << res << endl;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:14:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     int n, k, m; scanf("%d %d %d", &n, &k, &m);
                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:16:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 0; i< k; i++) scanf("%d", &imp[i]);
                               ~~~~~^~~~~~~~~~~~~~~
cities.cpp:19:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v, w; scanf("%d %d %d", &u, &v, &w);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 4 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 662 ms 44564 KB Output is correct
2 Correct 660 ms 44544 KB Output is correct
3 Correct 395 ms 36596 KB Output is correct
4 Correct 100 ms 11128 KB Output is correct
5 Correct 371 ms 42388 KB Output is correct
6 Correct 94 ms 11128 KB Output is correct
7 Correct 8 ms 3320 KB Output is correct
8 Correct 6 ms 3192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 3236 KB Output is correct
2 Correct 10 ms 3320 KB Output is correct
3 Correct 8 ms 3192 KB Output is correct
4 Correct 8 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1293 ms 44668 KB Output is correct
2 Correct 1279 ms 44584 KB Output is correct
3 Correct 870 ms 36676 KB Output is correct
4 Correct 737 ms 28836 KB Output is correct
5 Correct 225 ms 14952 KB Output is correct
6 Correct 104 ms 12664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2578 ms 44920 KB Output is correct
2 Correct 2692 ms 44772 KB Output is correct
3 Correct 2596 ms 44688 KB Output is correct
4 Correct 1840 ms 36636 KB Output is correct
5 Correct 1392 ms 28760 KB Output is correct
6 Correct 350 ms 14908 KB Output is correct
7 Correct 119 ms 12612 KB Output is correct