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 "cyberland.h"
#include <bits/stdc++.h>
using namespace std;
const int INFINI = 1e17;
const int MAXI_NOEUDS = 100*1000 + 100;
const int MAXI_K = 70;
vector<int> pouvoirs;
int nbNoeuds, nbAretes, K, fin;
bool finAccess;
struct Arc{
int dest;
int poids;
};
struct Sit{
int node;
int nbDiv;
double dis;
bool operator <(const Sit &other) const{
return dis > other.dis;
}
};
vector<Arc> adj[MAXI_NOEUDS];
int zerosAccess[MAXI_NOEUDS];
int dejaVu[MAXI_NOEUDS];
void dfs(int n){
if(dejaVu[n] == 1)
return;
dejaVu[n] = 1;
if(n == fin){
return;
}
if(pouvoirs[n] == 0)
zerosAccess[n] = 1;
for(int v=0; v<(int)adj[n].size(); v++){
dfs(adj[n][v].dest);
}
}
priority_queue<Sit> aTraiter;
int deja[MAXI_NOEUDS][MAXI_K + 1];
double distances[MAXI_NOEUDS][MAXI_K + 1];
double djikstra(){
aTraiter.push({fin, 0, 0});
distances[fin][0] = 0;
while(!aTraiter.empty()){
Sit curSit = aTraiter.top();
aTraiter.pop();
if(curSit.node == fin && curSit.nbDiv != 0)
continue;
if(deja[curSit.node][curSit.nbDiv])
continue;
if(zerosAccess[curSit.node] == 1){
return curSit.dis;
}
deja[curSit.node][curSit.nbDiv] = 1;
for(int i=0; i<(int)adj[curSit.node].size(); i++){
Arc voisin = adj[curSit.node][i];
int v = voisin.dest;
int w = voisin.poids;
// pas diviser par 2
if( distances[curSit.node][curSit.nbDiv] + (double)w/(double)pow(2, curSit.nbDiv) < distances[v][curSit.nbDiv]){
distances[v][curSit.nbDiv] = distances[curSit.node][curSit.nbDiv] + (double)w/(double)pow(2, curSit.nbDiv);
aTraiter.push({v, curSit.nbDiv, distances[v][curSit.nbDiv]});
}
// diviser par 2
if(pouvoirs[v] == 2 && curSit.nbDiv < K){
if(distances[curSit.node][curSit.nbDiv] + (double)w/(double)pow(2, curSit.nbDiv) < distances[v][curSit.nbDiv + 1]){
distances[v][curSit.nbDiv + 1] = distances[curSit.node][curSit.nbDiv] + (double)w/(double)pow(2, curSit.nbDiv);
aTraiter.push({v, curSit.nbDiv+1, distances[v][curSit.nbDiv + 1]});
}
}
}
}
return -1;
}
double solve(int N, int M, int k, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
pouvoirs.clear();
finAccess = false;
while(!aTraiter.empty()){
aTraiter.pop();
}
for(int i=0; i<MAXI_NOEUDS; i++){
adj[i].clear();
zerosAccess[i] = 0;
dejaVu[i] = 0;
for(int j=0; j<MAXI_K + 1; j++){
deja[i][j] = 0;
distances[i][j] = INFINI;
}
}
zerosAccess[0] = 1;
pouvoirs = arr;
fin = H;
nbNoeuds = N;
nbAretes = M;
K = min(k, MAXI_K);
for(int n=0; n<N; n++){
adj[x[n]].push_back({y[n], c[n]});
adj[y[n]].push_back({x[n], c[n]});
}
dfs(0);
if(!dejaVu[fin])
return -1;
else
return djikstra();
}
Compilation message (stderr)
cyberland.cpp:5:20: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+17' to '2147483647' [-Woverflow]
5 | const int INFINI = 1e17;
| ^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |