In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.
Input
Standard input will contain multiple test cases. The first line of the input is a single integerT (1 <= T <= 50) which is the number of test cases. And it will be followed byT consecutive test cases.
In each test case, the first line contains two positive integers N andE (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There followE lines, and each of them contains three integers, A, B,K (0 <= A, B < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. IfK is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.
Output
For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.
Sample Input
1 3 3 0 1 5 0 2 0 1 2 9
Sample Output
5
说明: 最小生成树 Kruscal && Prim (模板题) 。输入的是 编号 和 权值
已AC代码:(Kruscal )
#include<cstdio>
#include<cmath>
#define M 25000
#include<algorithm>
using namespace std;
int n,m;
int per[600]; // 并查集
struct node{
int u,v,w; //w为距离
}s[M];
bool cmp(node a,node b)
{
return a.w<b.w;
}
void itoa() //初始化
{
for(int i=0;i<=n;++i)
per[i]=i;
}
int find(int x) // 查找根节点
{
if(x==per[x])
return x;
return find(per[x]);
}
bool join(int a,int b) //合并根节点,并判断是否成环
{
int fa=find(a);
int fb=find(b);
if(fb!=fa)
{
per[fa]=fb;
return true;
}
return false;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<m;++i)
scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].w);
sort(s,s+m,cmp); //按距离从小到大排序
itoa(); //初始化
int sum=0;
for(i=0;i<m;++i)
if(join(s[i].u,s[i].v)) //判断是否成环
sum+=s[i].w;
printf("%d\n",sum);
}
return 0;
}
已AC代码:(Prim )
#include<cstdio>
#include<cstring>
#define INF 0xfffffff
int n,m,sum; //sum最小生成树权值和
int vis[600]; //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问
int map[600][600],low[600];
void prim()
{
memset(vis,0,sizeof(vis));
int i,j,pos,t;
int MIN;
for(i=1;i<=n;++i) //从某点开始,分别标记vis和记录该点pos
low[i]=map[1][i]; //第一次给low数组赋值 map的第一行
vis[1]=1;pos=1;t=0;
for(i=1;i<n;++i) //再运行n-1次,一次找一个最小
{
MIN=INF;
for(j=1;j<=n;++j)
{
if(vis[j]==0&&low[j]<MIN)
{
MIN=low[j]; // 找出最小值min,记录位置pos
pos=j;
}
}
sum+=MIN;
vis[pos]=1; //标记该点已访问
for(j=1;j<=n;++j) //更新权值low 把 map的 pos 行中比对应的 low 小的赋给low
if(vis[j]==0&&low[j]>map[pos][j])
low[j]=map[pos][j];
}
}
int main()
{
int T,i,j,a,b,c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i) //要初始化,否则 WA
for(j=1;j<=n;++j)
map[i][j]=INF;
for(i=0;i<m;++i)
{
scanf("%d%d%d",&a,&b,&c);
map[a+1][b+1]=map[b+1][a+1]=c; //加 1 是使编号从 1 开始
}
sum=0;
prim();
printf("%d\n",sum);
}
return 0;
}