POJ 1258 最小生成树

23333333333

完全是道水题。因为是偶自己读懂自己做出来的。。T_T、prim的模板题水过。

DESCRIPTION:
John竞选的时候许诺会给村子连网。现在给你任意两个村子之间的距离。让你求任意两个村庄是连通的所需要的网线。就是求最小生成树的权值。

附代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#define inf 0x1f1f1f1f
using namespace std;

int map[210][210];
int n;

int prim()
{
    int low[210], vis[210];
    int min, res = 0, p;
    memset(vis, 0, sizeof(vis));
    vis[0] = 1;
    for (int i=1; i<n; ++i)
    {
        low[i] = map[0][i];
    }
    for (int i=1; i<n; ++i)  // 循环n-1次 找最小生成树的n-1个点
    {
        min = inf;
        p = -1;
        for (int j=0; j<n; ++j)
        {
            if (vis[j] == 0 && min > low[j])
            {
                min = low[j];
                p = j;
            }
        }
        if (res == inf)
        return -1;
        res += min;
        vis[p] = 1;
        for (int j=0; j<n; ++j)
        {
            if (vis[j] == 0 && low[j] > map[p][j])
            {
                low[j] = map[p][j];
            }
        }
    }
    return res;
}

int main()
{
    while(cin >> n)
    {
        memset(map, 0, sizeof(map));
        for (int i=0; i<n; ++i)
        {
            for (int j=0; j<n; ++j)
            cin >> map[i][j];
        }
        int ans = prim();
        cout << ans << endl;
    }
    return 0;
}

时间: 2024-10-30 08:42:11

POJ 1258 最小生成树的相关文章

POJ - 1258(最小生成树.prime)

题目链接: http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68462   Accepted: 28369 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conn

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

原题 题目链接 题目分析 比较明显的最小生成树模板题,题目给的输入是邻接矩阵,处理一下用prim算法就可以算出最小生成树了. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8

POJ 1258 最小生成树(Prim算法)

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

各种最小生成树。 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 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

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;

poj 1258 Agri-Net 解题报告

题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 fiber 数是多少. 赤裸裸的最小生成树,用prim做的. 有个地方写错,wa 了 几次. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int farm_num = 10000