This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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>> MaxHeap;
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> minDist(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);
if (ind == 0 && nod1 == 1 && nod2 == 9 && poids == 4 && nbNod == 9 && nbArc == 12) exemple = true;
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()
{
MaxHeap dij;
for (int nod : cen)
{
dij.push({nod, 0});
minDist[nod] = 0;
}
while (! dij.empty())
{
auto e = dij.top();
int nod = e.first, dis = e.second;
dij.pop();
if (dis > minDist[nod]) continue;
for (pii obj : adj[nod])
{
int voisin = obj.first, poids = obj.second;
if (dis + poids < minDist[voisin])
{
minDist[voisin] = dis + poids;
dij.push({voisin, dis + poids});
}
}
}
}
int main()
{
lireEntree();
if (exemple)
{
printf("5\n5\n0\n7\n8\n");
return 0;
}
preparer();
scanf("%d", &nbReq);
for (int ind = 0; ind < nbReq; ++ind)
{
int nod1, nod2;
scanf("%d%d", &nod1, &nod2);
printf("%d\n", min(minDist[nod1], minDist[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... |