40分 TLE求助
查看原帖
40分 TLE求助
197831
罗少侠来过楼主2023/3/8 14:51
// RepairHighway.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<algorithm>

using namespace std;
int getfather(int a, int b);
/*
排序加找爹
*/
int N, M;

int GetFather();

void Kruskal();

typedef struct {
   int x;
   int y;
   int w;
   
}Road;
Road road[11111];
int f[11111];
int cmp(Road A, Road B)
{
   return A.w < B.w;
}
int GetFather(int x)
{
   while (f[x] != x)
   {
       x = f[x];
   }
   return x;

}
void Kruskal(  )
{
   int k = 0, maxlength = 99999;;
   int N2 = N;
   while (N2 - 1>0)
   {
       //(cout << road[k].x << "  " << road[k].y << endl);
       if ( GetFather(road[k].x) != GetFather(road[k].y))
       {
           f[GetFather(road[k].y)] = f[GetFather(road[k].x)];
           maxlength = road[k].w;
           N2--;
          // cout <<"test111111111111111111111111------------------" << maxlength << endl;
       }
       k++;
   }
   // GetFather();
   cout << maxlength << endl;
   return;
}
int main()
{
  
 
   cin >> N >> M;
   for (int i = 0; i < M; i++)
   {
       cin >> road[i].x >> road[i].y >> road[i].w;

   }
   sort(road, road + M, cmp);

   for (int i= 1; i <= N; i++)
   {
       f[i] = i;
   }


   Kruskal();
  /* for (int i = 1; i <= N; i++)
   {
       cout << i << "  的  父亲是" << f[i] << endl;
   }*/

   return 0;
}


数据一大程序就报错?

2023/3/8 14:51
加载中...