[USACO 102]Agri-Net

OJ题号:POJ1258、洛谷1546

思路:Kruskal。

 1 #include<cstdio>
 2 #include<utility>
 3 #include<vector>
 4 #include<algorithm>
 5 #define w first
 6 #define a second.first
 7 #define b second.second
 8 typedef std::pair<int,std::pair<int,int> > Edge;
 9 const int N=100;
10 int n;
11 std::vector<Edge> e;
12 class UnionFindSet {
13     private:
14         int anc[N];
15     public:
16         UnionFindSet(int n) {
17             for(int i=0;i<n;i++) anc[i]=i;
18         }
19         int Find(int x) {
20             return (x==anc[x])?x:(anc[x]=Find(anc[x]));
21         }
22         void Union(int x,int y) {
23             anc[Find(y)]=Find(x);
24         }
25 };
26 int main() {
27     scanf("%d",&n);
28     for(int i=0;i<n;i++) {
29         for(int j=0;j<n;j++) {
30             int c;
31             scanf("%d",&c);
32             if(i<j) {
33                 e.push_back(std::make_pair(c,std::make_pair(i,j)));
34             }
35         }
36     }
37     UnionFindSet s(n);
38     std::sort(e.begin(),e.end());
39     int ans=0;
40     for(int i=0;i<(int)e.size();i++) {
41         if(s.Find(e[i].a)==s.Find(e[i].b)) continue;
42         ans+=e[i].w;
43         s.Union(e[i].a,e[i].b);
44     }
45     printf("%d\n",ans);
46     return 0;
47 }
时间: 2024-08-08 05:18:13

[USACO 102]Agri-Net的相关文章

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 nee

POJ 题目1258 Agri-Net(最小生成树)

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

[ACM] poj 1258 Agri-Net (最小生成树)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37131   Accepted: 14998 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 【MST,Prime算法】

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52097   Accepted: 21722 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 +hdu 1233 还是畅通工程 (最小生成树Prime算法)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43215   Accepted: 17683 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 【最小生成树Prim】

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40889   Accepted: 16677 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(最小生成树果题)

题目链接: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