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,只是是双关键字排序
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct arc{
int u,v,length,cost;
bool operator<(const arc &rhs)const{
if(length == rhs.length) return cost < rhs.cost;
return length < rhs.length;
}
}e[maxn];
int uf[maxn];
int Find(int x){
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i = ; i < m; ++i)
scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].length,&e[i].cost);
for(int i = ; i <= n; ++i) uf[i] = i;
sort(e,e + m);
LL length = ,cost = ,cnt = ;
for(int i = ; i < m && cnt + < n; ++i){
int u = Find(e[i].u);
int v = Find(e[i].v);
if(u == v) continue;
uf[u] = v;
length += e[i].length;
cost += e[i].cost;
++cnt;
}
if(cnt + == n) printf("%lld %lld\n",length,cost);
else puts("-1 -1");
}
return ;
}

ACdream 1135 MST的更多相关文章

  1. ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)

    F - MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...

  2. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  3. 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)

    怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...

  4. BZOJ 2654 & 玄学二分+MST

    题意: 给一张图,边带权且带颜色黑白,求出一棵至少包含k条白边的MST SOL: 正常人都想优先加黑边或者是白边,我也是这么想的...你看先用白边搞一棵k条边的MST...然后维护比较黑边跟白边像堆一 ...

  5. LA 5713 秦始皇修路 MST

    题目链接:http://vjudge.net/contest/144221#problem/A 题意: 秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通.道士徐福声称他可以用法术修路,不 ...

  6. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  7. [BZOJ2654]tree(二分+MST)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...

  8. CodeForces 125E MST Company

    E. MST Company time limit per test 8 seconds memory limit per test 256 megabytes input standard inpu ...

  9. 2015baidu复赛2 连接的管道(mst && 优先队列prim)

    连接的管道 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. PL/SQL 多表关联UPDATE

    假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 upda ...

  2. 用java代码写一个简单的网上购物车程序

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...

  3. [BZOJ2434][Noi2011]阿狸的打字机 AC自动机+树状数组+离线

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2434 题目中这种多个串匹配的问题,一下子就想到了AC自动机.然后发现如果要建立AC自动机, ...

  4. IE下的圆角

    元素{ position: relative;/*必须*/ z-index: 10;/*必须*/ border-radius: 8px; -moz-border-radius: 8px; -webki ...

  5. dos命令-环境变量-数据类型-命名规范

    JAVA第一天笔记--dos命令-环境变量-数据类型-命名规范 1.能够阐述JDK和JRE之间区别 JDK(Java Development Kit)是提供给开发人员使用的JAVA开发工具包(java ...

  6. Android.mk模板

    此文列出Android.mk的常用模板(部分内容源于多篇他人博客,这里不具体指出),如有错漏,还请在评论中指出,后期持续更新   #链接第三方动态库,在和部分android源码的编译中验证不过 LOC ...

  7. [转+补]Android打包so后魅族5中安装运行崩溃问题的解决方法

    上周在做噪音检测so集成中,遇到不同的so库打包到 APK 时,安装在某些机器上,出现 java.lang.UnsatisfiedLinkError 加载失败. 为此,深究了一下原理,和给出了解决方案 ...

  8. RunTests.sh && RunIPhoneSecurityd.sh

    https://github.com/gh-unit/gh-unit/blob/master/Scripts/RunTests.sh     #!/bin/sh   # If we aren't ru ...

  9. Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.withCharset(Ljava/lang/String;)Lorg/apache/http/entity/ContentType;

    解决方案是:第一点先检查一下使用的包是否冲突,是否是版本号一致.第二点是增加一个包 忙活了好久才解决了这个异常,小小的激动一下啊啊

  10. HTML5资源汇总(更新游戏引擎cocos2d-html5)

    我也是现学现用,想了解的可以看看效果,想知道实现的也有源码 http://cocos2d-html5.org Cocos2d-HTML5 API和Cocos2d-x一致,同样的代码可以支持cocos2 ...