poj2377 Bad Cowtractors

思路:

最大生成树。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7
 8 struct edge
 9 {
10     int a, b, cost;
11 };
12 edge es[20005];
13 int ran[1005];
14 int par[1005];
15 int n, m, x, y, c;
16
17 void init(int n)
18 {
19     for (int i = 0; i < n; i++)
20     {
21         par[i] = i;
22         ran[i] = 0;
23     }
24 }
25
26 int find(int x)
27 {
28     if (par[x] == x)
29         return x;
30     return par[x] = find(par[x]);
31 }
32
33 void unite(int x, int y)
34 {
35     x = find(x);
36     y = find(y);
37     if (x == y)
38         return;
39     if (ran[x] < ran[y])
40     {
41         par[x] = y;
42     }
43     else
44     {
45         par[y] = x;
46         if (ran[x] == ran[y])
47         {
48             ran[x] ++;
49         }
50     }
51 }
52
53 bool same(int x, int y)
54 {
55     return find(x) == find(y);
56 }
57
58 bool cmp(const edge & a, const edge & b)
59 {
60     return a.cost > b.cost;
61 }
62
63 int kru()
64 {
65     init(n);
66     sort(es, es + m, cmp);
67     int res = 0, cnt = 0;
68     for (int i = 0; i < m; i++)
69     {
70         if (!same(es[i].a, es[i].b))
71         {
72             unite(es[i].a, es[i].b);
73             cnt += 1;
74             res += es[i].cost;
75         }
76     }
77     return cnt < n - 1 ? -1 : res;
78 }
79
80 int main()
81 {
82     scanf("%d %d", &n, &m);
83     for (int i = 0; i < m; i++)
84     {
85         scanf("%d %d %d", &x, &y, &c);
86         es[i].a = x;
87         es[i].b = y;
88         es[i].cost = c;
89     }
90     int tmp = kru();
91     printf("%d\n", tmp);
92     return 0;
93 }
时间: 2024-10-10 12:26:50

poj2377 Bad Cowtractors的相关文章

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ

POJ2377 Bad Cowtractors 【最大生成树】

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10885   Accepted: 4586 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

[POJ2377]Bad Cowtractors(最大生成树,Kruskal)

题目链接:http://poj.org/problem?id=2377 于是就找了一道最大生成树的AC了一下,注意不连通的情况啊,WA了一次. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #inc

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 69  Solved: 49[Submit][Status][Discuss] Description 奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤ 20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给

BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 53  Solved: 37[Submit][Status] Description 奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤ 20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 

Poj2377--Bad Cowtractors(最大生成树)

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12058   Accepted: 5008 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N

BZOJ3390: [Usaco2004 Dec]Bad Cowtractors牛的报复

3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 43  Solved: 29[Submit][Status] Description 奶牛贝茜被雇去建设N(2≤N≤1000)个牛棚间的互联网.她已经勘探出M(1≤M≤ 20000)条可建的线路,每条线路连接两个牛棚,而且会苞费C(1≤C≤100000).农夫约翰吝啬得很,他希望建设费用最少甚至他都不想给贝茜工钱. 贝茜得

poj 2377 Bad Cowtractors

Bad Cowtractors Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10194   Accepted: 4333 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N