POJ 1258(最小生成树+知识)

用kruskal算法,利用w[i]给r[i]间接排序,从而r[i]可以按照边大小保存序号,同时要判断是否在一个集合里面

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

#define sf scanf
#define pf printf
#define debug printf("!\n")
#define blank printf("\n")
#define mem(a,b) memset(a,b,sizeof(a))

const int MaxN = 110;
const int INF = 1<<27;

int p[MaxN];

int map[MaxN][MaxN],w[MaxN*MaxN],r[MaxN*MaxN],u[MaxN*MaxN],v[MaxN*MaxN];

int m,n;

int find(int x){return p[x]==x?x:p[x]=find(p[x]);}

int cmp(const int a,const int b)
{
    return w[a]<w[b];
}

int kruskal()
{
    int ans = 0,i;

    for(i = 0;i<n;i++) p[i] = i;
    for(i = 0;i<m;i++) r[i] = i;

    sort(r,r+m,cmp);

    for(i = 0;i<m;i++)
    {
        int e = r[i];
        int x = find(u[e]);
        int y = find(v[e]);
        if(x!=y)
        {
            ans+=w[e];p[x] = y;
        }
    }
    return ans;
}

int main()
{
    int i,j;
    while(~sf("%d",&n))
    {
        m = 0;
        mem(u,0);
        mem(v,0);
        mem(w,0);
        for(i = 0;i<n;i++)
        {
            for(j=0;j<n;j++)
                sf("%d",&map[i][j]);
        }
        for(i = 0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                u[m] = i;
                v[m] = j;
                w[m++] = map[i][j];
            }
        }
        int ans = kruskal();
        pf("%d\n",ans);

    }

    return 0;
}
时间: 2024-10-06 02:25:31

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

POJ 1258 最小生成树

23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求任意两个村庄是连通的所需要的网线.就是求最小生成树的权值. 附代码: #include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<algorithm>#d

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