<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n-1</code>. Given the array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer <code>distanceThreshold</code>.</p>

<p>Return the city with the smallest number of cities that are reachable through some path and whose distance is <strong>at most</strong> <code>distanceThreshold</code>, If there are multiple such cities, return the city with the greatest number.</p>

<p>Notice that the distance of a path connecting cities <em><strong>i</strong></em> and <em><strong>j</strong></em> is equal to the sum of the edges&#39; weights along that path.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png" style="width: 300px; height: 224px;" /></p>

<pre>
<strong>Input:</strong> n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
<strong>Output:</strong> 3
<strong>Explanation: </strong>The figure above describes the graph.&nbsp;
The neighboring cities at a distanceThreshold = 4 for each city are:
City 0 -&gt; [City 1, City 2]&nbsp;
City 1 -&gt; [City 0, City 2, City 3]&nbsp;
City 2 -&gt; [City 0, City 1, City 3]&nbsp;
City 3 -&gt; [City 1, City 2]&nbsp;
Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png" style="width: 300px; height: 224px;" /></p>

<pre>
<strong>Input:</strong> n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
<strong>Output:</strong> 0
<strong>Explanation: </strong>The figure above describes the graph.&nbsp;
The neighboring cities at a distanceThreshold = 2 for each city are:
City 0 -&gt; [City 1]&nbsp;
City 1 -&gt; [City 0, City 4]&nbsp;
City 2 -&gt; [City 3, City 4]&nbsp;
City 3 -&gt; [City 2, City 4]
City 4 -&gt; [City 1, City 2, City 3]&nbsp;
The city 0 has 1 neighboring city at a distanceThreshold = 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li>
	<li><code>edges[i].length == 3</code></li>
	<li><code>0 &lt;= from<sub>i</sub> &lt; to<sub>i</sub> &lt; n</code></li>
	<li><code>1 &lt;= weight<sub>i</sub>,&nbsp;distanceThreshold &lt;= 10^4</code></li>
	<li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li>
</ul>

#include<bits/stdc++.h>
#define M 10001
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

static const bool Booster = [](){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return true;
}();

void parse_input_and_solve(std::ofstream& out, std::string& a, std::string& s, std::string& b) {
    s[0] = ',';
    s.pop_back();
    int n = stoi(a);
    int thresh = stoi(b);

    vector<vector<int>> graph(n, vector<int>(n, M));
    for(int i=0; i<n; i++) {
        graph[i][i] = 0;
    }

    istringstream iss(s);
    string row;
    vector<int> it;
    while(getline(iss, row, ']')) {
        row.erase(row.begin());
        row.erase(row.begin());
        istringstream ss(row);
        string w;
        while(getline(ss, w, ',')) {
            it.push_back(stoi(w));
        }
        graph[it[0]][it[1]] = it[2];
        graph[it[1]][it[0]] = it[2];
        it.clear();
    }
    
    for(int k=0; k<n; k++) {
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
            }
        }
    }

    int minReachCities = n;
    int res = -1;

    for(int i=0; i<n; i++) {
        int reachCities = 0;
        for(int j=0; j<n; j++) {
            if(graph[i][j] <= thresh) reachCities++;
        }
        if(reachCities <= minReachCities) {
            minReachCities = reachCities;
            res = i;
        }
    }

    out << res << endl;
}

bool Solve = []() {
    std::ofstream out("user.out");
    std::string s, a, b;
    while (std::getline(std::cin, a) && std::getline(std::cin, s) && std::getline(std::cin, b)) {
        parse_input_and_solve(out, a, s, b);
    }
    out.flush();
    exit(0);
    return true;
}();



class Solution {
public:
    int findTheCity(int n, vector<vector<int>>& edges, int thresh) {
        std::ios_base::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout.tie(nullptr);

        vector<vector<int>> graph(n, vector<int>(n, M));
        for(int i=0; i<n; i++) {
            graph[i][i] = 0;
        }
        for(auto& it:edges) {
            graph[it[0]][it[1]] = it[2];
            graph[it[1]][it[0]] = it[2];
        }
        
        for(int k=0; k<n; k++) {
            for(int i=0; i<n; i++) {
                for(int j=0; j<n; j++) {
                    graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
                }
            }
        }

        int minReachCities = n;
        int res = -1;

        for(int i=0; i<n; i++) {
            int reachCities = 0;
            for(int j=0; j<n; j++) {
                if(graph[i][j] <= thresh) reachCities++;
            }
            if(reachCities <= minReachCities) {
                minReachCities = reachCities;
                res = i;
            }
        }

        // for(int i=0; i<n; i++) {
        //     for(int j=0; j<n; j++) {
        //         if(graph[i][j] == M) cout << "inf";
        //         else cout << graph[i][j];
        //         cout << " ";
        //     }
        //     cout << endl;
        // }
        return res;
    }
};
9949