이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
using namespace std;
int n, m, k;
vector< pair<int, int> > g[100005];
bool isnpp[100005] = {0};
int cln[100005] = {0}, npp[100005] = {0};
bool used[100005] = {0};
void getclosestnpp()
{
priority_queue< pair<int, int> > pq;
for(int i = 0; i < k; i++)
{
pq.push({0, npp[i]});
}
while(!pq.empty())
{
int c = -pq.top().first, v = pq.top().second;
pq.pop();
if(!used[v])
{
cln[v] = c;
used[v] = true;
for(pair<int, int> u : g[v])
{
if(!used[u.first]) pq.push({-c-u.second, u.first});
}
}
}
}
int solve(int s, int t)
{
if(isnpp[s] || isnpp[t]) return 0;
for(int i = 1; i <= n; i++) used[i] = 0;
priority_queue< pair<int, int> > pq;
pq.push({cln[s], s});
while(!pq.empty())
{
int d = pq.top().first, v = pq.top().second;
pq.pop();
if(v == t)
{
return d;
}
if(!used[v])
{
used[v] = true;
for(pair<int, int> u : g[v])
{
if(!used[u.first]) pq.push({min(d, cln[u.first]), u.first});
}
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i = 0; i < m; i++)
{
int x, y, w;
cin>>x>>y>>w;
g[x].push_back({y, w});
g[y].push_back({x, w});
}
cin>>k;
for(int i = 0; i < k; i++)
{
cin>>npp[i];
isnpp[npp[i]] = true;
}
getclosestnpp();
int q;
cin>>q;
for(int i = 0; i < q; i++)
{
int s, t;
cin>>s>>t;
cout<<solve(s, t)<<'\n';
}
return 0;
}
# | 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... |