답안 #73651

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
73651 2018-08-28T16:21:06 Z karma Cities (BOI16_cities) C++11
37 / 100
6000 ms 74772 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
//Cities oj.uz not nkbuild spoj(khác dữ kiệu)

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 = 2e5 + 7;
const int M = 35;
const ll oo = 1e15 + 1;

int n, k, x, city[N], m, u, v, mask, mark;
//set<pair<ll, pair<int, int>>> s;
priority_queue<pair<ll, pii>> pq;
ll f[N][M], c, w, cost;
vector<plli> a[N];
vector<int> b[M];

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

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

void Enter()
{
    Cin(n); Cin(k); Cin(m);
    For(i, 0, Bit(k) - 1)
    {
        For(j, 0, Bit(k) - 1)
        {
            if((i & j) == 0)
                b[i].pb(j);
        }
    }
    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)}});
        pq.push({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(!pq.empty())
    {
//        w = s.begin()->X;
//        plli p = s.begin()->Y;
//        s.erase(s.begin());
        w = pq.top().X;
        plli p = pq.top().Y;
        pq.pop();
        if(w != f[p.X][p.Y]) continue;
//        if(p.Y == Bit(k) - 1)
//        {
//            cout << w << '\n';
//            exit(0);
//        }
        for(plli adj: a[p.X])
        {
            c = adj.X + f[p.X][p.Y];
            mark = p.Y;
            if(city[adj.Y]) mark |= Bit(city[adj.Y] - 1);
            if(f[adj.Y][mark] > c)
            {
                f[adj.Y][mark] = c;
                //s.insert({c, {adj.Y, mark}});
                pq.push({c, {adj.Y, mark}});
            }
            for(int bit: b[p.Y])
            {
                cost = c + f[adj.Y][bit];
                if(f[adj.Y][mark | bit] > cost)
                {
                    f[adj.Y][mark | bit] = cost;
                    //s.insert({cost, {adj.Y, mark | bit}});
                    pq.push({cost, {adj.Y, mark | bit}});
                }
            }
        }
    }
    ll res = oo;
    For(i, 1, n) res = min(res, f[i][Bit(k) - 1]);
    cout << res;
}

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 52 ms 59896 KB Output is correct
2 Correct 49 ms 60008 KB Output is correct
3 Correct 49 ms 60008 KB Output is correct
4 Correct 46 ms 60028 KB Output is correct
5 Correct 48 ms 60052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6037 ms 72640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2696 ms 72640 KB Output is correct
2 Correct 458 ms 72640 KB Output is correct
3 Correct 155 ms 72640 KB Output is correct
4 Correct 1076 ms 72640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6097 ms 72780 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 6078 ms 74772 KB Time limit exceeded
2 Halted 0 ms 0 KB -