ACdream 1135 MST
MST
Problem Description
------ 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
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
Manager
#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的更多相关文章
- ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)
F - MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)
怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...
- BZOJ 2654 & 玄学二分+MST
题意: 给一张图,边带权且带颜色黑白,求出一棵至少包含k条白边的MST SOL: 正常人都想优先加黑边或者是白边,我也是这么想的...你看先用白边搞一棵k条边的MST...然后维护比较黑边跟白边像堆一 ...
- LA 5713 秦始皇修路 MST
题目链接:http://vjudge.net/contest/144221#problem/A 题意: 秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通.道士徐福声称他可以用法术修路,不 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- [BZOJ2654]tree(二分+MST)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...
- CodeForces 125E MST Company
E. MST Company time limit per test 8 seconds memory limit per test 256 megabytes input standard inpu ...
- 2015baidu复赛2 连接的管道(mst && 优先队列prim)
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
随机推荐
- Java GUI设置图标
ImageIcon是Icon接口的一个实现类. ImageIcon类的构造函数: ImageIcon() ImageIcon(String filename) //本地图片文件 ImageIcon ...
- 2、替换空格------------>剑指offer系列
题目 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 代码 1.直接用空格将字符串切割成 ...
- 浅析 var that = this;
在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事. 在JavaScript中,this代表的是当前对象. var t ...
- [翻译] API测试的最佳实践 - 介绍
API测试的最佳实践 - 介绍 在上一篇“是什么让API测试很叼”一文中,我们讨论API与其他形式的软件测试的差异.部分是因为API之间的通信压根就没考虑让你能读懂,纯粹是为了方便计算机之间的交互而设 ...
- rsyslog+analyzer
环境:最小化centos6.2 准备:rsyslog-4.6.1.tar.gz loganalyzer-3.6.3.tar.gz wget http://download.adiscon.com/l ...
- 快速排序的一种Java实现
快速排序是笔试和面试中很常见的一个考点.快速排序是冒泡排序的升级版,时间复杂度比冒泡排序要小得多.除此之外,快速排序是不稳定的,冒泡排序是稳定的. 1.原理 (1)在数据集之中,选择一个元素作为&qu ...
- js3
举几个小例子: 1. 九九乘法表 var s = "<table>"; for (var i=1;i<=9;i++) { s += "<tr> ...
- codevs 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description CCC的足球比赛和传统的足球比赛有一点不同, 一次进球当且仅当先后接触到球的 ...
- codevs 1097 校门外的树 2005年NOIP全国联赛普及组 (线段树)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可 ...
- 一条SQL语句在MySQL中是如何执行的
概览 本篇文章会分析下一个sql语句在mysql中的执行流程,包括sql的查询在mysql内部会怎么流转,sql语句的更新是怎么完成的. 一.mysql架构分析 mysql主要分为Server层和存储 ...