MstTime limit:2000/1000ms (java/others) Memory limit:128000/64000kb (java/others) Problem Descriptiongiven a connected, undirected graph, a spanning tree of that graph was a subgraph that is a tree and con Nects all the vertices together. A Single graph can has many different spanning trees. We can also assign a weight to each edge, which was a number representing how unfavorable it was, and use this to assign a W Eight to a spanning tree by computing the sum of the weights of the edges in that spanning tree. A minimum spanning tree (MST) is then a spanning tree with weight less than or equal to the weight of every other spanning Tree.
------from Wikipedia
Now we do the problem more complex. We assign each edge, kinds of weight:length and cost. We call a spanning tree with sum of length less than or equal to others MST. And we want to find a MST who have minimal sum of cost. Input
There is multiple test cases.
The first line contains, integers N and M indicating the number of vertices and edges in the Gragh.
The next M lines, each line contains three integers a, B, L and C indicating there is an edge with l length and c cos T between A and B.
1 <= N <= 10,000
1 <= M <= 100,000
1 <= A, b <= N
1 <= L, c <= 10,000
Outputfor each test case output, integers indicating the sum of length and cost of corresponding MST.
If you can find the corresponding MST, please output "-1-1". Sample Input
4 51 2 1 1 2 3 1 13 4 1 11 3 1 22 4 1 3
Sample Output
3 3
sourcedut200901102managerdut200901102 Problem Solving: Yes, yes, it's MST, just a double-keyword sort.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN =500010;5 structarc{6 intU,v,length,cost;7 BOOL operator< (ConstArc &rhs)Const{8 if(length = = rhs.length)returnCost <Rhs.cost;9 returnLength <rhs.length;Ten } One }E[MAXN]; A intUF[MAXN]; - intFind (intx) { - if(x = uf[x]) uf[x] =Find (uf[x]); the returnUf[x]; - } - intMain () { - intn,m; + while(~SCANF ("%d%d",&n,&m)) { - for(inti =0; I < m; ++i) +scanf"%d%d%d%d",&e[i].u,&e[i].v,&e[i].length,&e[i].cost); A for(inti =0; I <= N; ++i) Uf[i] =i; atSort (e,e +m); -LL length =0, cost =0, cnt =0; - for(inti =0; I < m && CNT +1< n; ++i) { - intU =Find (e[i].u); - intv =Find (E[I].V); - if(U = = v)Continue; inUf[u] =v; -Length + =e[i].length; toCost + =E[i].cost; +++CNT; - } the if(CNT +1= = N) printf ("%lld%lld\n", length,cost); * ElsePuts"-1-1"); $ }Panax Notoginseng return 0; -}View Code
Acdream 1135 MST