pojHighways(prime)

http://poj.org/problem?id=2485

很裸的最小生成树

View Code
 1  #include<stdio.h>

 2  #include<string.h>

 3  #define INF 100000

 4  int w[1001][501];

 5  int visit[1001],low[1001],max;

 6  void prime(int n)

 7  {

 8      int i,j,k;

 9      memset(visit,0,sizeof(visit));

10      visit[1] = 1;

11      for(i = 2; i <= n ; i++)

12          low[i] = w[1][i];

13      for(i = 1; i <= n ; i++)

14      {

15          int min = INF;

16          for(j = 1; j <= n ; j++)

17              if(!visit[j]&&min>=low[j])

18                  min = low[k = j];

19          if(min==INF)

20              break;

21          visit[k] = 1;

22          if(max<min)

23          max = min;

24          for(j = 1 ; j <= n ; j++)

25              if(!visit[j]&&low[j]>w[k][j])

26                  low[j] = w[k][j];

27      }

28  }

29  int main()

30  {

31      int i,j,k,n,m,t,u,v,w1;

32      scanf("%d", &t);

33      while(t--)

34      {

35          for(i = 1; i <= 501 ; i++)

36              for(j = 1; j <= 501  ;j++)

37                  w[i][j] = 100000;

38          scanf("%d", &n);

39          max = 0;

40          for(i = 1; i <= n ; i++)

41          for(j = 1; j <= n ; j++)

42          {

43              scanf("%d", &w1);

44              if(w[i][j]>w1)

45              {

46                  w[i][j] = w1;

47                  w[j][i] = w1;

48              }

49          }

50          prime(n);

51          printf("%d\n",max);

52      }

53      return 0;

54  }

 

你可能感兴趣的:(Prim)