이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 2e5 + 69;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;
vector<LL> g[N];
ll n,k,m,a[69],c,x,y,D[6][N],d[N],dp[69],ans = inf,cand;
ll b[69];
void Dij(ll scr){
set<LL> s; fill(d,d + n + 1,inf);
s.insert({0,scr}); d[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[a[i]][ver];
return res;
}
ll f(ll mask){
//cout<<mask<<"\n";
if (mask == (1 << k) - 1) return 0;
if (dp[mask] != -1) return dp[mask];
ll kq = inf;
for (ll i = 0;i < k;i++){
if (chkbit(mask,i)){
for (ll j = 1;j <= n;j++){
for (ll l = 1;l <= (1 << k) - 1;l++){
if (chkmask(mask,l)){
kq = min(kq,cal(l,j) + D[a[i + 1]][j] + f(mask | l));
}
}
}
}
}
return dp[mask] = kq;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
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 <= n;i++) Dij(a[i]); memset(dp,-1,sizeof(dp));
cout<<f(1);
}
컴파일 시 표준 에러 (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 'int main()':
cities.cpp:82:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
82 | for (ll i = 1;i <= n;i++) Dij(a[i]); memset(dp,-1,sizeof(dp));
| ^~~
cities.cpp:82:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
82 | for (ll i = 1;i <= n;i++) Dij(a[i]); memset(dp,-1,sizeof(dp));
| ^~~~~~
cities.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
73 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |