Language: C++

#include <iostream>
#include <vector>
#include <set>
#include <unordered_map>
#include <algorithm>

#define ll long long
#define Inf 1000000001
using namespace std;


vector<pair<int,int> > roads[333];


bool done[333];
bool dfs(int u,int v) {
    if (u == v)
        return true;
    if (done[u])
        return false;
    done[u]=true;
    for(pair<int,int> w_dist : roads[u]) {
        if (dfs(w_dist.first,v))
            return true;
    }
    return false;
}

bool IsConn(int u,int v,int N) {
    
    for(int i=1;i<=N;i++)
        done[i]=false;
    return dfs(u,v);
    
}


int djikstra(int u,int v,int maxD,int N) {


    set<pair<int,int> > dists;
    bitset<333> dones;
    unordered_map<int,int> best_dists;
    dones.reset();
    dists.insert({0,u});
    
    int iters=0;
    while(!dists.empty()) {
        set<pair<int,int> >::iterator it = dists.begin();
        int curru = it->second;
        int currd = it->first;
        if (currd > maxD)
            return maxD+1;
        dists.erase(it);
        if (curru == v)
            return currd;
        if (dones[curru])
            continue;
        dones.set(curru);

        iters++;
        for(pair<int,ll> w_dist : roads[curru]) {
            int w = w_dist.first;
            ll nextd = w_dist.second + currd;
            
            if (nextd > maxD)
                continue;
            
            if (best_dists.find(w) == best_dists.end() || best_dists[w] > nextd) {
                dists.erase({best_dists[w],w});
                best_dists[w]=nextd;
                dists.insert({nextd, w});
            }
            
        }
    }
    return Inf;
}



int A[333][333];

ll Solve(int N) {
    
    for(int i=1;i<=N;i++) {
        roads[i].clear();
    }
    
    vector<pair<ll,pair<int,int> > > dist_sorted;
    for(int i=1;i<=N;i++)
        for(int j=i+1;j<=N;j++)
            dist_sorted.push_back({A[i][j],{i,j}});
    
    sort(dist_sorted.begin(),dist_sorted.end());
    ll ans = 0;
    for(int i=0;i<dist_sorted.size();i++) {
        
        int u=dist_sorted[i].second.first;
        int v=dist_sorted[i].second.second;
        
        int reqd_dist = dist_sorted[i].first;
        int curr_dist = djikstra(u,v,reqd_dist,N);

        if (curr_dist < reqd_dist) {
            return -1;
        } else if (curr_dist > reqd_dist) {
            roads[u].push_back({v,reqd_dist});
            roads[v].push_back({u,reqd_dist});
            ans += reqd_dist;
        }
    }
    
    return ans;
    
    
    
}


ll Solve2(int N) {
    vector<pair<ll,pair<int,int> > > dist_sorted;
    ll ans = 0;
    for(int i=1;i<=N;i++)
        for(int j=i+1;j<=N;j++) {
            dist_sorted.push_back({-A[i][j],{i,j}});
            ans += A[i][j];
        }
    
    sort(dist_sorted.begin(),dist_sorted.end());

    
    for(int i=0;i<dist_sorted.size();i++) {
        int u=dist_sorted[i].second.first;
        int v=dist_sorted[i].second.second;
        
        bool can = false;
        for(int w=1;w<=N;w++) {
            if (A[u][w]+A[w][v] < A[u][v])
                return -1;
            if (w != u && w != v && A[u][w]+A[w][v]==A[u][v]) {
                can=true;
            }
        }
        
        if (can) {
            ans -= A[u][v];
        }
        
        
        
    }
    
    return ans;
    
}

int main() {
    int N;
    cin>>N;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++) {
            cin>>A[i][j];
            //A[i][j]=(i==j?0:1);
        }
    
    cout << Solve2(N) << endl;
    
}

Compiler output:
a.cc: In function 'int djikstra(int, int, int, int)':
a.cc:42:5: error: 'bitset' was not declared in this scope
   42 |     bitset<333> dones;
      |     ^~~~~~
a.cc:6:1: note: 'std::bitset' is defined in header '<bitset>'; this is probably fixable by adding '#include <bitset>'
    5 | #include <algorithm>
  +++ |+#include <bitset>
    6 | 
a.cc:42:17: error: 'dones' was not declared in this scope; did you mean 'done'?
   42 |     bitset<333> dones;
      |                 ^~~~~
      |                 done