New Exercise

This commit is contained in:
e2hang
2025-12-08 22:54:52 +08:00
parent 4965074539
commit ce2b06bf1e
8 changed files with 726 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 1e9;
int main(){
int n, e;
while(cin >> n >> e){
//直接dp解决
vector<vector<int>> dp(n, vector<int>(n, INF));
for(int i = 0; i < n; i++){
dp[i][i] = 0;
}
for(int i = 0; i < e; i++){
int a, b, c;
cin >> a >> b >> c;
dp[a][b] = min(dp[a][b], c);
dp[b][a] = min(dp[b][a], c);
}
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(dp[i][k] != INF && dp[k][j] != INF){
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
}
int mins = INF;
int out = 0;
for(int i = 0; i < n; i++){
int sum = 0;
bool valid = true;
for(int j = 0; j < n; j++){
if(dp[i][j] == INF){
valid = false;
break;
}
sum += dp[i][j];
}
if(valid && sum < mins){
mins = sum;
out = i;
}
}
cout << out << endl;
}
return 0;
}