이 제출은 이전 버전의 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 = 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[35][N],ans = inf;
ll b[N];
bool chkmask(ll mask,ll now){
ll new_mask = mask | now;
return new_mask == mask;
}
struct Node{
ll val,cur;
};
bool operator < (Node x,Node y){
return x.val < y.val||(x.val == y.val && x.cur < y.cur);
}
void Dij(){
for (ll i = 0;i < 33;i++)
for (ll j = 1;j <= n;j++) d[i][j] = inf;
for (ll i = 1;i <= k;i++) d[(1 << (i - 1))][a[i]] = 0;
for (ll mask = 1;mask < (1 << k);mask++){
for (ll m1 = 1;m1 <= mask;m1++){
if (!chkmask(mask,m1)) continue;
ll m2 = mask ^ m1;
for (ll i = 1;i <= n;i++) d[mask][i] = min(d[mask][i],d[m1][i] + d[m2][i]);
}
set<Node> s;
for (ll i = 1;i <= n;i++) if (d[mask][i] != inf) s.insert({d[mask][i],i});
while(!s.empty()){
Node t = *s.begin(); s.erase(s.begin());
ll u = t.cur;
for (auto i : g[u]){
ll v = i.fs,L = i.sc;
if (d[mask][v] > d[mask][u] + L){
s.erase({d[mask][v],v}); d[mask][v] = d[mask][u] + L;
s.insert({d[mask][v],v});
}
}
}
}
for (ll i = 1;i <= n;i++) ans = min(ans,d[(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});
}
Dij(); 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 'int main()':
cities.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
62 | 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... |