Build The Electric System
4388 단어 테스트
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 integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
In each test case, the first line contains two positive integers N and E (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 follow E lines, and each of them contains three integers, A, B, K (0 <= A, B < N, 0 <= K < 1000). A and Brespectively means the index of the starting village and ending village of the power line. If K 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
: , ,
:
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int s,e,c;
}ss[40005];
int father[600],ans;
int cmp(struct node x,struct node y)
{
return x.c<y.c;
}
void init(int n)
{
int i;
for(i=0;i<n;i++)
{
father[i]=i;
}
}
int Find_Set(int x)
{
if(x!=father[x])
return Find_Set(father[x]);
return father[x];
}
void Union_Set(struct node st)
{
int x,y;
x=Find_Set(st.s);
y=Find_Set(st.e);
if(x==y) return ;
if(x!=y)
{
ans+=st.c;
father[x]=y;
}
}
int main()
{
int t,n,m,i;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<m;i++)
cin>>ss[i].s>>ss[i].e>>ss[i].c;
init(n);
sort(ss,ss+m,cmp);
ans=0;
for(i=0;i<m;i++)
{
Union_Set(ss[i]);
}
cout<<ans<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
작은 재료 : 결함 혼입, 테스트 레벨, 공정 책임결함은 후공정에서 적출할수록 비용이 부풀기 때문에 조기에 적출하는 것이 이상적입니다. 그럼에도 불구하고 결함의 종류에 따라 조기에 발견되는 것이나 후공정에서 처음으로 나타나게 되는 것이 있습니다. 예를 들어 컴파일러...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.