BNU 16485 Build The Electric System
Build The Electric System
Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 2966
64-bit integer IO format:
%lld Java class name:
Main
Prev
Submit
Status
Statistics
Discuss
Next
Font Size:
+
-
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 andB respectively 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
Source
The 5th Zhejiang Provincial Collegiate Programming Contest
Author
ZHOU, Ran
标题:nの村e本線路kは線路の修理費用で最低いくらでこれらの村の電力供給を回復できるかを聞く
考え方:まずe線を並べ替えて修理費用の少ないものを前に置く
それからどのいくつがつながっているのか調べてみて、修理しなくてもいいです.
また違う修理をつなげればOKです#include<stdio.h>
#include<algorithm>
using namespace std;
int father[50000];
int find(int x)
{
return father[x]==x?father[x]:find(father[x]);
}
struct ml
{
int a,b,k;
}p[50000];
int cmp(ml x,ml y)
{
return x.k<y.k;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,e;
scanf("%d%d",&n,&e);
for(int i=0;i<e;i++)
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].k);
sort(p,p+e,cmp);
for(int i=0;i<n;i++)
father[i]=i;
int sum=0;
for(int i=0;i<e;i++)
{
int fx=find(p[i].a);
int fy=find(p[i].b);
if(fx!=fy)
{
father[fx]=fy;
sum+=p[i].k;
}
}
printf("%d
",sum);
}
return 0;
}
Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 2966
64-bit integer IO format:
%lld Java class name:
Main
Prev
Submit
Status
Statistics
Discuss
Next
Font Size:
+
-
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 andB respectively 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
Source
The 5th Zhejiang Provincial Collegiate Programming Contest
Author
ZHOU, Ran
标题:nの村e本線路kは線路の修理費用で最低いくらでこれらの村の電力供給を回復できるかを聞く
考え方:まずe線を並べ替えて修理費用の少ないものを前に置く
それからどのいくつがつながっているのか調べてみて、修理しなくてもいいです.
また違う修理をつなげればOKです
#include<stdio.h>
#include<algorithm>
using namespace std;
int father[50000];
int find(int x)
{
return father[x]==x?father[x]:find(father[x]);
}
struct ml
{
int a,b,k;
}p[50000];
int cmp(ml x,ml y)
{
return x.k<y.k;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,e;
scanf("%d%d",&n,&e);
for(int i=0;i<e;i++)
scanf("%d%d%d",&p[i].a,&p[i].b,&p[i].k);
sort(p,p+e,cmp);
for(int i=0;i<n;i++)
father[i]=i;
int sum=0;
for(int i=0;i<e;i++)
{
int fx=find(p[i].a);
int fy=find(p[i].b);
if(fx!=fy)
{
father[fx]=fy;
sum+=p[i].k;
}
}
printf("%d
",sum);
}
return 0;
}