POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题

#include<bits/stdc++.h>
using namespace std;
int n,a;
int dist[120],m[120][120];
void prim()
{
    bool p[1020];
    for(int i=2;i<=n;i++)
    {
        p[i]=false;
        dist[i]=m[1][i];
    }
    dist[1]=0,p[1]=true;
    for(int i=1;i<=n-1;i++)
    {
        int min=INT_MAX,k=0;
        for(int j=1;j<=n;j++)
        {
            if(!p[j]&&dist[j]!=0&&dist[j]<min)
            {
                min=dist[j];
                k=j;
            }
        }
        if(k==0)
            return;
        p[k]=true;
        for(int j=1;j<=n;j++)
        {
            if(!p[j]&&m[k][j]!=0&&(dist[j]==0||dist[j]>m[k][j]))
                dist[j]=m[k][j];
        }
    }
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a);
                m[i][j]=a;
            }
        prim();
        int sum=0;
        for(int i=1;i<=n;i++)
            sum+=dist[i];
        printf("%d\n",sum);
    }
    return 0;
}

POJ 1258 Agri-Net (prim最小生成树)

时间: 2024-12-23 05:21:28

POJ 1258 Agri-Net (prim最小生成树)的相关文章

POJ 1258 Agri-Net(Prim算法)

题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> #include<cstring> using namespace std; int n; int tot; int v[150][150]; int dist[150];//存 节点到树 的最小距离 bool use[150];//标记节点是否存在 int main(){ while(sca

POJ 1258:Agri-Net(最小生成树&amp;amp;&amp;amp;prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38918   Accepted: 15751 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ 1258:Agri-Net(最小生成树&amp;&amp;prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38918   Accepted: 15751 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ 1258 Agri-Net(Prim)

( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<cstdlib> #include<vector> using namespace std; typedef long long ll; const int

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

各种最小生成树。 HDU 1863 HDU 1301 POJ 1258

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18811    Accepted Submission(s): 7981 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways