답안 #182002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
182002 2020-01-09T11:20:50 Z Ruxandra985 Toll (BOI17_toll) C++14
0 / 100
493 ms 113680 KB
#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++){
                    rmq[p][i][j] = INF;
                        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])
                                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);

        if (x / k == y / k){
            fprintf (fout,"-1");
            continue;
        }

        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) || mp[y].second == INF)
            fprintf (fout,"-1\n");
        else fprintf (fout,"%d\n",mp[y].second);

    }

    return 0;
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:13:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d%d%d",&k,&n,&m,&o);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:15:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d%d",&x,&y,&z);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:38:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d",&x,&y);
         ~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 84596 KB Output is correct
2 Incorrect 46 ms 47356 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 493 ms 113680 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 47276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 47276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 84596 KB Output is correct
2 Incorrect 46 ms 47356 KB Output isn't correct
3 Halted 0 ms 0 KB -