zoukankan      html  css  js  c++  java
  • POJ2485Highways

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

    题意 : 这道题和1258很像,但是这道题求的是最小生成树中最大的那条边,所以在函数里处理一下就行了。

    思路 : 赤裸裸的最小生成树啊,但是这道题一定要注意,因为底下有提醒,一定要用scanf输入,结果我就很悲剧的没看到啊,然后一直改一直改,到最后又去看了一遍题才发现要用scanf啊,╮(╯▽╰)╭

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 int ans,dis[5005][5005];
     7 const int INF = 1<<29 ;
     8 int vis[50005],n,low[50005];
     9 int prim()
    10 {
    11     memset(vis,0,sizeof(vis));
    12     int i,j,flag,min;
    13     ans = 0 ;
    14     for(i = 1 ; i <= n ; i++)
    15     {
    16         low[i] = dis[1][i];
    17     }
    18     //low[1] = 0;
    19     vis[1] = 1 ;
    20     for(i = 2 ; i <= n ; i++)
    21     {
    22         min = INF;
    23         for(j = 1 ;j <= n ; j++)
    24         {
    25             if(min > low[j]&&!vis[j])
    26             {
    27                 min = low[j] ;
    28                 flag = j;
    29             }
    30         }
    31         /*if(min >= INF)
    32         {
    33             flag = -1 ;
    34             break ;
    35         }*/
    36         if(ans < min)
    37         ans = min ;
    38         //ans+=min ;
    39         vis[flag] = 1 ;
    40         for(j = 1 ; j <= n ; j++)
    41         {
    42             if(dis[flag][j] < low[j] && !vis[j])
    43             low[j] = dis[flag][j];
    44         }
    45     }
    46     return ans ;
    47 }
    48 int main()
    49 {
    50     int s;
    51     scanf("%d",&s);
    52     while(s--)
    53     {
    54         scanf("%d",&n);
    55         for(int i = 1 ; i <= n ; i++)
    56         {
    57             for(int j = 1 ; j <= n ; j++)
    58             {
    59                 scanf("%d",&dis[i][j]);
    60             }
    61         }
    62         ans = prim();
    63         cout<<ans<<endl;
    64     }
    65     return 0 ;
    66 }
    View Code
  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/luyingfeng/p/3257050.html
Copyright © 2011-2022 走看看