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

链接:

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

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/I

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 110;
const int INF = 0xfffffff;

int n, J[N][N], dist[N], vis[N];

int Prim()
{
    int i, j, ans=0;
    dist[1]=0;
    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        int index=1, MIN=INF;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN)
            {
                index=j; MIN=dist[j];
            }
        }
        vis[index]=1;
        ans += MIN;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>J[index][j])
                dist[j]=J[index][j];
        }
    }
    return ans;
}

int main ()
{
    while(scanf("%d", &n)!=EOF)
    {
        int i, j;

        memset(J, 0, sizeof(J));

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        scanf("%d", &J[i][j]);

        int ans=Prim();

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-25 21:13:14

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

最小生成树模板题POJ - 1287-prim+kruskal

POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出发,每次走这个点没走过的最小边权值,这样不断找下去就可以找出,本质就是贪心算法 而kruskal是利用并查集,先按照边权值大小排序,然后从小的边开始往里面添加边,利用并查集判断是否在一个联通分量里面(就是是否相连)如果不相 连就建立边,从而建图,注意,节点编号如果是从1->n,那么相应初始化就应该从

(最小生成树) Jungle Roads -- POJ -- 1251

链接: http://poj.org/problem?id=1251 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 110; const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim() { i

(最小生成树) Networking -- POJ -- 1287

链接: http://poj.org/problem?id=1287 代码: #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 210; const int INF = 0xfffffff; int n; int J[N][N], dist

【裸最小生成树】 模板 poj 1258

#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #define MAX 102 void read(); using namespace std; int map[MAX][MAX],best[MAX],n; bool visit[MAX]; int main() { //freopen("in.txt","r",stdin

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

各种最小生成树。 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 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路,最小生成树,Kruskal算法

题目链接:ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路 Jungle Roads Time Limit: 2 Seconds      Memory Limit: 65536 KB The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some ye

poj 1679 The Unique MST (判断最小生成树是否唯一)

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