CSES - HIIT Open 2019 - Results
Submission details
Task:HIIT Speedrun
Sender:Varokaa J:tä
Submission time:2019-05-25 13:25:02 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.02 sdetails
#5ACCEPTED0.02 sdetails
#6ACCEPTED0.02 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.02 sdetails
#10ACCEPTED0.02 sdetails
#11ACCEPTED0.03 sdetails
#12ACCEPTED0.03 sdetails
#13ACCEPTED0.02 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

int n, m;
char t[111];
int g[111][111];
vector<int> connections[111];
set<string> perms;

int visited[111];

int dfs(int i, const string& hiit, int pos) {
    char c = hiit[pos-1];
    if (visited[i] == pos) return 1e6;
    int old_vis = visited[i];
    if (t[i] == c) {
        if (pos == 5) return 0;
        int best = 1e6;
        visited[i] = pos+1;
        for (int n : connections[i]) {
            int v = dfs(n, hiit, pos+1) + 1;
            if (v < best) best = v;
        }
        visited[i] = old_vis;
        return best;
    }
    int best = 1e6;
    visited[i] = pos;
    for (int n : connections[i]) {
        int v = dfs(n, hiit, pos) + 1;
        if (v < best) best = v;
    }
    visited[i] = old_vis;
    return best;
}

struct state {
    int pos;
    int length;
    int i;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> t[i];    
    }
    
    for (int i = 0; i <= n+1; i++) {
        for (int j = 0; j <= n+1; j++) {
            if (i == j) continue;
            g[i][j] = 1e6;
        }
    }
    g[0][1] = 0;
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        connections[a].push_back(b);
        connections[b].push_back(a);
        g[a][b] = g[b][a] = 1;
    }
    connections[n].push_back(n+1);
    g[n][n+1] = 0;
    t[n+1] = '#';
    
    for (int i = 0; i <= n+1; i++) {
        for (int j = 0; j <= n+1; j++) {
            for (int k = 0; k <= n+1; k++) {
                if (g[i][j] > g[i][k] + g[k][j]) g[i][j] = g[i][k] + g[k][j];
            }
        }
    }
    
    
    /*for (int i = 0; i <= n+1; i++) {
        for (int j = 0; j <= n+1; j++) {
            if (g[i][j] < 10) cout << g[i][j] << " ";
            else cout << "X ";
        }
        cout << endl;
    }*/
    
    string hiit = "HIIT";
    sort(hiit.begin(), hiit.end());
    int best = 1e6;
    do {
        vector<state> starts;
        starts.push_back({0, 0, 0});
        
        for (char c : hiit + "#") {
            vector<state> new_starts(111);
            for (auto s : starts) {
                for (int i = 1; i <= n+1; i++) {
                    if (g[s.pos][i] > 1000) continue;
                    if (t[i] != c) continue;
                    if (i == s.i) continue;
                    int f = s.i;
                    if (!s.i && c == 'I') f = i;
                    if (!new_starts[i].pos || new_starts[i].length > s.length+g[s.pos][i]) {
                        new_starts[i] = {i, s.length+g[s.pos][i], f};
                    }
                }
            }
            starts.clear();
            for (auto s : new_starts) {
                if (!s.pos) continue;
                starts.push_back(s);
            }
        }
        for (auto s : starts) {
            if (s.length < best) best = s.length;
//            cout << hiit << " " << s.pos << " " << s.length << " " << s.i << "\n";
        }
        
    } while (next_permutation(hiit.begin(), hiit.end()));
    
    if (best > 1e5) cout << "IMPOSSIBLE\n";
    else     cout << best << "\n";
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
4 0
HIIT

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 2

Verdict: ACCEPTED

input
4 3
HIIT
1 2
2 3
3 4

correct output
3

user output
3

Test 3

Verdict: ACCEPTED

input
4 3
HIIT
4 3
3 2
2 1

correct output
3

user output
3

Test 4

Verdict: ACCEPTED

input
6 5
XHIITX
1 2
1 3
1 4
...

correct output
9

user output
9

Test 5

Verdict: ACCEPTED

input
100 5000
TIIIIIIITHHHTHHHIHIIHIHHHITHIH...

correct output
3

user output
3

Test 6

Verdict: ACCEPTED

input
100 99
HIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
99

user output
99

Test 7

Verdict: ACCEPTED

input
10 10
AAAHIITAAA
1 2
2 3
3 4
...

correct output
9

user output
9

Test 8

Verdict: ACCEPTED

input
9 9
HIIIIIIIT
1 2
2 3
3 4
...

correct output
4

user output
4

Test 9

Verdict: ACCEPTED

input
9 9
HIIIIAAAT
1 2
2 3
3 4
...

correct output
5

user output
5

Test 10

Verdict: ACCEPTED

input
100 99
HIIIIIIIIIIIIIIIIIIIIIIIIIIIII...

correct output
197

user output
197

Test 11

Verdict: ACCEPTED

input
8 8
HIITHIIT
1 2
2 3
3 4
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 12

Verdict: ACCEPTED

input
6 5
HIIITI
1 2
2 3
3 4
...

correct output
IMPOSSIBLE

user output
IMPOSSIBLE

Test 13

Verdict: ACCEPTED

input
100 99
HHHHHHHHHHHHHHHHHHHHHHHHHIIIII...

correct output
99

user output
99