이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ar array< long long , 2>
#define arr array< long long , 3>
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
const long long MAX = 1e6 + 1000;
long long d[MAX], n, m, x, y, z, npp[MAX], k, q;
bool NPP[MAX];
vector < ar > A[MAX];
void DFS(long long x)
{
for (ar y : A[x])
{
if (!NPP[y[0]] && d[y[0]] > d[x] + y[1])
{
d[y[0]] = d[x] + y[1];
DFS(y[0]);
}
}
}
long long BFS(long long s, long long t)
{
vector < long long > dd;
dd.resize(n + 100, 0);
priority_queue< ar > QU;
QU.push({d[s], s});
while (!QU.empty())
{
ar x = QU.top();
QU.pop();
if (x[1] == t) return x[0];
for (ar y : A[x[1]])
{
if (dd[y[0]] < min(d[y[0]], x[0]))
{
dd[y[0]] = min(d[y[0]], x[0]);
QU.push({dd[y[0]], y[0]});
}
}
}
}
void BFS2()
{
queue< long long > QU;
for (long long i = 1; i <= k; i++)
{
QU.push(npp[i]);
}
while (!QU.empty())
{
long long x = QU.front();
QU.pop();
for (ar y : A[x])
{
if (d[y[0]] > d[x] + y[1])
{
d[y[0]] = d[x] + y[1];
QU.push(y[0]);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
memset(d, 0x3f, sizeof d);
for (long long i = 1; i <= m; i++)
{
cin >> x >> y >> z;
A[x].push_back({y, z});
A[y].push_back({x, z});
}
cin >> k;
for (long long i = 1; i <= k; i++)
{
cin >> npp[i];
NPP[npp[i]] = 1;
d[npp[i]] = 0;
}
BFS2();
//for (long long i = 1; i <= n; i++) cout << d[i] << " " ;
cin >> q;
for (long long i = 1; i <= q; i++)
{
cin >> x >> y;
if (NPP[x] || NPP[y]) cout << 0 << '\n' ;
else
cout << BFS(x, y) << '\n' ;
}
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'long long int BFS(long long int, long long int)':
plan.cpp:26:26: warning: control reaches end of non-void function [-Wreturn-type]
26 | vector < long long > dd;
| ^~
# | 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... |