# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
182152 | Ruxandra985 | Toll (BOI17_toll) | C++14 | 1279 ms | 173228 KiB |
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>
#define INF 2000000000
using namespace std;
map <int,int> rmq[20][50010];
map <int , pair <int,int> > mp;
int lg[50010] , step[50010];
deque <pair <int,int> > dq;
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int k , n , m , o , x , y, z, i , j , p , len , elem , nod , nr , nrr;
fscanf (fin,"%d%d%d%d",&k,&n,&m,&o);
for (i=1;i<=m;i++){
fscanf (fin,"%d%d%d",&x,&y,&z);
rmq[0][min(x , y)][max(x , y)] = z;
}
for (i=2;i<=n;i++)
lg[i] = 1 + lg[i/2];
for (p=1;(1<<p) <= n/k;p++){
for (i=0;i<=n;i++){
if (i / k + (1 << p) <= n / k){ /// e ok
for (j = (i / k + (1 << p)) * k ; j < (i / k + (1 << p) + 1) * k; j++){
for (x = (i / k + (1 << (p-1))) * k ; x < (i / k + (1 << (p-1)) + 1) * k; x++){
if (rmq[p-1][i][x] && rmq[p-1][x][j]){
if (!rmq[p][i][j])
rmq[p][i][j] = rmq[p-1][i][x] + rmq[p-1][x][j];
else rmq[p][i][j] = min(rmq[p][i][j] , rmq[p-1][i][x] + rmq[p-1][x][j]);
}
}
}
}
else break;
}
}
for (;o;o--){
fscanf (fin,"%d%d",&x,&y);
len = (y / k) - (x / k);
elem = 0;
while (len){
step[++elem] = lg[len];
len -= (1 << lg[len]);
}
dq.push_back(make_pair(x , 1));
mp[x] = make_pair(o , 0); /// frecventa + cost sa ajungi
while (!dq.empty()){
nod = dq.front().first;
nr = dq.front().second;
nrr = step[nr];
dq.pop_front();
if (nr == elem+1)
continue;
mp[nod].first = -o;
for (j = (nod / k + (1 << nrr)) * k ; j < (nod / k + (1 << nrr) + 1) * k; j++){
if (rmq[nrr][nod][j] && ((mp[j].first != o && mp[j].first != -o) || mp[j].second > mp[nod].second + rmq[nrr][nod][j])){
mp[j].second = mp[nod].second + rmq[nrr][nod][j];
if (mp[j].first != o){ /// nu e ACUM in deque
mp[j].first = o;
dq.push_back(make_pair(j , nr + 1));
}
}
}
}
if ((mp[y].first != o && mp[y].first != -o))
fprintf (fout,"-1\n");
else fprintf (fout,"%d\n",mp[y].second);
}
return 0;
}
Compilation message (stderr)
# | 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... |