Submission #84248

# Submission time Handle Problem Language Result Execution time Memory
84248 2018-11-14T03:04:31 Z Vinhspm Cities (BOI16_cities) C++14
74 / 100
6000 ms 53716 KB
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define pb push_back
#define ll long long

const int N = 1e5 + 10;
const ll oo = 1e18;
typedef pair<int, int> ii;
typedef pair<ll, ii> pii;

int n, k, m;
int pos[N];
bool inqueue[N][(1 << 5)];
ll f[N][(1 << 5)];
vector<ii> vi[N];

void upd(ll &x, ll y) { x = min(x, y); }
void solve()   {
    FOR(i, 1, n)    FOR(j, 0, (1 << k) - 1) f[i][j] = oo;
    queue<ii> pq;
    FOR(i, 1, n) {
        int mask = 0; if(pos[i]) mask  = (1 << (pos[i] - 1));
        f[i][mask] = 0ll;
        pq.push(ii(i, mask)); inqueue[i][mask] = true;
    }
    while(!pq.empty())  {
        int u = pq.front().fi, mask  = pq.front().se;
        pq.pop(); inqueue[u][mask] = false;
        //cout << u << ' ' << mask << '\n';
        for(auto v: vi[u])  {
            for(int t = 0; t < (1 << k); ++t) if(!(mask & t))   {
                int nex = (mask | t);
                if(f[v.fi][nex] > f[u][mask] + 1ll * v.se + f[v.fi][t]) {
                    f[v.fi][nex] = f[u][mask] + 1ll * v.se + f[v.fi][t];
                    if(!inqueue[v.fi][nex]) {
                        pq.push(ii(v.fi, nex));
                        inqueue[v.fi][nex] = true;
                    }
                }
            }
        }
    }
}

signed main()
{
    //freopen("test.inp", "r", stdin);
    scanf("%d%d%d", &n, &k, &m);
    FOR(i, 1, k) {
        int x; scanf("%d", &x);
        pos[x] = i;
    }
    FOR(i, 1, m)    {
        int u, v, c; scanf("%d%d%d", &u, &v, &c);
        vi[u].pb(ii(v, c)); vi[v].pb(ii(u, c));
    }
    solve();

    ll ans = oo;
    FOR(i, 1, n) upd(ans, f[i][(1 << k) - 1]);
    printf("%lld", ans);
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:52: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:54:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int x; scanf("%d", &x);
                ~~~~~^~~~~~~~~~
cities.cpp:58:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v, c; scanf("%d%d%d", &u, &v, &c);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2672 KB Output is correct
2 Correct 4 ms 2844 KB Output is correct
3 Correct 4 ms 2848 KB Output is correct
4 Correct 4 ms 2848 KB Output is correct
5 Correct 4 ms 2848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1181 ms 40460 KB Output is correct
2 Correct 929 ms 40528 KB Output is correct
3 Correct 296 ms 40528 KB Output is correct
4 Correct 300 ms 40528 KB Output is correct
5 Correct 533 ms 40528 KB Output is correct
6 Correct 156 ms 40528 KB Output is correct
7 Correct 7 ms 40528 KB Output is correct
8 Correct 6 ms 40528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 40528 KB Output is correct
2 Correct 12 ms 40528 KB Output is correct
3 Correct 6 ms 40528 KB Output is correct
4 Correct 12 ms 40528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3572 ms 44504 KB Output is correct
2 Correct 2264 ms 45260 KB Output is correct
3 Correct 518 ms 45260 KB Output is correct
4 Correct 2377 ms 45260 KB Output is correct
5 Correct 936 ms 45260 KB Output is correct
6 Correct 703 ms 45260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6085 ms 53716 KB Time limit exceeded
2 Halted 0 ms 0 KB -