답안 #1059757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059757 2024-08-15T07:54:51 Z vjudge1 Cities (BOI16_cities) C++17
100 / 100
5904 ms 169788 KB
#include<bits/stdc++.h>

using namespace std;
const int M=1e5+10;
int n,m,k;
long long  f[M][1<<5];
vector<pair<int,int>> ke[M];
priority_queue<tuple<long long ,int ,int >,vector<tuple<long long ,int ,int > > ,greater<tuple<long long ,int ,int > > > h;
long long  tmp[1<<5];
vector<int> submask[1<<5];
void dij()
{
  while(!h.empty())
  {
    auto [val,mask,u]=h.top();h.pop();
    if(f[u][mask]<val) continue;
    for(pair<int,int> v:ke[u])
    {
      for(int i:submask[mask]) tmp[i]=min(f[v.first][i],val+f[v.first][i^mask]+v.second);
      for(int i:submask[mask])
      {
        if(tmp[i]<f[v.first][i])
        {
          f[v.first][i]=tmp[i];
          h.push({f[v.first][i],i,v.first});
          tmp[i]=1e18;
        }
      }
    }
  }
}
int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  #define task "1"
  if(fopen(task".inp","r"))
  {
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);
  }
  cin >> n >> k >> m;
  for(int i=0;i<(1<<k);i++)
  {
    for(int j=0;j<(1<<k);j++) if((i&j)==i) submask[i].push_back(j);
  }
  for(int i=1;i<=n;i++)
  {
    for(int j=1;j<(1<<k);j++) f[i][j]=1e18;
  }
  for(int i=0;i<(1<<k);i++) tmp[i]=1e18;
  for(int i=0;i<k;i++)
  {
    int x;
    cin >> x;
    h.push({0,1<<i,x});
    f[x][1<<i]=0;
  }
  for(int i=1;i<=m;i++)
  {
    int u,v;
    long long w;
    cin >> u >> v >> w;
    ke[u].push_back({v,w});
    ke[v].push_back({u,w});
  }
  dij();
  long long  kq=1e18;
  for(int i=1;i<=n;i++) kq=min(kq,f[i][(1<<k)-1]);
  cout << kq;
}


Compilation message

cities.cpp: In function 'int32_t main()':
cities.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:41:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     freopen(task".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 640 ms 54844 KB Output is correct
2 Correct 556 ms 54300 KB Output is correct
3 Correct 287 ms 37272 KB Output is correct
4 Correct 64 ms 12156 KB Output is correct
5 Correct 226 ms 42436 KB Output is correct
6 Correct 44 ms 11468 KB Output is correct
7 Correct 4 ms 3416 KB Output is correct
8 Correct 2 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3800 KB Output is correct
2 Correct 8 ms 3800 KB Output is correct
3 Correct 5 ms 3420 KB Output is correct
4 Correct 6 ms 3544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1937 ms 103932 KB Output is correct
2 Correct 1752 ms 103532 KB Output is correct
3 Correct 891 ms 49564 KB Output is correct
4 Correct 1107 ms 58416 KB Output is correct
5 Correct 280 ms 30128 KB Output is correct
6 Correct 114 ms 14788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5878 ms 169788 KB Output is correct
2 Correct 5904 ms 169664 KB Output is correct
3 Correct 5506 ms 169392 KB Output is correct
4 Correct 2788 ms 98896 KB Output is correct
5 Correct 3655 ms 156528 KB Output is correct
6 Correct 713 ms 46740 KB Output is correct
7 Correct 283 ms 20928 KB Output is correct