POJ 1258 Agri-Net 【MST,Prime算法】

Agri-Net

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52097   Accepted: 21722

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 connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

The distance between any two farms will not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines
of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

USACO 102

原题链接:http://poj.org/problem?id=1258

做这题的时候,傻X了。。。。悲剧。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int a[105][105];
int dis[105];
bool vis[105];
int n;

void Prime()
{
    for(int i=0; i<n; i++)
    {
        vis[i]=false;
        dis[i]=a[0][i];
    }
    vis[0]=true;
    int ans=0;
    for(int i=1; i<n; i++)
    {
        int p=-1;
        int minn=INF;
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
                minn=dis[p=j];
        }
        if(p==-1)
        {
            cout<<-1<<endl;
            return;
        }
        vis[p]=true;
        ans+=minn;
        for(int j=0; j<n; j++)
        {
            if(!vis[j]&&dis[j]>a[p][j])//SX了。。。
                dis[j]=a[p][j];
        }
    }
    cout<<ans<<endl;
}

int main()
{
    //freopen("data/1258.txt","r",stdin);
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                scanf("%d",&a[i][j]);
        }
        Prime();
    }
    return 0;
}
时间: 2024-10-08 18:14:37

POJ 1258 Agri-Net 【MST,Prime算法】的相关文章

POJ 1258 Agri-Net【MST】

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49389   Accepted: 20511 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 2560 Freckles Prime算法题解

本题是求最小生成树. 给出的是坐标节点,然后需要根据这些坐标计算出各个点之间的距离. 除此就是标准的Prime算法了,能使用Prime的基本上都可以使用Kruskal. 这些经典的算法一定要多写,熟练掌握,否则很难灵活运用的. 而且经典的算法之所以为经典,原因之一是没那么容易自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <float.h> #

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

POJ 2485 Highways【最小生成树最大边,Prime算法】

Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28592   Accepted: 13037 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Fl

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 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1258

题意:给n*n矩阵 表示个点个边  求最小生成树 思路:Kruskal 算法 //:简单介绍一下题意.农民要建立互联网络,目的使村庄里所有的农民连上网, //并且总费用最小.多组数据,每组数据给出一个n,然后给出n * n大小的无向图的邻接矩阵表示,值表示边权. //要求输出最小生成树的权值和. #include<iostream> #include<cstring> using namespace std; int map[101][101]; int dist[101]; in

[2016-04-14][POJ][1258][Agri-Net]

时间:2016-04-14 20:36:55 星期四 题目编号:[2016-04-14][POJ][1258][Agri-Net] 题目大意:求最小生成树 分析:直接prim算法 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100 + 10 ; int g[maxn][maxn],vis[maxn],lowc[maxn]; int