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. Linux 环境下安装 Nginx+php+mysql 开发环境

    一直以来都没有养成记录学习的好习惯,我想从这么一天开始,把自己学习和工作中的经验和坑都记录下来.等到以后老的时候还有可以回忆的地方. 最近在学习linux,虽然已经玩linux很久了,但是没有怎么用心 ...

  2. 数据库 join

    数据库 join 最近有个项目用到了数据库,写 sql 时有联表查询,不知道怎么写,怎过滤,查了一些资料,补充了一些知识点. 以下是链接: 维基百科 JOIN关联表中ON,WHERE后面跟条件的区别 ...

  3. 关于React的赋值与调用方法

    #关于React的赋值与调用方法 比如调用方法的时候我们可以这样来使用closeFrm() <div className = "infoFrm_close" onMouseO ...

  4. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  5. 异步 Thread

    Threads 最早的版本,微软推荐不要再使用Thread了thread不支持带返回值的方法本身也没有线程回调,但是可以自己做一个 private void btnThreads_Click(obje ...

  6. Servlet和JSP之有关Servlet和JSP的梳理(一)

    大二第一学期的时候有学JSP的课,但是因为在开学之前做过JSP的小项目,所以一个学期的课也没听,直到期末考试成绩出来了,才回想JSP的内容还有多少记得,没想到模模糊糊也记不起多少,赶紧回头学回来.接下 ...

  7. leecode 旋转数组

    描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋 ...

  8. PostgressSQL-Installation

    安装 sudo apt install -y postgresql 自动生成一个名为 postgres 的 Linux 系统用户 $ finger postgres Login: postgres N ...

  9. git快速入门(MAC系统,github,ssh key)

    如果使用过svn的话,git大致可以认为是多了本地库的svn.git先本地提交commit到本地库,然后再push到远程服务器的库.git是分布式的代码管理工具,基于SSH协议.ssh的作用就是为了不 ...

  10. Ibatis入门基本语法

    1.       Ibatis是开源软件组织Apache推出的一种轻量级的对象关系映射(ORM)框架,和Hibernate.Toplink等在java编程的对象持久化方面深受开发人员欢迎. 对象关系映 ...