http://poj.org/problem?id=2377
坏奶牛:为了破坏农夫约翰的光纤计划,奶牛决定骗丫拉一条最长的网络。
2.5 它们其实都是“图”
最小生成树
其实是所谓的最大生成树,只要按花费从大到小加入树就行了。我看kruskal改一个符号就行了于是就它了,另外需要判断一下是否有悬孤节点,总之又是一道水题。
https://gist.github.com/amoshyc/61d7fe9182b0d61f634b
Read full article from 2377 -- Bad Cowtractors
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 has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
Sample Input
5 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 5 17
Sample Output
42http://www.hankcs.com/program/cpp/poj-2377-bad-cowtractors-problem-solution-challenge-programming-contest.html
坏奶牛:为了破坏农夫约翰的光纤计划,奶牛决定骗丫拉一条最长的网络。
2.5 它们其实都是“图”
最小生成树
其实是所谓的最大生成树,只要按花费从大到小加入树就行了。我看kruskal改一个符号就行了于是就它了,另外需要判断一下是否有悬孤节点,总之又是一道水题。
// 并查集相关数据与算法
#define MAX_N 1000 + 16
int
parent[MAX_N];
int
height[MAX_N];
void
init(
const
int
& n)
{
for
(
int
i = 0; i < n; ++i)
{
parent[i] = i;
height[i] = 0;
}
}
int
find(
const
int
& x)
{
if
(parent[x] == x)
{
return
x;
}
else
{
return
parent[x] = find(parent[x]);
}
}
void
unite(
int
x,
int
y)
{
x = find(x);
y = find(y);
if
(x == y)
{
return
;
}
if
(height[x] < height[y])
{
parent[x] = y;
}
else
{
parent[y] = x;
if
(height[x] == height[y])
{
++height[x];
}
}
}
bool
same(
const
int
& x,
const
int
& y)
{
return
find(x) == find(y);
}
// End Of 并查集
struct
edge
{
int
u, v, cost;
edge(
int
u = 0,
int
v = 0,
int
cost = 0) : u(u), v(v), cost(cost) {}
bool
operator < (
const
edge & e2)
const
{
return
cost > e2.cost;
}
};
edge es[MAX_E];
int
V, E;
int
in_tree;
// 在树中的节点个数
int
kruskal()
{
sort(es, es + E);
// 按照权值从小到大排序
init(V);
int
res = 0;
in_tree = 1;
for
(
int
i = 0; i < E && in_tree <= V; ++i)
{
edge e = es[i];
if
(!same(e.u, e.v))
{
unite(e.u, e.v);
res += e.cost;
++in_tree;
}
}
return
res;
}
int
main(
int
argc,
char
*argv[])
{
#ifndef ONLINE_JUDGE
freopen
(
"in.txt"
,
"r"
, stdin);
freopen
(
"out.txt"
,
"w"
, stdout);
#endif
cin >> V >> E;
for
(
int
i = 0; i < E; ++i)
{
cin >> es[i].u >> es[i].v >> es[i].cost;
--es[i].u; --es[i].v;
}
int
ans = kruskal();
if
(in_tree < V)
{
ans = -1;
}
cout << ans << endl;
#ifndef ONLINE_JUDGE
fclose
(stdin);
fclose
(stdout);
system
(
"out.txt"
);
#endif
return
0;
}
https://gist.github.com/amoshyc/61d7fe9182b0d61f634b
Read full article from 2377 -- Bad Cowtractors