F - 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

图中边有2个值l,c,求关于l的MST,在此基础上求min∑c

直接把边按l从小到大(第一keyword),c从小到大(第二keyword)排序,然后用Kruskal算法

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
char a[MAXN][MAXN];
ll p10[MAXN]={0};
ll pow2(ll b)
{
if (b==1) return 10;
if (b==0) return 1;
if (p10[b]) return p10[b];
ll p=pow2(b/2)%F;
p=(p*p)%F;
if (b&1)
{
p=(p*10)%F;
}
p10[b]=p;
return p;
}
ll pow2(ll a,ll b)
{
if (b==1) return a;
if (b==0) return 1;
ll p=pow2(a,b/2)%F;
p=p*p%F;
if (b&1)
{
p=(p*a)%F;
}
return p;
}
ll tot[MAXN]={0};
ll mulinv(ll a)
{
return pow2(a,F-2);
}
int main()
{
// freopen("sum.in","r",stdin);
// freopen("sum.out","w",stdout);
scanf("%d",&n);
For(i,n)
{
scanf("%s",a[i]+1); }
/*
For(i,n)
{
For(j,n) cout<<a[i][j];
cout<<endl;
}
*/
For(i,n)
{
For(j,n) tot[i]+=a[i][j]-'0'+a[j][i]-'0';
} // For(i,n) cout<<tot[i]<<endl; // cout<<mul(pow2(10,1232),mulinv(pow2(10,1232)))<<endl;
// cout<<mulinv(9); ll c9=mulinv(9); For(i,n) p10[i]=pow2(i); ll ans=0;
For(i,n)
{
ll t=sub(p10[n-i+1],1),a=tot[i];
t=mul(t,c9);
t=mul(a,t);
ans=add(ans,mul(t,i));
}
cout<<ans<<endl; return 0;
}

ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)的更多相关文章

  1. ACdream 1135 MST

    MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Problem Descrip ...

  2. MST最小生成树及克鲁斯卡尔(Kruskal)算法

    最小生成树MST,英文名如何拼写已忘,应该是min spaning tree吧.假设一个无向连通图有n个节点,那么它的生成树就是包括这n个节点的无环连通图,无环即形成树.最小生成树是对边上权重的考虑, ...

  3. MST最小生成树及Prim普鲁姆算法

    MST在前面学习了Kruskal算法,还有一种算法叫做Prim的.这两者的区别是Prim算法适合稠密图,比如说鸟巢这种几乎所有点都有相连的图.其时间复杂度为O(n^2),其时间复杂度与边的数目无关:而 ...

  4. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...

  5. [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)

    1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 802  Solved: 344[Submit][Sta ...

  6. Prim求MST最小生成树

    最小生成树即在一个图中用最小权值的边将所有点连接起来.prim算法求MST其实它的主要思路和dijkstra的松弛操作十分相似 prim算法思想:在图中随便找一个点开始这里我们假定起点为“1”,以点1 ...

  7. 【BZOJ1937】[Shoi2004]Mst 最小生成树 KM算法(线性规划)

    [BZOJ1937][Shoi2004]Mst 最小生成树 Description Input 第一行为N.M,其中 表示顶点的数目, 表示边的数目.顶点的编号为1.2.3.…….N-1.N.接下来的 ...

  8. POJ1258 Agri-Net MST最小生成树题解

    搭建一个最小代价的网络,最原始的最小生成树的应用. 这里使用Union find和Kruskal算法求解. 注意: 1 给出的数据是原始的矩阵图,可是须要转化为边表示的图,方便运用Kruskal,由于 ...

  9. Hbase 学习笔记(一) Hbase的物理模型 Hbase为每个值维护了一个多级索引,即<key, column family, column name, timestamp>

      比如第一个region 代表 0-100 第二个region 代表 101 -200的 分的越多越不好管理,但同时方便了并行化处理,并发度越高,处理的越快.mapreduce就是按照rowkey的 ...

随机推荐

  1. IOS系统对fixed定位支持不好的解决方法

    问题: IOS 中所有浏览器,当页面上的输入框获得焦点时,呼出键盘. 页面底部的导航栏(position:fixed)会被键盘顶到页面的中间. 而当输入框失去焦点时,导航栏停留在页面中间,造成页面错乱 ...

  2. [译]TCP和UDP的区别

    译者:华科小涛:http://www.cnblogs.com/hust-ghtao/ 最近开始学习计算机网络的知识,找了些英文的资料,翻译过来,一是为了深入学习网络,也是为了锻炼自己看英文文档的能力. ...

  3. 《大象UML》看书笔记2:

    <大象UML>看书笔记2 抽象角度:                                                        在为现实世界建模的时候,首先要搞清楚有多 ...

  4. CentOS: make menuconfig error: curses.h: No such file or directory

    the problem  when use centos5 to build kernel or busybox step 1. Centos中关于 ncurses.h:no such file or ...

  5. 基于visual Studio2013解决C语言竞赛题之1017次数

         题目 解决代码及点评 /* 功能:有人说在400, 401, 402, ...499这些数中4这个数字共出现112次,请编程序判定这 种说法是否正确.若正确请打印出'YE ...

  6. TangoWalk小组课程与优惠(20131208更新) | TangoWalk 学跳阿根廷探戈舞

    TangoWalk小组课程与优惠(20131208更新) | TangoWalk 学跳阿根廷探戈舞 TangoWalk小组课程与优惠(20131208更新) Posted by redsky on 2 ...

  7. 远程调用内核接口(remote call kernel)

    -------------------------------------------------------------------------------- 标题: 远程调用内核接口(remote ...

  8. 操作系统栈溢出检測之ucosII篇

    操作系统栈溢出检測之uc/osII篇 Author               :       David Lin (林鹏) E-mail               :       linpeng1 ...

  9. 自定义navigationBar的高度

    原来看过一些解决办法,都不太好,最近解决自定义 tab bar的高度的问题,从中受到启发,找到下面的解决办法. 个人觉得和网上找到的其它方法比还是很简洁的. 关键是要调整navBarTransitio ...

  10. Qt图片显示效率的比较 转

    转http://blog.sina.com.cn/s/blog_5c70dfc80100r257.html 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不能够直接显示出来,要想能 ...