zoukankan      html  css  js  c++  java
  • 最小生成树问题(prim算法)POJ-1258 Agri-Net

    /*

    这个题很水,但是,莫名其妙runtime error一晚上,重写了一遍就又没了,很伤心!

    题意很简单,大致为n个村庄,连光缆,要求连上所有村庄的长度最短。

    输入n,接着是n*n的矩阵,直接用prim算法写就行;

    */

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int map_[100][100];
    int lowcost[100];
    const int inf = 100000 ;
    int main()
    {
        int n;
        while ( cin >> n )
        {
            int i , j ;
            for ( i = 0 ; i < n ; i ++ )
                for ( j = 0 ; j < n ; j ++ )
                    cin >> map_[i][j];
            for(int i=0;i<n;i++)
            lowcost[i]=map_[0][i];
            int ans=0;
            for (int i=0;i<n-1;i++)
            {
                int min1=inf;
                int min2;
                for (int j=0;j<n;j++)
                {
                    if (lowcost[j]&&lowcost[j]<min1)
                    {
                        min1=lowcost[j];
                        min2=j;
                    }
                }
                ans+=min1;//没错,,就是这里,莫名奇妙re。。。
                lowcost[min2]=0;//还有这里。。。
                for (int j=0;j<n;j++)//更新数组
                {
                    if (lowcost[j]>map_[min2][j])
                    {
                        lowcost[j]=map_[min2][j];
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    hdu2328 Corporate Identity
    hdu1238 Substrings
    hdu4300 Clairewd’s message
    hdu3336 Count the string
    hdu2597 Simpsons’ Hidden Talents
    poj3080 Blue Jeans
    poj2752 Seek the Name, Seek the Fame
    poj2406 Power Strings
    hust1010 The Minimum Length
    hdu1358 Period
  • 原文地址:https://www.cnblogs.com/SunQi-lvbu/p/6833026.html
Copyright © 2011-2022 走看看