답안 #601765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601765 2022-07-22T09:42:53 Z CaoHuuKhuongDuy Cities (BOI16_cities) C++17
100 / 100
4851 ms 142728 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;
bool check(int mask,int x)
{
    return !(mask & (1 << x));
}
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 (check(i,j)) Newcity[i].push_back(j);
    for (int i = 0;i < k;i++)
      Dist(dist[i],city[i]);
    cout<<solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 970 ms 54240 KB Output is correct
2 Correct 888 ms 54908 KB Output is correct
3 Correct 460 ms 41708 KB Output is correct
4 Correct 68 ms 12616 KB Output is correct
5 Correct 410 ms 47320 KB Output is correct
6 Correct 57 ms 12176 KB Output is correct
7 Correct 5 ms 3284 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3608 KB Output is correct
2 Correct 9 ms 3608 KB Output is correct
3 Correct 7 ms 3476 KB Output is correct
4 Correct 6 ms 3224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2089 ms 91960 KB Output is correct
2 Correct 1787 ms 92532 KB Output is correct
3 Correct 1333 ms 60956 KB Output is correct
4 Correct 1143 ms 53452 KB Output is correct
5 Correct 247 ms 27364 KB Output is correct
6 Correct 75 ms 14844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4603 ms 141908 KB Output is correct
2 Correct 4851 ms 141952 KB Output is correct
3 Correct 4105 ms 142728 KB Output is correct
4 Correct 3070 ms 135476 KB Output is correct
5 Correct 2414 ms 78544 KB Output is correct
6 Correct 451 ms 27564 KB Output is correct
7 Correct 92 ms 15836 KB Output is correct