답안 #72600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
72600 2018-08-26T10:58:32 Z karma Cities (BOI16_cities) C++11
51 / 100
6000 ms 225968 KB
#include<bits/stdc++.h>
#define For(i, a, b)  for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define pb            push_back
#define X             first
#define Y             second
//#define Karma

using namespace std;

template <typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-')
            sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}

typedef pair<int, int> pii;
typedef pair<ll, int> plli;
const int N = 1e5 + 7;
const int M = (1 << 5) + 7;
const ll oo = 1e15 + 1;

int n, k, x, city[N], m, u, v;
set<pair<ll, pair<int, int>>> s;
ll f[N][M], c;
vector<plli> a[N];

//f[u][mark] = f[v][mark] + c(u, v)
//f[u][mark] = f[u][mark'] + f[u][mark ^ mark']

int Bit(int i)
{
    return (1 << i);
}

void Enter()
{
    Cin(n); Cin(k); Cin(m);
    fill_n(&f[0][0], N * M, oo);
    For(i, 1, k)
    {
        Cin(x);
        city[x] = i;
        f[x][Bit(i - 1)] = 0;
        s.insert({0, {x, Bit(i - 1)}});
    }
    For(i, 1, m)
    {
        Cin(u); Cin(v); Cin(c);
        a[u].pb({c, v});
        a[v].pb({c, u});
    }
}

void Solve()
{
    while(!s.empty())
    {
        ll w = s.begin()->X;
        pii p = s.begin()->Y;
        s.erase(s.begin());
        if(w > f[p.X][p.Y])
            continue;

        if(p.Y == Bit(k) - 1)
        {
            cout << w << '\n';
            return;
        }

        int u = p.X;
        int mask = p.Y;
        for(plli adj: a[u])
        {
            ll c = adj.X + f[u][mask];
            int v = adj.Y;
            int mark = mask;
            if(city[v]) mark |= Bit(city[v] - 1);
            if(f[v][mark] > c)
            {
                f[v][mark] = c;
                s.insert({c, {v, mark}});
            }
            For(bit, 0, Bit(k) - 1)
            {
                if((bit & mask) == 0)
                {
                    ll cost = c + f[v][bit];
                    if(f[v][mark | bit] > cost)
                    {
                        f[v][mark | bit] = cost;
                        s.insert({cost, {v, mark | bit}});
                    }
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifdef Karma
    freopen(FileName".inp", "r", stdin);
    freopen(FileName".out", "w", stdout);
#endif // Karma

    Enter();
    Solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 33152 KB Output is correct
2 Correct 34 ms 33308 KB Output is correct
3 Correct 34 ms 33460 KB Output is correct
4 Correct 32 ms 33472 KB Output is correct
5 Correct 33 ms 33492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3673 ms 99260 KB Output is correct
2 Correct 3224 ms 99260 KB Output is correct
3 Correct 337 ms 99260 KB Output is correct
4 Correct 262 ms 99260 KB Output is correct
5 Correct 1038 ms 99260 KB Output is correct
6 Correct 149 ms 99260 KB Output is correct
7 Correct 47 ms 99260 KB Output is correct
8 Correct 37 ms 99260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 99260 KB Output is correct
2 Correct 66 ms 99260 KB Output is correct
3 Correct 39 ms 99260 KB Output is correct
4 Correct 61 ms 99260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6023 ms 208148 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6075 ms 225968 KB Time limit exceeded
2 Halted 0 ms 0 KB -