CSES - Datatähti 2025 loppu - Results
Submission details
Task:Suunnistus
Sender:Kemm1706
Submission time:2025-01-18 16:40:36 +0200
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED31
#2ACCEPTED12
#3ACCEPTED57
Test results
testverdicttimegroup
#1ACCEPTED0.00 s3details
#2ACCEPTED0.03 s1, 2, 3details
#3ACCEPTED0.02 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.00 s1, 2, 3details
#6ACCEPTED0.00 s1, 2, 3details
#7ACCEPTED0.19 s2, 3details
#8ACCEPTED0.28 s2, 3details
#9ACCEPTED0.01 s2, 3details
#10ACCEPTED0.52 s3details
#11ACCEPTED0.53 s3details
#12ACCEPTED0.01 s3details
#13ACCEPTED0.27 s3details
#14ACCEPTED0.05 s2, 3details
#15ACCEPTED0.00 s3details
#16ACCEPTED0.00 s3details
#17ACCEPTED0.04 s2, 3details

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:78:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |         auto [dis, no] = pq[c - 1].top(); pq[c - 1].pop();
      |              ^
input/code.cpp:79:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   79 |         auto [x, y] = no;
      |              ^
input/code.cpp:94:37: warning: comparison of integer expressions of different signedness: 'std::priority_queue<std::pair<long long int, std::pair<long long int, long long int> >, std::vector<std::pair<long long int, std::pair<long long int, long long int> > >, std::greater<std::pair<long long int, std::pair<long long int, long long int> > > >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} [-Wsign-compare]
   94 |                     if(pq[c].size() == fre[c])
input/code.cpp:125:18: warning: structur...

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef pair <ll, ll> pl;
typedef vector <pl> vpl;
typedef stack <ll> stl;
typedef set <ll> sl;
typedef vector <char> vc;
typedef vector <vc> vvc;
typedef vector <bool> vb;
typedef pair <ll, pl> llpl;

#define fi first
#define se second
#define pb push_back

const ll inf = 1e18;

pl d[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll cvt(ll x, ll y, ll m)
{
    return x * m + y;
}

bool valid(ll x, ll y, ll n, ll m, const vvc &g)
{
    return (x < n && x >= 0 && y < m && y >= 0 && g[x][y] != '#');
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m, i, j, k, c = 1;
    bool flag = false;
    pl b;
    string s;
    cin >> n >> m >> k;
    k++;
    vl fre(k + 1); fre[0] = fre[k] = 1;
    vvc g(n, vc(m));
    vector <vb> vis(n, vb(m, false));
    for(i = 0; i < n; i++)
    {
        cin >> s;
        for(j = 0; j < m; j++)
        {
            g[i][j] = s[j];
            if(s[j] == 'S')
            {
                b.fi = i; b.se = j;
                vis[i][j] = true;
            }
            else
                if(s[j] == 'E')
                    g[i][j] = char(k + '0');
                else
                    if(s[j] != '.' && s[j] != '#')
                    {
                        //cerr << g[i][j] - '0' << " ";
                        fre[g[i][j] - '0']++;
                    }
        }
    }

    /*for(i = 0; i <= k; i++)
        cerr << fre[i] << " ";*/

    //vector <map <pl, ll>> vmp(k + 2);
    vector <priority_queue <llpl, vector <llpl>, greater <llpl>>> pq(k + 2); pq[0].push({0, b});
    while(!pq[c - 1].empty())
    {
        flag = false;
        auto [dis, no] = pq[c - 1].top(); pq[c - 1].pop();
        auto [x, y] = no;
        vis[x][y] = true;
        //cerr << x << " " << y << "\n";
        for(i = 0; i < 4; i++)
        {
            ll xx = x + d[i].fi, yy = y + d[i].se;
            if(valid(xx, yy, n, m, g) && !vis[xx][yy])
            {
                vis[xx][yy] = true;
                if(g[xx][yy] == c + '0')
                {
                    pq[c].push({dis + 1, {xx, yy}});
                    //vmp[c][{xx, yy}] = pq[c].size();
                    //cerr << "{" << xx << ", " << yy << "} ";
                    //cerr << pq[c].size() << "!\n";
                    if(pq[c].size() == fre[c])
                    {
                        flag = true;
                        break;
                    }
                }
                pq[c - 1].push({dis + 1, {xx, yy}});
            }
        }
        if(flag)
        {
            c++;
            //cerr << "\n" << c << ": ";
            for(ll z = 0; z < n; z++)
                fill(vis[z].begin(), vis[z].end(), false);
        }
        if(c > k)
            break;
        if(pq[c - 1].empty() && !pq[c].empty())
        {
            for(ll z = 0; z < n; z++)
                fill(vis[z].begin(), vis[z].end(), false);
            c++;
        }
    }
    //cerr << "!!!" << c << " " << k << "\n";
    if(c > k)
    {
        ll ans = inf;
        while(pq[c - 1].size())
        {
            auto [dis, z] = pq[c - 1].top(); pq[c - 1].pop();
            ans = min(ans, dis);
            //cerr << ans;
        }
        if(ans == inf)
            cout << -1;
        else
            cout << ans;
    }
    else
        cout << -1;

    return 0;
}

/*
7 7 9
##..#9#
#8....#
9##.67E
1#S3..1
3#..4#8
.#.####
..25.#9
*/

Test details

Test 1

Group: 3

Verdict: ACCEPTED

input
10 10 9
S293#35616
#662963731
54975451#7
5162589168
...

correct output
25

user output
25

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
500 500 0
.................................

correct output
301

user output
301

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
500 500 0
.#.........#.#..##..#............

correct output
253

user output
253

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
500 500 0
...#......##.##.#.#..##..#..##...

correct output
-1

user output
-1

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
500 1 0
.
.
.
.
...

correct output
77

user output
77

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
1 500 0
.................................

correct output
166

user output
166

Test 7

Group: 2, 3

Verdict: ACCEPTED

input
500 500 9
.................................

correct output
3447

user output
3447

Test 8

Group: 2, 3

Verdict: ACCEPTED

input
500 500 9
.#........#..................#...

correct output
4952

user output
4952

Test 9

Group: 2, 3

Verdict: ACCEPTED

input
500 500 9
##.########.##########.#..#......

correct output
-1

user output
-1

Test 10

Group: 3

Verdict: ACCEPTED

input
500 500 9
623475428948841896621266296765...

correct output
205

user output
205

Test 11

Group: 3

Verdict: ACCEPTED

input
500 500 9
7##814125813#3463#272134469457...

correct output
157

user output
157

Test 12

Group: 3

Verdict: ACCEPTED

input
500 500 9
##67##36##5#3###67###8972#61##...

correct output
-1

user output
-1

Test 13

Group: 3

Verdict: ACCEPTED

input
500 500 9
....................#...#........

correct output
1313

user output
1313

Test 14

Group: 2, 3

Verdict: ACCEPTED

input
499 499 9
S#...#...#...#...#...#...#...#...

correct output
1124942

user output
1124942

Test 15

Group: 3

Verdict: ACCEPTED

input
500 1 9
1
6
1
3
...

correct output
332

user output
332

Test 16

Group: 3

Verdict: ACCEPTED

input
1 500 9
996327784392827829434482995353...

correct output
135

user output
135

Test 17

Group: 2, 3

Verdict: ACCEPTED

input
500 500 9
.................................

correct output
-1

user output
-1