Submission #79770

# Submission time Handle Problem Language Result Execution time Memory
79770 2018-10-16T09:15:42 Z SamAnd Cities (BOI16_cities) C++17
0 / 100
1156 ms 83876 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long INF = 100000000000000 + 14;
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 b[N];
vector<ban> a[N];

long long dp[1 << 5][N];

bool c[N];

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

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

int main()
{
    //freopen("input.txt", "r", stdin);
    cin >> n >> k >> m;
    for (int i = 0; i < k; ++i)
        cin >> b[i];
    for (int i = 0; i < m; ++i)
    {
        int x, y, z;
        cin >> 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[x][i] = INF;
    }
    for (int i = 0; i < k; ++i)
        djk(i);
    for (int x = 0; x < (1 << k); ++x)
    {
        int q = 0;
        for (int i = 0; i < k; ++i)
            if ((x&(1 << i)))
                ++q;
        if (q <= 1)
            continue;
        for (int y = x; y; y = (y - 1) & x)
        {
            int z = x - y;
            if (z == 0)
                continue;
            for (int i = 1; i <= n; ++i)
                dp[x][i] = min(dp[x][i], dp[z][i] + dp[y][i]);
        }
        djk1(x);
    }
    long long ans = INF;
    for (int i = 1; i <= n; ++i)
        ans = min(ans, dp[(1 << k) - 1][i]);
    cout << ans << endl;
    return 0;
}

Compilation message

cities.cpp: In function 'void djk(int)':
cities.cpp:48: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 'void djk1(int)':
cities.cpp:76:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < a[t.d].size(); ++i)
                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Runtime error 8 ms 5428 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 814 ms 46064 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 46064 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 968 ms 58856 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1156 ms 83876 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -