您的位置:首页 > 产品设计 > UI/UE

BNU 16485 Build The Electric System

2014-08-30 20:44 459 查看


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

Type:

None

None Graph Theory
2-SAT Articulation/Bridge/Biconnected Component
Cycles/Topological Sorting/Strongly Connected Component
Shortest Path
Bellman Ford Dijkstra/Floyd Warshall
Euler Trail/Circuit
Heavy-Light Decomposition Minimum Spanning Tree
Stable Marriage Problem
Trees Directed Minimum Spanning Tree
Flow/Matching
Graph Matching Bipartite Matching
Hopcroft–Karp Bipartite Matching
Weighted Bipartite Matching/Hungarian Algorithm
Flow
Max Flow/Min Cut Min Cost Max Flow
DFS-like
Backtracking with Pruning/Branch and Bound
Basic Recursion IDA* Search
Parsing/Grammar Breadth First Search/Depth First Search
Advanced Search Techniques
Binary Search/Bisection Ternary Search
Geometry
Basic Geometry Computational Geometry
Convex Hull
Pick's Theorem Game Theory
Green Hackenbush/Colon Principle/Fusion Principle
Nim Sprague-Grundy Number
Matrix Gaussian Elimination
Matrix Exponentiation Data Structures
Basic Data Structures Binary Indexed Tree
Binary Search Tree
Hashing Orthogonal Range Search
Range Minimum Query/Lowest Common Ancestor
Segment Tree/Interval Tree Trie Tree
Sorting
Disjoint Set String
Aho Corasick Knuth-Morris-Pratt
Suffix Array/Suffix Tree Math
Basic Math Big Integer Arithmetic
Number Theory Chinese Remainder Theorem
Extended Euclid
Inclusion/Exclusion Modular Arithmetic
Combinatorics
Group Theory/Burnside's lemma Counting
Probability/Expected Value
Others Tricky
Hardest Unusual
Brute Force Implementation
Constructive Algorithms Two Pointer
Bitmask Beginner
Discrete Logarithm/Shank's Baby-step Giant-step Algorithm
Greedy Divide and Conquer
Dynamic Programming
Tag it!

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 B 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


有n个城市,有m条线路,每条线路a,b,c表示a到b的线路需要花费c的费用维修,要求能将所有城市联通的最小维修花费。

思路:按照花费从小到大将路线排序,然后再来一次并查集整合所有线路。


#include <iostream>
#include <algorithm>
using namespace std;
const int Max=1010;
int father[Max];
int Find(int x)
{
while (x!=father[x])
{
x=father[x];
}
return x;
}
struct node
{
int a;
int b;
int c;
}num[40010];
int cmp(node a,node b)
{
return a.c<b.c;
}
int main()
{
int t,a,b;
int ans;
cin>>t;
while (t--)
{
ans=0;
cin>>a>>b;
for (int i=0; i<a; i++)
{
father[i]=i;
}
for (int i=0; i<b; i++)
{
cin>>num[i].a>>num[i].b>>num[i].c;
}

sort(num, num+b, cmp);
for (int i=0; i<b; i++)
{
int fx=Find(num[i].a);
int fy=Find(num[i].b);
if(fx!=fy)
{
father[fx]=fy;
ans+=num[i].c;
}
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: