Source Code
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

priority_queue<int>q[100002][28];
pair < pair <int, int>, int>a[100002];
string s;
int n, m;

int main() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> a[i].first.first >> a[i].first.second >> a[i].second;
    }
    cin >> s;
    for (int i = 0; i < m; i++) {
        int x = a[i].first.first;
        int y = a[i].first.second;
        q[x][s[y - 1] - 'a' + 1].push(-1 * a[i].second);
        q[y][s[x - 1] - 'a' + 1].push(-1 * a[i].second);
    }
    int ans = 2000000010, sum = 2000000010;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 27; j++) {
            sum = 2000000010;
            int z = s[a[i].first.first] - 'a' + 1;
            if (z == j && !q[i][j].empty()) {
                sum = (-1 * q[i][j].top());
            }
            else {
                if (q[i][j].size() >= 2) {
                    sum = (-1 * q[i][j].top());
                    q[i][j].pop();
                    sum += (-1 * q[i][j].top());
                }
            }
            ans = min(ans, sum);
        }
    }
    if (ans == 2000000010) {
        cout << -1;
    }
    else {
        cout << ans;
    }
    return 0;
}
Copy
Selen With a C lafi-Odeh
GNU G++17
67 ms
88.1 MB
Wrong Answer