zoukankan      html  css  js  c++  java
  • 树(最小乘积生成树,克鲁斯卡尔算法):BOI timeismoney

    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画框有类似的思想。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=210;
     6 const int maxm=10010;
     7 int fa[maxn],a[maxm],b[maxm];
     8 int u[maxm],v[maxm],n,m;
     9 
    10 struct Node{
    11     int x,y,z,id;
    12     Node(int a=0,int b=0,int c=0,int d=0){
    13         x=a;y=b;z=c;id=d;
    14     }
    15     friend bool operator <(Node a,Node b){
    16         return a.z<b.z;
    17     }
    18 }p[maxm];
    19 
    20 struct Point{
    21     int x,y;
    22     Point(int a=0,int b=0){
    23         x=a;y=b;
    24     }
    25     friend bool operator ==(Point a,Point b){
    26         return a.x==b.x&&a.y==b.y;
    27     }
    28 }lo,hi;
    29 
    30 int Find(int x){
    31     return fa[x]==x?x:fa[x]=Find(fa[x]);
    32 }
    33 
    34 Point Get_Ans(){
    35     sort(p+1,p+m+1);Point ret(0,0);
    36     for(int i=1;i<=n;i++)fa[i]=i;
    37     for(int i=1;i<=m;i++){
    38         int x=p[i].x,y=p[i].y;
    39         if(Find(x)!=Find(y)){
    40             ret.x+=a[p[i].id];
    41             ret.y+=b[p[i].id];
    42             fa[Find(y)]=Find(x);
    43         }
    44     }
    45     return ret;
    46 }
    47 
    48 Point Solve(Point l,Point r){
    49     for(int i=1;i<=m;i++)
    50         p[i]=Node(u[i],v[i],b[i]*(r.x-l.x)-a[i]*(r.y-l.y),i);
    51     Point mid=Get_Ans();
    52     if(mid==l||mid==r)return l.x*l.y<r.x*r.y?l:r;
    53     l=Solve(l,mid);r=Solve(mid,r);
    54     return l.x*l.y<r.x*r.y?l:r;    
    55 }
    56 
    57 int main(){
    58     freopen("timeismoney.in","r",stdin);
    59     freopen("timeismoney.out","w",stdout);
    60     scanf("%d%d",&n,&m);
    61     for(int i=1;i<=m;i++){
    62         scanf("%d%d",&u[i],&v[i]);u[i]+=1;v[i]+=1;
    63         scanf("%d%d",&a[i],&b[i]);
    64     }
    65     
    66     for(int i=1;i<=m;i++)p[i]=Node(u[i],v[i],a[i],i);lo=Get_Ans();
    67     for(int i=1;i<=m;i++)p[i]=Node(u[i],v[i],b[i],i);hi=Get_Ans();    
    68     Point ans=Solve(lo,hi);
    69     printf("%d %d
    ",ans.x,ans.y);    
    70     return 0;
    71 }
  • 相关阅读:
    iOS 成员变量和属性的区别
    程序如何在RAM ROM运行,内存分配与分区
    iOS CGAffineTransform你了解多少?
    iOS配置字符表,查看Crash
    苹果开发账号过期不续费会怎样?
    iOS蓝牙接收外设数据自动中断
    数据结构简述
    antd按需加载,配置babel-plugin-import插件,编译后报错.bezierEasingMixin()解决方案
    docker 启动 nginx 服务
    安装docker
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5700737.html
Copyright © 2011-2022 走看看