답안 #601742

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601742 2022-07-22T09:32:32 Z CaoHuuKhuongDuy Cities (BOI16_cities) C++17
0 / 100
339 ms 46620 KB
#include <bits/stdc++.h>
using namespace std;
#define ll 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,newmask,val + xnew.second + dist[city][xnew.first]);
          }
      }
    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 2 ms 2644 KB Output is correct
3 Incorrect 2 ms 2772 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 237 ms 44704 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 3156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 279 ms 45588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 339 ms 46620 KB Output isn't correct
2 Halted 0 ms 0 KB -