蓝桥杯 -- 大臣的旅费

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 06:38   11   0

路费和走的距离的关系是等差数列 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;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP