Submission #21310886


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}

void solve(){
    int n,m;
    cin>>n>>m;
    vector<vector<pair<int,char>>> e(n);
    bool v[1000][1000]={};
    for(int i{},a,b;i<(m);++i){
        char c;
        cin>>a>>b>>c;--a,--b;
        v[a][b]=v[b][a]=true;
        e[a].emplace_back(b,c);
        e[b].emplace_back(a,c);
    }
    using ti = tuple<int,int,int>;
    priority_queue<ti, vector<ti>, greater<>> pq;
    vector<vector<bool>> cc(n,vector<bool>(n,false));
    pq.emplace(0,0,n-1);
    int ans{INT_MAX};
    while(!pq.empty()){
        auto[c,i,I]=pq.top();pq.pop();
        if(ans<=2*c) break;
        if(i==I){
            chmin(ans, 2*c);
            break;
        }
        if(v[i][I]){
            chmin(ans,2*c+1);
            continue;
        }
        for(auto[j,cj]:e[i]) for(auto[J,cJ]:e[I]) if(cj==cJ){
            if(cc[j][J]) continue;
            cc[j][J]=true;
            pq.emplace(c+1, j, J);
        }
    }
    cout<<(ans==INT_MAX?-1:ans)<<'\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
}

Submission Info

Submission Time
Task F - Construct a Palindrome
User Motsu_xe
Language C++ (GCC 9.2.1)
Score 600
Code Size 1370 Byte
Status AC
Exec Time 18 ms
Memory 7396 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 46
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All big_answer_00.txt, extreme_00.txt, extreme_01.txt, extreme_02.txt, handmade_00.txt, handmade_01.txt, handmade_02.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_constructive_00.txt, random_constructive_01.txt, random_constructive_02.txt, random_constructive_03.txt, random_constructive_04.txt, random_constructive_05.txt, random_constructive_06.txt, random_constructive_07.txt, random_constructive_08.txt, random_constructive_09.txt, random_constructive_10.txt, random_constructive_11.txt, random_constructive_12.txt, random_constructive_13.txt, random_constructive_14.txt, random_constructive_15.txt, sample_01.txt, sample_02.txt, sample_03.txt, tester2_00.txt, tester2_01.txt, tester3_00.txt, tester_00.txt
Case Name Status Exec Time Memory
big_answer_00.txt AC 11 ms 4880 KiB
extreme_00.txt AC 4 ms 4700 KiB
extreme_01.txt AC 11 ms 4676 KiB
extreme_02.txt AC 4 ms 4888 KiB
handmade_00.txt AC 2 ms 4628 KiB
handmade_01.txt AC 2 ms 4536 KiB
handmade_02.txt AC 3 ms 4604 KiB
random_00.txt AC 3 ms 4912 KiB
random_01.txt AC 5 ms 4868 KiB
random_02.txt AC 2 ms 4716 KiB
random_03.txt AC 3 ms 4848 KiB
random_04.txt AC 3 ms 4704 KiB
random_05.txt AC 7 ms 4528 KiB
random_06.txt AC 4 ms 4684 KiB
random_07.txt AC 5 ms 4684 KiB
random_08.txt AC 2 ms 4560 KiB
random_09.txt AC 4 ms 4772 KiB
random_10.txt AC 4 ms 4636 KiB
random_11.txt AC 4 ms 4704 KiB
random_12.txt AC 4 ms 4720 KiB
random_13.txt AC 3 ms 4704 KiB
random_14.txt AC 4 ms 4868 KiB
random_15.txt AC 4 ms 4580 KiB
random_constructive_00.txt AC 3 ms 4868 KiB
random_constructive_01.txt AC 3 ms 4916 KiB
random_constructive_02.txt AC 4 ms 4840 KiB
random_constructive_03.txt AC 6 ms 4852 KiB
random_constructive_04.txt AC 4 ms 4756 KiB
random_constructive_05.txt AC 6 ms 4732 KiB
random_constructive_06.txt AC 4 ms 4752 KiB
random_constructive_07.txt AC 5 ms 4840 KiB
random_constructive_08.txt AC 3 ms 4608 KiB
random_constructive_09.txt AC 3 ms 4720 KiB
random_constructive_10.txt AC 4 ms 4732 KiB
random_constructive_11.txt AC 4 ms 4660 KiB
random_constructive_12.txt AC 3 ms 4512 KiB
random_constructive_13.txt AC 4 ms 4696 KiB
random_constructive_14.txt AC 2 ms 4704 KiB
random_constructive_15.txt AC 4 ms 4712 KiB
sample_01.txt AC 2 ms 4628 KiB
sample_02.txt AC 3 ms 4604 KiB
sample_03.txt AC 5 ms 4600 KiB
tester2_00.txt AC 13 ms 4912 KiB
tester2_01.txt AC 6 ms 4928 KiB
tester3_00.txt AC 17 ms 7396 KiB
tester_00.txt AC 18 ms 4908 KiB