【转】POJ-1258-Agri-Net:简单 Kruskal 最小生成树第一题

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
        int s, e, w;
        node(int ss, int ee, int ww):s(ss),e(ee),w(ww){}
        bool operator < (const node & n) const {
                return w<n.w;
        }
};
vector<node>edges;
vector<int>parent;

int GetRot(int a)
{
        if(a!=parent[a])
            parent[a]=GetRot(parent[a]);
        return parent[a];
}

void Union(int a, int b)
{
        a = GetRot(a);
        b = GetRot(b);
        if(a==b)
            return ;
        parent[a]=b;
}

int main()
{
        int N;
        while(cin>>N)
        {
                edges.clear();      parent.clear();
                for(int i=0; i<N; i++)
                    parent.push_back(i);
                for(int i=0; i<N; i++)
                    for(int j=0; j<N; j++)
                {
                        int w;  cin>>w;
                        edges.push_back(node(i,j,w));
                }
                sort(edges.begin(), edges.end());
                int done = 0;      int ans=0;
                for(int i=0; i<edges.size(); i++){
                    if( GetRot(edges[i].s)!=GetRot(edges[i].e) )
                    {
                            Union(edges[i].s,edges[i].e);
                            ans+=edges[i].w;
                            done++;
                    }
                    if(done==N-1) break;
                }
                cout<<ans<<endl;
        }
        return 0;
}
时间: 2024-10-09 23:45:10

【转】POJ-1258-Agri-Net:简单 Kruskal 最小生成树第一题的相关文章

HDU 1233 prim kruskal最小生成树模板题

A - 还是畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小.请计算最小的公路总长度. Input 测试输入包含若干测试

HDU 1879--继续畅通工程【kruskal &amp;&amp; 最小生成树 &amp;&amp; 水题】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17798    Accepted Submission(s): 7662 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

[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

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

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

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