제출 #361682

#제출 시각아이디문제언어결과실행 시간메모리
361682cpp219Cities (BOI16_cities)C++14
51 / 100
6030 ms142540 KiB
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 1e5 + 69;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;
vector<LL> g[N];
ll n,k,m,a[N],c,x,y,D[9][N],d[N],dp[N],ans = inf;
ll b[N];
void Dij(ll scr){
    set<LL> s; fill(d,d + n + 1,inf);
    s.insert({0,a[scr]}); d[a[scr]] = 0;
    while(!s.empty()){
        LL t = *s.begin(); s.erase(s.begin());
        ll u = t.sc;
        for (auto i : g[u]){
            ll v = i.fs,L = i.sc;
            if (d[v] > d[u] + L){
                s.erase({d[v],v}); d[v] = d[u] + L;
                s.insert({d[v],v});
            }
        }
    }
    for (ll i = 1;i <= n;i++) D[scr][i] = d[i];
}

bool chkbit(ll x,ll i){
    return (x >> i) & 1;
}

bool chkmask(ll mask,ll now){
    ll p = mask & now;
    return !p;
}

ll cal(ll mask,ll ver){
    ll res = 0;
    for (ll i = 1;i <= k;i++) if (chkbit(mask,i - 1)) res += D[i][ver];
    return res;
}

ll dis[40][N];
struct Node{
    ll val,mask,cur;
};
bool operator < (Node x,Node y){
    return x.val < y.val||(x.val == y.val && x.mask < y.mask)||
    (x.val == y.val && x.mask == y.mask && x.cur < y.cur);
}
set<Node> s;
ll Find(ll ver){
    for (ll i = 1;i <= k;i++) if (a[i] == ver) return i;
    return 0;
}

void Minimize(ll new_mask,ll v,ll val){
    if (dis[new_mask][v] > val){
        s.erase({dis[new_mask][v],new_mask,v}); dis[new_mask][v] = val;
        s.insert({dis[new_mask][v],new_mask,v});
    }
}

void Gay_dij(ll now,ll scr){
    for (ll i = 1;i <= n;i++)
        for (ll j = 0;j < 40;j++) dis[j][i] = inf;
    s.insert({0,now,scr}); dis[now][scr] = 0;
    while(!s.empty()){
        Node t = *s.begin(); s.erase(s.begin());
        ll u = t.cur,mask = t.mask;
        for (ll i = 0;i < (1 << k);i++){
            if (!chkmask(mask,i)) continue;
            ll new_mask = mask | i,inc = cal(i,u); Minimize(new_mask,u,inc + dis[mask][u]);
            for (auto i : g[u]){
                ll v = i.fs,L = i.sc,p = Find(i.fs);
                if (p) new_mask | (1 << (p - 1));
                Minimize(new_mask,v,dis[mask][u] + inc + L);
            }
        }
    }
    for (ll i = 1;i <= n;i++) ans = min(ans,dis[(1 << k) - 1][i]);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define task "tst"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>n>>k>>m;
    for (ll i = 1;i <= k;i++) cin>>a[i];
    while(m--){
        cin>>x>>y>>c;
        g[x].push_back({y,c}); g[y].push_back({x,c});
    }
    for (ll i = 1;i <= k;i++) Dij(i); Gay_dij(1,a[1]); //cout<<dis[1][2]; return 0;
    cout<<ans;
}

컴파일 시 표준 에러 (stderr) 메시지

cities.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
cities.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
cities.cpp: In function 'void Gay_dij(long long int, long long int)':
cities.cpp:82:33: warning: statement has no effect [-Wunused-value]
   82 |                 if (p) new_mask | (1 << (p - 1));
      |                        ~~~~~~~~~^~~~~~~~~~~~~~~~
cities.cpp: In function 'int main()':
cities.cpp:104:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  104 |     for (ll i = 1;i <= k;i++) Dij(i); Gay_dij(1,a[1]); //cout<<dis[1][2]; return 0;
      |     ^~~
cities.cpp:104:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  104 |     for (ll i = 1;i <= k;i++) Dij(i); Gay_dij(1,a[1]); //cout<<dis[1][2]; return 0;
      |                                       ^~~~~~~
cities.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   95 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...