New Exercise
This commit is contained in:
58
Exercise/Homework8/Q1Dij.cpp
Normal file
58
Exercise/Homework8/Q1Dij.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user