zoukankan      html  css  js  c++  java
  • poj

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

    FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费。

    最小生成树。

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int maxn = 105;
    const int INF =1<<29;
    int cost[maxn][maxn];
    int mincost[maxn];
    bool used[maxn];
    int n;
    
    int prim()
    {
        for(int i=0;i<n;++i)
        {
            mincost[i]=INF;
            used[i]=false;
        }
        mincost[0]=0;
        int res=0;
    
        while(true)
        {
            int v=-1;
            for(int u=0;u<n;u++)
            {
                if(!used[u]&&(v==-1||mincost[u]<mincost[v])) v=u;
            }
            if(v==-1) break;
            used[v]=true;
            res+=mincost[v];
            for(int u=0;u<n;u++) mincost[u]=min(mincost[u],cost[v][u]);
        }
        return res;
    }
    
    int main()
    {
        //freopen("a.txt","r",stdin);
        while(~scanf("%d",&n))
        {
            for(int i=0;i<n;++i)
                for(int j=0;j<n;++j)
                {
                    scanf("%d",&cost[i][j]);
                }
            printf("%d
    ",prim());
        }
        return 0;
    }
  • 相关阅读:
    Python with
    Python else
    Python list
    The Python Debugger Pdb
    RPM 包
    yum
    OpenStack I18N
    Python unittest
    MySQL 行格式
    MySQL 行溢出数据
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4511246.html
Copyright © 2011-2022 走看看