Submission #601826

# Submission time Handle Problem Language Result Execution time Memory
601826 2022-07-22T10:35:37 Z CaoHuuKhuongDuy Cities (BOI16_cities) C++17
100 / 100
5364 ms 143440 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
// #define int long long
const int N =  1e5 + 9;
const ll oo = 1e17;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
int n,m,k,city[7];
ll f[N][1 << 5],dist[6][N];
vector <ii> a[N];
vector <int> Newcity[1 << 5];
void Dist(ll f[],int x)
{
    for (int i = 1;i <= n;i++)
      f[i] = oo;
    priority_queue <ii,vector <ii>,greater <ii> > q;
    f[x] = 0;
    q.push({0,x});
    ll val;
    while (!q.empty())
      {
        val = q.top().first;
        x = q.top().second;
        q.pop();
        if (f[x] != val) continue;
        for (ii xnew:a[x])
          if (f[xnew.first] > f[x] + xnew.second)
            {
              f[xnew.first] = f[x] + xnew.second;
              q.push({f[xnew.first],xnew.first});
            }     
      }
}
priority_queue <iii,vector <iii>,greater <iii> > q;
void add(int x,int mask,ll val)
{
    if (f[x][mask] <= val) return;
    f[x][mask] = val;
    q.push({val,{x,mask}});
}
ll solve()
{
    for (int i = 1;i <= n;i++)
      for (int j = 0; j < (1 << k);j++)
        f[i][j] = oo;
    for (int i = 0;i < k;i++)
      {
        f[city[i]][1 << i] = 0;
        q.push({0,{city[i],1 << i}});
      } 
    ll val,x,mask; 
    while (!q.empty())
      {
        val = q.top().first;
        x = q.top().second.first;
        mask = q.top().second.second;
        q.pop();
        if (f[x][mask] != val) continue;
        for (int city:Newcity[mask])
          {
            int newmask = mask | (1 << city);
            add(x,newmask,val + dist[city][x]);
          }
        for (ii xnew:a[x])
          add(xnew.first,mask,val + xnew.second);
      }
    ll res = oo;
    for (int i = 1;i <= n;i++)
      res = min(res,f[i][(1 << k) - 1]);
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // freopen("test.inp","r",stdin);
    cin>>n>>k>>m;
    int x,y,c;
    for (int i = 0;i < k;i++)
      cin>>city[i];
    for (int i = 1;i <= m;i++)
      {
        cin>>x>>y>>c;
        a[x].push_back({y,c});
        a[y].push_back({x,c});
      }   
    for (int i = 0; i < (1 << k);i++)
      for (int j = 0; j < k;j++)
        if (!(i & (1 << j))) Newcity[i].push_back(j);
    for (int i = 0;i < k;i++)
      Dist(dist[i],city[i]);
    cout<<solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 929 ms 55036 KB Output is correct
2 Correct 892 ms 55628 KB Output is correct
3 Correct 478 ms 42416 KB Output is correct
4 Correct 71 ms 13480 KB Output is correct
5 Correct 501 ms 47992 KB Output is correct
6 Correct 89 ms 12940 KB Output is correct
7 Correct 7 ms 3340 KB Output is correct
8 Correct 5 ms 3076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3608 KB Output is correct
2 Correct 9 ms 3656 KB Output is correct
3 Correct 7 ms 3604 KB Output is correct
4 Correct 6 ms 3256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2294 ms 92960 KB Output is correct
2 Correct 1997 ms 93328 KB Output is correct
3 Correct 1579 ms 61580 KB Output is correct
4 Correct 1402 ms 54160 KB Output is correct
5 Correct 369 ms 28100 KB Output is correct
6 Correct 119 ms 15568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5364 ms 142824 KB Output is correct
2 Correct 5358 ms 142728 KB Output is correct
3 Correct 4475 ms 143440 KB Output is correct
4 Correct 3242 ms 136356 KB Output is correct
5 Correct 2604 ms 79416 KB Output is correct
6 Correct 520 ms 28432 KB Output is correct
7 Correct 93 ms 16784 KB Output is correct