Submission #220946

# Submission time Handle Problem Language Result Execution time Memory
220946 2020-04-09T10:18:58 Z Ruxandra985 Cities (BOI16_cities) C++14
74 / 100
6000 ms 169876 KB
#include <bits/stdc++.h>
#define DIMN 100010
#define INF 1000000000000000
using namespace std;
long long dist[64][DIMN];
int sp[DIMN] , poz[DIMN];
vector <pair <int,int> > v[DIMN];

priority_queue <pair <long long , pair <int,int> > > h;

char buff[6000000];
int pos = 0;

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 main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , m , k , i , j , x , y , nod , vecin , c , cst , mask , st , big;
    long long sum = 0 , cost;
    fread (buff , 1 , 6000000 , fin);
    n = getnr();
    k = getnr();
    m = getnr();

    for (i = 1 ; i <= k ; ++i){
        sp[i] = getnr();
        poz[sp[i]] = i;
    }

    for (i = 1 ; i <= m ; ++i){
        x = getnr();
        y = getnr();
        c = getnr();
        v[x].push_back(make_pair(y , c));
        v[y].push_back(make_pair(x , c));
    }
    for (i = 0 ; i < 32 ; ++i){
        for (j = 1 ; j <= n ; ++j)
            dist[i][j] = INF;
    }

    for (j = 1 ; j <= n ; ++j){
        if (poz[j]){
            dist[1 << (poz[j] - 1)][j] = 0;
            h.push(make_pair(0 , make_pair(1 << (poz[j] - 1) , j)));
        }
        else {
            dist[0][j] = 0;
            h.push(make_pair(0 , make_pair(0 , j)));
        }
    }


    while (!h.empty()){

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

        if (mask == (1 << k) - 1){
            fprintf (fout,"%lld",cost);
            return 0;
        }


        if (dist[mask][nod] != cost)
            continue; /// nu e ok

        /// acuma trb sa vad cum se pot combina starile

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

            big = ((1 << k) - 1) ^ mask;

            for (st = big ; st ; st = (st - 1) & big ){

                if (dist[mask | st][vecin] > dist[mask][nod] + cst + dist[st][vecin]){
                    dist[mask | st][vecin] = dist[mask][nod] + cst + dist[st][vecin];
                    h.push(make_pair(-dist[mask | st][vecin] , make_pair(mask|st , vecin)));
                }

            }

            if (dist[mask | st][vecin] > dist[mask][nod] + cst + dist[st][vecin]){
                dist[mask | st][vecin] = dist[mask][nod] + cst + dist[st][vecin];
                h.push(make_pair(-dist[mask | st][vecin] , make_pair(mask|st , vecin)));
            }

        }

    }

    return 0;
}

Compilation message

cities.cpp: In function 'int main()':
cities.cpp:87:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i = 0 ; i < v[nod].size() ; ++i){
                      ~~^~~~~~~~~~~~~~~
cities.cpp:34:15: warning: unused variable 'sum' [-Wunused-variable]
     long long sum = 0 , cost;
               ^~~
cities.cpp:35: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 , 6000000 , fin);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2816 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 6 ms 2944 KB Output is correct
5 Correct 7 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 582 ms 54736 KB Output is correct
2 Correct 610 ms 54476 KB Output is correct
3 Correct 144 ms 35184 KB Output is correct
4 Correct 42 ms 12028 KB Output is correct
5 Correct 212 ms 42464 KB Output is correct
6 Correct 25 ms 11264 KB Output is correct
7 Correct 8 ms 3456 KB Output is correct
8 Correct 7 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3840 KB Output is correct
2 Correct 13 ms 3840 KB Output is correct
3 Correct 8 ms 3328 KB Output is correct
4 Correct 10 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2540 ms 104148 KB Output is correct
2 Correct 2084 ms 103588 KB Output is correct
3 Correct 1213 ms 49672 KB Output is correct
4 Correct 1437 ms 58300 KB Output is correct
5 Correct 209 ms 30432 KB Output is correct
6 Correct 96 ms 14848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6067 ms 169876 KB Time limit exceeded
2 Halted 0 ms 0 KB -