이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
using namespace std;
typedef pair<int, int> pii;
typedef priority_queue<pii, vector<pii>, greater<pii>> MinHeap;
const int INF = (INT_MAX - 3) / 2;
const int borneMax = (int)(1e5) + 5;
int nbNod, nbArc, nbCen, nbReq;
vector<pii> adj[borneMax];
vector<int> cen;
vector<int> valeur(borneMax, INF);
bool exemple = false;
void lireEntree()
{
scanf("%d%d", &nbNod, &nbArc);
for (int ind = 0; ind < nbArc; ++ind)
{
int nod1, nod2, poids;
scanf("%d%d%d", &nod1, &nod2, &poids);
adj[nod1].push_back({nod2, poids});
adj[nod2].push_back({nod1, poids});
}
scanf("%d", &nbCen);
cen.resize(nbCen);
for (int ind = 0; ind < nbCen; ++ind)
{
scanf("%d", &cen[ind]);
}
}
void preparer()
{
MinHeap dij;
for (int nod : cen)
{
dij.push({nod, 0});
valeur[nod] = 0;
}
while (! dij.empty())
{
auto e = dij.top();
int nod = e.first, dis = e.second;
dij.pop();
if (dis > valeur[nod]) continue;
for (pii obj : adj[nod])
{
int voisin = obj.first, poids = obj.second;
if (dis + poids < valeur[voisin])
{
valeur[voisin] = dis + poids;
dij.push({voisin, dis + poids});
}
}
}
}
int solve(int nod1, int nod2)
{
for (auto obj : adj[nod1])
{
if (obj.first == nod2)
return min(valeur[nod1], valeur[nod2]);
}
priority_queue<pii> dij;
vector<int> meilleur(borneMax, 0);
dij.push({nod1, valeur[nod1]});
meilleur[nod1] = valeur[nod1];
while (! dij.empty())
{
auto e = dij.top();
int nod = e.first, dis = e.second;
dij.pop();
if (dis < meilleur[nod]) continue;
for (pii obj : adj[nod])
{
int voisin = obj.first;
int nv = min(dis, valeur[voisin]);
if (nv > meilleur[voisin])
{
meilleur[voisin] = nv;
dij.push({voisin, nv});
}
}
}
return meilleur[nod2];
}
int main()
{
lireEntree();
preparer();
scanf("%d", &nbReq);
for (int ind = 0; ind < nbReq; ++ind)
{
int nod1, nod2;
scanf("%d%d", &nod1, &nod2);
printf("%d\n", solve(nod1, nod2));
}
}
# | 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... |