码迷,mamicode.com
首页 > 其他好文 > 详细

ACdream 1135 MST

时间:2015-11-16 23:59:29      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:

MST

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

Given a connected, undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together.  A single graph can have many different spanning trees. We can also assign a weight to each edge, which is a number representing how unfavorable it is, and use this to assign a weight 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 make the problem more complex. We assign each edge two 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 has minimal sum of cost.

Input

There are multiple test cases.
The first line contains two 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 are an edge with l length and c cost between a and b.

1 <= N <= 10,000
1 <= M <= 100,000
1 <= a, b <= N
1 <= l, c <= 10,000

Output

For each test case output two 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 5
1 2 1 1 
2 3 1 1
3 4 1 1
1 3 1 2
2 4 1 3

Sample Output

3 3

Source

dut200901102

Manager

 
解题:是的,没错,就是MST,只是是双关键字排序
技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 500010;
 5 struct arc{
 6     int u,v,length,cost;
 7     bool operator<(const arc &rhs)const{
 8         if(length == rhs.length) return cost < rhs.cost;
 9         return length < rhs.length;
10     }
11 }e[maxn];
12 int uf[maxn];
13 int Find(int x){
14     if(x != uf[x]) uf[x] = Find(uf[x]);
15     return uf[x];
16 }
17 int main(){
18     int n,m;
19     while(~scanf("%d%d",&n,&m)){
20         for(int i = 0; i < m; ++i)
21         scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].length,&e[i].cost);
22         for(int i = 0; i <= n; ++i) uf[i] = i;
23         sort(e,e + m);
24         LL length = 0,cost = 0,cnt = 0;
25         for(int i = 0; i < m && cnt + 1 < n; ++i){
26             int u = Find(e[i].u);
27             int v = Find(e[i].v);
28             if(u == v) continue;
29             uf[u] = v;
30             length += e[i].length;
31             cost += e[i].cost;
32             ++cnt;
33         }
34         if(cnt + 1 == n) printf("%lld %lld\n",length,cost);
35         else puts("-1 -1");
36     }
37     return 0;
38 }
View Code

 

ACdream 1135 MST

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4970306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!