
路费和走的距离的关系是等差数列 cost = 11 *n + n*(n-1)/2
然后用Folyd 求出所有城市之间的距离.找最大 ,特殊 n = 1 的时候 .
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstring>
#include <stack>
#include <vector>
#include <queue>
#define Swap(a,b) a ^= b ^= a ^= b
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std ;
typedef long long LL;
const int N = 1e7+10 ;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
const int MAX = 1005;
const int inf = 0xffffff;
const LL mod = 1e9+7 ;
priority_queue<int,vector<int>,greater<int> > Q ;
// 小跟堆
int Map[MAX][MAX] ;
int vis[MAX];
int n ;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
int n ;
cin >>n ;
if(n == 1 ){
cout<<"0"<<endl;
return 0 ;
}
for(int i = 1 ; i<=n ;i++ ){
for(int j = 1 ; j<=n ;j++){
Map[i][j] = inf ;
if(i == j ){
Map[i][j] = 0;
}
}
}
for(int i = 1 ; i<n ; i++){
int u , v ,w ;
cin >> u >> v>>w ;
Map[u][v] = w ;
Map[v][u] = w ;
}
int maxx = -inf;
for(int k = 1 ; k<=n ;k++){
for(int i = 1 ; i<=n ; i++){
for(int j = 1 ; j<=n ; j++ ){
if(Map[i][j] > Map[i][k] + Map[k][j] && Map[i][k]!=inf&& Map[k][j]!=inf){
Map[j][i] = Map[i][j] = Map[i][k] + Map[k][j] ;
}
}
}
}
for(int i = 1 ; i<=n ; i++ ){
for(int j= 1 ; j<=n ;j++){
if(Map[i][j]!=inf && i!=j){
maxx = max(maxx,Map[i][j]) ;
}
}
}
cout<<maxx*11+ maxx*(maxx-1)/2 <<endl;
return 0;
}
|