POJ1258 Agri-Net

Agri-Net

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46319   Accepted: 19052

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

prim求最小生成树

prim和dijkstra的代码只有更新dis数组这一块是不同的,prim中dis[i]表示的是i到当前生成树的距离,dijkstra中dis[i]表示的是i到源点s的距离

/*
ID: LinKArftc
PROG: 1258.cpp
LANG: C++
*/

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll;

const int maxn = 110;

int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n;

int prim() {
    int ret = 0;
    for (int i = 2; i <= n; i ++) dis[i] = mp[i][1];
    dis[1] = 0;
    vis[1] = true;
    int ii = 1;
    for (int i = 2; i <= n; i ++) {
        int mi = inf;
        for (int j = 1; j <= n; j ++) {
            if (!vis[j] && dis[j] < mi) {
                mi = dis[j];
                ii = j;
            }
        }
        vis[ii] = true;
        ret += dis[ii];
        for (int j = 1; j <= n; j ++) {
            if (!vis[j] && dis[j] > mp[j][ii]) dis[j] = mp[j][ii];
        }
    }
    return ret;
}

int main() {

    //input;
    while (~scanf("%d", &n)) {
        memset(mp, 0x3f, sizeof(mp));
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; i ++) {
            for (int j = 1; j <= n; j ++) scanf("%d", &mp[i][j]);
        }
        printf("%d\n", prim());
    }

    return 0;
}
时间: 2024-12-19 09:27:54

POJ1258 Agri-Net的相关文章

poj1258 Agri-Net (prim+heap)

题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<vector> 6 #include<set> 7 #define CLR(a,b) memset((a),(b),sizeof((

POJ1258最小生成树(prim算法)

POJ1258 思路:首先把第一个结点加入树中,每次往树中加入一个结点,加入的结点必须是与当前树中的结点距离最小那个点,这样每次把结点加入树中选取的都是最小权值,循环n-1次后把所有结点都加入树中. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 1e9; //创建map二维数组储存图表,low数组记录每2个点间最小权值,vis数组标记

poj1258

依旧是最小生成树的基础题~ #include<iostream> #include<algorithm> using namespace std; int father[101]; struct n1 { int s,e,w; }; n1 path[6000]; bool cmp(n1 a,n1 b) { return a.w<b.w; } int find(int i) { while(father[i]!=i) { i=father[i]; } return i; } i

poj1258 最小生成树 普林姆

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36780   Accepted: 14825 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

poj1258(Agri-net)

解题思路: 最小生成树. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <bitset> 9 #include <vector&

最小生成树Prim poj1258 poj2485

poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Farmer John has been elected mayor of his town! One of his campaign promises was to bri

POJ1258 基础最小生成树

本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一个点,然后不停的向其中加入权值最小的边,边的一端在已经生成的部分生成树中,另一端在未生成的生成树中. 2.利用优先队列维护边,将加入的点所包含的边加入到队列中去,随后按照边的权值弹出. 简单理解方法:一个人可以拉很多人,新被拉进来的人,把能拉的人(有边,且未被访问)排队,找最好拉的人拉进来,循环.

最小生成树问题(prim算法)POJ-1258 Agri-Net

/* 这个题很水,但是,莫名其妙runtime error一晚上,重写了一遍就又没了,很伤心! 题意很简单,大致为n个村庄,连光缆,要求连上所有村庄的长度最短. 输入n,接着是n*n的矩阵,直接用prim算法写就行: */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath>

Prim算法POJ1258

http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, 1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 const int N=110; 7 #define M 0x3f3f3f3f///设置无穷大的数M,当两点之间无法联通时设两者之间距离为M 8 in