이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> ii;
const int N = 2e6+7;
int n,m,k,q,NNP[N];
vector<ii> adj[N];
vector<int> dist(N,1e9+7);
void DIJKSTRA() {
priority_queue< ii, vector<ii>, greater<ii> > dt;
for(int i=1;i<=n;i++) {
if(NNP[i] == 1) {
dist[i] = 0;
dt.push({0,i});
}
}
while(!dt.empty()) {
int u = dt.top().se;
int val = dt.top().fi;
dt.pop();
if(dist[u] != val) continue;
for(ii w : adj[u]) {
int v = w.se;
int he = w.fi;
if(dist[v] > he + val) {
dist[v] = he + val;
dt.push({dist[v], v});
}
}
}
}
bool check(int val,int x,int y) {
queue<int> s;
vector<int> ok(n+1,0);
ok[x] = 1;
if(dist[x] >= val) s.push(x);
while(!s.empty()) {
int u = s.front(); s.pop();
for(ii w : adj[u]) {
int v = w.se;
if(ok[v] == 0 && dist[v] >= val) {
ok[v] = 1;
if(v == y) return true;
s.push(v);
}
}
}
if(ok[y] == 1) return true;
return false;
}
void solve(int u,int v) {
int l = 0;
int r = 1e8;
int ans;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid,u,v) == true) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
cout<<ans<<'\n';
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++) {
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({w,v});
adj[v].push_back({w,u});
}
cin>>k;
while(k--) {
int x;
cin>>x;
NNP[x] = 1;
}
DIJKSTRA();
cin>>q;
while(q--) {
int u,v;
cin>>u>>v;
solve(u,v);
}
//for(int i=1;i<=n;i++) cout<<dist[i]<<" ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void solve(int, int)':
plan.cpp:64:13: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
64 | cout<<ans<<'\n';
| ^~~~
# | 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... |