假设 WN=(V,{E}) 是一个含有 n 个顶点的连通网,则按照克鲁斯卡尔算法构造最小生成树的过程为:先构造一个只含 n 个顶点,而边集为空的子图,若将该子图中各个顶点看成是各棵树上的根结点,则它是一个含有 n 棵树的一个森林。之后,从网的边集 E 中选取一条权值最小的边,若该条边的两个顶点分属不同的树,则将其加入子图,也就是说,将这两个顶点分别所在的两棵树合成一棵树;反之,若该条边的两个顶点已落在同一棵树上,则不可取,而应该取下一条权值最小的边再试之。依次类推,直至森林中只有一棵树,也即子图中含有 n-1条边为止。
以上都来自百度百科,好多字,看着就烦。。。
n个节点 n-1条边 sigma(wi)==min; 图-->最小生成树
这样就简单好多 ̄ω ̄=
原理啥的不知道,现在的目标是会用就可以了 (=′ω`=)
直接上题了。。。
先贴一个用Kruskal的题(〃‘▽‘〃)
HDU2988
Dark roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1056 Accepted Submission(s): 463
Problem Description
Economic times these days are tough, even in Byteland. To reduce the operating costs, the government of Byteland has decided to optimize the road lighting. Till now every road was illuminated all night long, which costs 1 Bytelandian Dollar per meter and day. To save money, they decided to no longer illuminate every road, but to switch off the road lighting of some streets. To make sure that the inhabitants of Byteland still feel safe, they want to optimize the lighting in such a way, that after darkening some streets at night, there will still be at least one illuminated path from every junction in Byteland to every other junction.
What is the maximum daily amount of money the government of Byteland can save, without making their inhabitants feel unsafe?
Input
The input file contains several test cases. Each test case starts with two numbers m and n, the number of junctions in Byteland and the number of roads in Byteland, respectively. Input is terminated by m=n=0. Otherwise, 1 ≤ m ≤ 200000 and m-1 ≤ n ≤ 200000. Then follow n integer triples x, y, z specifying that there will be a bidirectional road between x and y with length z meters (0 ≤ x, y < m and x ≠ y). The graph specified by each test case is connected. The total length of all roads in each test case is less than 231.
Output
For each test case print one line containing the maximum daily amount the government can save.
Sample Input
7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0
Sample Output
51
直接贴代码了(??ω??)(老大的代码)
#include<bits/stdc++.h>
usingnamespace std;
constint N=2*1e5+100;
int parent[N];
int ans;
int m,n;
struct node{
int u,v,w;
}a[N];
bool cmp(node x,node y){
return x.w<y.w;
}
void init(){
for(int i=0;i<=N;i++)parent[i]=i;
}
int find(int x){
int r=x;
while(parent[r]!=r)r=parent[r];
int i=x;
int j;
while(i!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void Kruskal(){
for(int i=0;i<m;i++){
int x=find(a[i].u);
int y=find(a[i].v);
if(x!=y){
parent[x]=y;
ans=ans+a[i].w;
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
if(m==0&&n==0)break;
int sum=0;
ans=0;
for(int i=0;i<m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
sum=sum+a[i].w;
}
sort(a,a+m,cmp);
Kruskal();
printf("%d\n",sum-ans);
}
return0;
}
不想解释了,不会解释也没什么好解释的了(???)
下一个是用Prim的题(?ω?`ll)
HDU1233
上一个畅通工程是并查集?(????ω????)?
还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 45926 Accepted Submission(s): 20922