답안 #233693

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
233693 2020-05-21T11:29:16 Z Ruxandra985 Olympic Bus (JOI20_ho_t4) C++14
100 / 100
363 ms 4728 KB
#include <bits/stdc++.h>
#define DIMN 210
#define INF 2000000000
using namespace std;
int dp[DIMN] , dist[DIMN][DIMN];


priority_queue < pair <int , int > > h;

vector <pair <int , pair <int , int > > > v[DIMN];

struct idk{

    int x , y , z , c;

} mch[50010];

char buff[5000000];
int pos , n;

int getnr(){
    int nr = 0;

    while ('0' > buff[pos] || buff[pos] > '9')
        pos++;

    while ('0' <= buff[pos] && buff[pos] <= '9'){
        nr = nr * 10 + buff[pos] - '0';
        pos++;
    }

    return nr;

}

int dijkstra (int which){
    int i , val , nod , vecin , cost , nr;
    val = 0;
    for (i = 1 ; i <= n ; i++)
        dp[i] = INF;

    dp[1] = 0;
    h.push(make_pair(0 , 1));

    while (!h.empty()){

        nod = h.top().second;
        cost = -h.top().first;
        h.pop();

        if (dp[nod] != cost)
            continue;


        if (nod == mch[which].y){

            cost = dp[nod] + mch[which].z;

            if (dp[mch[which].x] > cost){
                dp[mch[which].x] = cost;
                h.push(make_pair(-cost , mch[which].x));
            }


        }


        for (i = 0 ; i < v[nod].size() ; i++){

            vecin = v[nod][i].first;
            nr = v[nod][i].second.second;
            if (nr != which){


                cost = dp[nod] + mch[nr].z;
                if (dp[vecin] > cost){
                    dp[vecin] = cost;
                    h.push(make_pair(-cost , vecin));
                }


            }

        }

    }

    val = dp[n];

    /// --------------------------------------------------------------------------


    for (i = 1 ; i <= n ; i++)
        dp[i] = INF;

    dp[n] = 0;
    h.push(make_pair(0 , n));

    while (!h.empty()){

        nod = h.top().second;
        cost = -h.top().first;
        h.pop();

        if (dp[nod] != cost)
            continue;


        if (nod == mch[which].y){

            cost = dp[nod] + mch[which].z;

            if (dp[mch[which].x] > cost){
                dp[mch[which].x] = cost;
                h.push(make_pair(-cost , mch[which].x));
            }


        }


        for (i = 0 ; i < v[nod].size() ; i++){

            vecin = v[nod][i].first;
            nr = v[nod][i].second.second;
            if (nr != which){


                cost = dp[nod] + mch[nr].z;
                if (dp[vecin] > cost){
                    dp[vecin] = cost;
                    h.push(make_pair(-cost , vecin));
                }


            }

        }

    }

    if (val > INF - dp[1])
        return INF;

    return val + dp[1];


}


int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int m , x , y , z , c , i , j , k;
    long long sol , val;
    fread (buff , 1 , 5000000 , fin);
    n = getnr();
    m = getnr();

    for (i = 1 ; i <= n ; i++){
        for (j = 1 ; j <= n ; j++){
            dist[i][j] = INF;
        }
        dist[i][i] = 0;
    }


    for (i = 1 ; i <= m ; i++){

        x = getnr();
        y = getnr();
        z = getnr();
        c = getnr();
        v[x].push_back(make_pair(y , make_pair(z , i)));
        mch[i] = {x , y , z , c};
        dist[x][y] = min(dist[x][y] , z);
    }

    for (k = 1 ; k <= n ; k++){

        for (i = 1 ; i <= n ; i++){

            for (j = 1 ; j <= n ; j++){

                if (i != j && i != k && j != k && dist[i][k] != INF && dist[k][j] != INF)
                    dist[i][j] = min(dist[i][j] , dist[i][k] + dist[k][j]);

            }
        }

    }

    sol = 1LL * dist[1][n] + dist[n][1];

    /// sol pt cand nu inversez nimic
    /// ce fac daca aleg sa inversez ceva?

    for (i = 1 ; i <= m ; i++){


        /// daca as inversa muchia i as rezolva ceva?

        val = min(1LL * dist[1][n] , 1LL * dist[1][mch[i].y] + dist[mch[i].x][n] + mch[i].z);
        val += min(1LL * dist[n][1] , 1LL * dist[n][mch[i].y] + mch[i].z + dist[mch[i].x][1]);

        if (val + mch[i].c < sol){
            sol = min(sol , 1LL * dijkstra (i) + mch[i].c);
        }


    }
    if (sol >= INF)
        sol = -1;
    fprintf (fout,"%lld",sol);

    return 0;
}

Compilation message

ho_t4.cpp: In function 'int dijkstra(int)':
ho_t4.cpp:68:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0 ; i < v[nod].size() ; i++){
                      ~~^~~~~~~~~~~~~~~
ho_t4.cpp:122:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0 ; i < v[nod].size() ; i++){
                      ~~^~~~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:157:11: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fread (buff , 1 , 5000000 , fin);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 640 KB Output is correct
2 Correct 18 ms 512 KB Output is correct
3 Correct 20 ms 640 KB Output is correct
4 Correct 20 ms 760 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 18 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 25 ms 632 KB Output is correct
11 Correct 33 ms 632 KB Output is correct
12 Correct 20 ms 640 KB Output is correct
13 Correct 21 ms 640 KB Output is correct
14 Correct 20 ms 512 KB Output is correct
15 Correct 21 ms 640 KB Output is correct
16 Correct 21 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 4224 KB Output is correct
2 Correct 26 ms 4352 KB Output is correct
3 Correct 24 ms 4608 KB Output is correct
4 Correct 20 ms 640 KB Output is correct
5 Correct 20 ms 512 KB Output is correct
6 Correct 20 ms 512 KB Output is correct
7 Correct 18 ms 512 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 34 ms 4608 KB Output is correct
10 Correct 30 ms 4608 KB Output is correct
11 Correct 27 ms 4608 KB Output is correct
12 Correct 28 ms 4608 KB Output is correct
13 Correct 27 ms 4608 KB Output is correct
14 Correct 26 ms 4728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 512 KB Output is correct
2 Correct 18 ms 512 KB Output is correct
3 Correct 23 ms 3200 KB Output is correct
4 Correct 19 ms 512 KB Output is correct
5 Correct 23 ms 3968 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 30 ms 4096 KB Output is correct
9 Correct 31 ms 4096 KB Output is correct
10 Correct 26 ms 4224 KB Output is correct
11 Correct 29 ms 4096 KB Output is correct
12 Correct 26 ms 4096 KB Output is correct
13 Correct 5 ms 432 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 26 ms 4096 KB Output is correct
20 Correct 27 ms 4096 KB Output is correct
21 Correct 26 ms 4096 KB Output is correct
22 Correct 26 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 640 KB Output is correct
2 Correct 18 ms 512 KB Output is correct
3 Correct 20 ms 640 KB Output is correct
4 Correct 20 ms 760 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 18 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 25 ms 632 KB Output is correct
11 Correct 33 ms 632 KB Output is correct
12 Correct 20 ms 640 KB Output is correct
13 Correct 21 ms 640 KB Output is correct
14 Correct 20 ms 512 KB Output is correct
15 Correct 21 ms 640 KB Output is correct
16 Correct 21 ms 640 KB Output is correct
17 Correct 24 ms 4224 KB Output is correct
18 Correct 26 ms 4352 KB Output is correct
19 Correct 24 ms 4608 KB Output is correct
20 Correct 20 ms 640 KB Output is correct
21 Correct 20 ms 512 KB Output is correct
22 Correct 20 ms 512 KB Output is correct
23 Correct 18 ms 512 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 34 ms 4608 KB Output is correct
26 Correct 30 ms 4608 KB Output is correct
27 Correct 27 ms 4608 KB Output is correct
28 Correct 28 ms 4608 KB Output is correct
29 Correct 27 ms 4608 KB Output is correct
30 Correct 26 ms 4728 KB Output is correct
31 Correct 20 ms 512 KB Output is correct
32 Correct 18 ms 512 KB Output is correct
33 Correct 23 ms 3200 KB Output is correct
34 Correct 19 ms 512 KB Output is correct
35 Correct 23 ms 3968 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 5 ms 384 KB Output is correct
38 Correct 30 ms 4096 KB Output is correct
39 Correct 31 ms 4096 KB Output is correct
40 Correct 26 ms 4224 KB Output is correct
41 Correct 29 ms 4096 KB Output is correct
42 Correct 26 ms 4096 KB Output is correct
43 Correct 5 ms 432 KB Output is correct
44 Correct 4 ms 384 KB Output is correct
45 Correct 4 ms 384 KB Output is correct
46 Correct 4 ms 384 KB Output is correct
47 Correct 4 ms 384 KB Output is correct
48 Correct 4 ms 384 KB Output is correct
49 Correct 26 ms 4096 KB Output is correct
50 Correct 27 ms 4096 KB Output is correct
51 Correct 26 ms 4096 KB Output is correct
52 Correct 26 ms 4096 KB Output is correct
53 Correct 23 ms 4224 KB Output is correct
54 Correct 24 ms 4096 KB Output is correct
55 Correct 24 ms 4224 KB Output is correct
56 Correct 20 ms 640 KB Output is correct
57 Correct 20 ms 640 KB Output is correct
58 Correct 25 ms 3456 KB Output is correct
59 Correct 26 ms 3576 KB Output is correct
60 Correct 27 ms 3456 KB Output is correct
61 Correct 26 ms 3456 KB Output is correct
62 Correct 60 ms 3576 KB Output is correct
63 Correct 122 ms 3456 KB Output is correct
64 Correct 80 ms 3832 KB Output is correct
65 Correct 195 ms 3704 KB Output is correct
66 Correct 363 ms 3832 KB Output is correct
67 Correct 9 ms 3324 KB Output is correct
68 Correct 31 ms 4608 KB Output is correct
69 Correct 29 ms 4608 KB Output is correct
70 Correct 28 ms 4608 KB Output is correct
71 Correct 28 ms 4728 KB Output is correct
72 Correct 27 ms 4480 KB Output is correct
73 Correct 27 ms 4608 KB Output is correct
74 Correct 27 ms 4608 KB Output is correct