The NetLine company wants to offer broadband internet to N towns. For this, it suffices to construct
a network of N-1 broadband links between the towns, with the property that a message can travel
from any town to any other town on this network. NetLine has already identified all pairs of towns
between which a direct link can be constructed. For each such possible link, they know the cost and
the time it would take to construct the link.
The company is interested in minimizing both the total amount of time (links are built one at a time)
and the total amount of money spent to build the entire network. Since they couldn’t decide among
the two criteria, they decided to use the following formula to evaluate the value of a network:
SumTime = sum of times spent to construct the chosen links
SumMoney = sum of the money spent to construct the chosen links
V = SumTime * SumMoney
Task
Find a list of N-1 links to build, which minimizes the value V.
Description of input
The first line of input contains integers N – the number of towns and M – the number of pairs of
towns which can be connected. The towns are numbered starting from 0 to N-1. Each of the next M
lines contain four integers x, y, t and c – meaning town x can be connected to town y in time t and
with cost c.
Description of output
In the first line of output print two numbers: the total time (SumTime) and total money (Sum-
Money) used in the optimal solution (the one with minimal value V), separated by one space. The
next N-1 lines describe the links to be constructed. Each line contains a pair of numbers (x,y) describing
a link to be build (which must be among the possible links described in the input file). The
pairs can be printed out in any order. When multiple solutions exist, you may print any of them.

Constraints

· 1 ≤ N ≤ 200
· 1 ≤ M ≤ 10 000
· 0 ≤ x,y ≤ N-1
· 1 ≤ t,c ≤ 255
· One test has M = N - 1
· 40% of the tests will have for each possible link t = c
Example
timeismoney.in
5 7
0 1 161 79
0 2 161 15
0 3 13 153
1 4 142 183
2 4 236 80
3 4 40 241
2 1 65 92

timeismoney.out

279 501
2 1
0 3
0 2
3 4

  方案啥的很好解决,就不写了。

  和HNOI2014画框有类似的思想。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int maxm=;
int fa[maxn],a[maxm],b[maxm];
int u[maxm],v[maxm],n,m; struct Node{
int x,y,z,id;
Node(int a=,int b=,int c=,int d=){
x=a;y=b;z=c;id=d;
}
friend bool operator <(Node a,Node b){
return a.z<b.z;
}
}p[maxm]; struct Point{
int x,y;
Point(int a=,int b=){
x=a;y=b;
}
friend bool operator ==(Point a,Point b){
return a.x==b.x&&a.y==b.y;
}
}lo,hi; int Find(int x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
} Point Get_Ans(){
sort(p+,p+m+);Point ret(,);
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<=m;i++){
int x=p[i].x,y=p[i].y;
if(Find(x)!=Find(y)){
ret.x+=a[p[i].id];
ret.y+=b[p[i].id];
fa[Find(y)]=Find(x);
}
}
return ret;
} Point Solve(Point l,Point r){
for(int i=;i<=m;i++)
p[i]=Node(u[i],v[i],b[i]*(r.x-l.x)-a[i]*(r.y-l.y),i);
Point mid=Get_Ans();
if(mid==l||mid==r)return l.x*l.y<r.x*r.y?l:r;
l=Solve(l,mid);r=Solve(mid,r);
return l.x*l.y<r.x*r.y?l:r;
} int main(){
freopen("timeismoney.in","r",stdin);
freopen("timeismoney.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&u[i],&v[i]);u[i]+=;v[i]+=;
scanf("%d%d",&a[i],&b[i]);
} for(int i=;i<=m;i++)p[i]=Node(u[i],v[i],a[i],i);lo=Get_Ans();
for(int i=;i<=m;i++)p[i]=Node(u[i],v[i],b[i],i);hi=Get_Ans();
Point ans=Solve(lo,hi);
printf("%d %d\n",ans.x,ans.y);
return ;
}

树(最小乘积生成树,克鲁斯卡尔算法):BOI timeismoney的更多相关文章

  1. 【最小乘积生成树】bzoj2395[Balkan 2011]Timeismoney

    设每个点有x,y两个权值,求一棵生成树,使得sigma(x[i])*sigma(y[i])最小. 设每棵生成树为坐标系上的一个点,sigma(x[i])为横坐标,sigma(y[i])为纵坐标.则问题 ...

  2. 【算法】最小乘积生成树 & 最小乘积匹配 (HNOI2014画框)

    今天考试的时候果然题目太难于是我就放弃了……转而学习了一下最小乘积生成树. 最小乘积生成树定义: (摘自网上一篇博文). 我们主要解决的问题就是当k = 2时,如何获得最小的权值乘积.我们注意到一张图 ...

  3. 最小生成树之Kruskal(克鲁斯卡尔)算法

    学习最小生成树算法之前我们先来了解下下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的所 ...

  4. 图->连通性->最小生成树(克鲁斯卡尔算法)

    文字描述 上一篇博客介绍了最小生成树(普里姆算法),知道了普里姆算法求最小生成树的时间复杂度为n^2, 就是说复杂度与顶点数无关,而与弧的数量没有关系: 而用克鲁斯卡尔(Kruskal)算法求最小生成 ...

  5. 最小生成树--克鲁斯卡尔算法(Kruskal)

    按照惯例,接下来是本篇目录: $1 什么是最小生成树? $2 什么是克鲁斯卡尔算法? $3 克鲁斯卡尔算法的例题 摘要:本片讲的是最小生成树中的玄学算法--克鲁斯卡尔算法,然后就没有然后了. $1 什 ...

  6. prim算法,克鲁斯卡尔算法---最小生成树

    最小生成树的一个作用,就是求最小花费.要在n个城市之间铺设光缆,主要目标是要使这 n 个城市的任意两个之间都可以通信,但铺设光缆的费用很高,且各个城市之间铺设光缆的费用不同,因此另一个目标是要使铺设光 ...

  7. HDU5697 刷题计划 dp+最小乘积生成树

    分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...

  8. 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal&#39;s algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...

  9. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. python s12 day2

    python s12 day2   入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...

  2. oracle约束条件状态

    Oracle完整性约束有一下4种: • DISABLE NOVALIDATE • ENABLE NOVALIDATE • DISABLE VALIDATE • ENABLE VALIDATE   •  ...

  3. wpf 调用线程必须为sta 因为许多ui组件都需要

    解决 办法 public void SomeMethod() { var task = System.Windows.Application.Current.Dispatcher.BeginInvok ...

  4. 取出当前会话的sid等

    select distinct sess.SID     db_sid,                sess.SERIAL# db_serial#,                process. ...

  5. Java中char占用几个字节

    在讨论这个问题之前,我们需要先区分unicode和UTF. unicode :统一的字符编号,仅仅提供字符与编号间映射.符号数量在不断增加,已超百万.详细:[https://zh.wikipedia. ...

  6. cas sso单点登录系列8_抛弃Https让Cas以Http协议提供单点登录服务

    转:http://blog.csdn.net/ycyk_168/article/details/18668951 本文环境: 1.apache-tomcat-7.0.50-windows-x86 2. ...

  7. 在O(1)时间删除链表结点

    //给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 1 struct ListNode //结点结构 { int m_nValue; ListNode* m_pNext; ...

  8. 总结Widows 7 Start->Run 命令

    Widows + R基本上成为很常用的方式,那么通过Windows + R我们可以在运行中做什么手脚呢. 下面从最基本的系统命令说起 notepad--------打开记事本    services. ...

  9. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...

  10. 防止mysql注入

    function check($sql_str) { $checks=eregi('select|insert|update|delete|\'|\/|\\\|\*|\.|union|into|loa ...