ACdream 1135(MST-最小生成树边上2个值,维护第一个最小的前提下让还有一个最小)
F - MST
Problem Description
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
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个值,维护第一个最小的前提下让还有一个最小)的更多相关文章
- ACdream 1135 MST
MST Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Problem Descrip ...
- MST最小生成树及克鲁斯卡尔(Kruskal)算法
最小生成树MST,英文名如何拼写已忘,应该是min spaning tree吧.假设一个无向连通图有n个节点,那么它的生成树就是包括这n个节点的无环连通图,无环即形成树.最小生成树是对边上权重的考虑, ...
- MST最小生成树及Prim普鲁姆算法
MST在前面学习了Kruskal算法,还有一种算法叫做Prim的.这两者的区别是Prim算法适合稠密图,比如说鸟巢这种几乎所有点都有相连的图.其时间复杂度为O(n^2),其时间复杂度与边的数目无关:而 ...
- MST最小生成树
首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...
- [BZOJ1937][SHOI2004]Mst最小生成树(KM算法,最大费用流)
1937: [Shoi2004]Mst 最小生成树 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 802 Solved: 344[Submit][Sta ...
- Prim求MST最小生成树
最小生成树即在一个图中用最小权值的边将所有点连接起来.prim算法求MST其实它的主要思路和dijkstra的松弛操作十分相似 prim算法思想:在图中随便找一个点开始这里我们假定起点为“1”,以点1 ...
- 【BZOJ1937】[Shoi2004]Mst 最小生成树 KM算法(线性规划)
[BZOJ1937][Shoi2004]Mst 最小生成树 Description Input 第一行为N.M,其中 表示顶点的数目, 表示边的数目.顶点的编号为1.2.3.…….N-1.N.接下来的 ...
- POJ1258 Agri-Net MST最小生成树题解
搭建一个最小代价的网络,最原始的最小生成树的应用. 这里使用Union find和Kruskal算法求解. 注意: 1 给出的数据是原始的矩阵图,可是须要转化为边表示的图,方便运用Kruskal,由于 ...
- Hbase 学习笔记(一) Hbase的物理模型 Hbase为每个值维护了一个多级索引,即<key, column family, column name, timestamp>
比如第一个region 代表 0-100 第二个region 代表 101 -200的 分的越多越不好管理,但同时方便了并行化处理,并发度越高,处理的越快.mapreduce就是按照rowkey的 ...
随机推荐
- 在Web Api中快速实现JSonp
本文翻译自:http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started Concept: 同源策 ...
- this小记
this小记 太久没有研究底层的js相关,今晚差点被紫红爸爸上课了. 正题 var net=new Object(); //定义一个全局变量net net.AjaxRequest=function(u ...
- could not create the java virtual machine(转)
打开不了myeclipse,报错“could not create the java virtual machine”,解决问题过程如下: 1.在D:\MyEclipse\eclipse有个eclip ...
- Hadoop MapReduce编程的一些个人理解
首先要实现mapreduce就要重写两个函数,一个是map 还有一个是reduce map(key ,value) map函数有两个參数,一个是key,一个是value 假设你的输入类型是TextIn ...
- HDOJ 1800 Flying to the Mars 盲目搜索......................so easy...........
check the original problem here:http://acm.hdu.edu.cn/showproblem.php?pid=1800 the AC code: #include ...
- .Net有许多Office,PDF,Email,HTML的控件
比如: Aspose.Total for .NET includes the following components: Aspose.Words for .NET 16.3.0 (4/13/2016 ...
- Qt5.2 android 环境搭建及其测试
1.<安装> 软件: qt-windows-opensource-5.2.0-android-x86-win32-offline //Qt开发环境 adt-bundle-windows-x ...
- 基于redis的cas集群配置(转)
1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...
- PHP学习之-面向对象
PHP学习之-面向对象 1.什么是对象 "世界万物皆对象",一切可以被抽象出来的东西都是对象.像花,草.看不到的"概念"都是对象. 2.对象的基本组成 a.属性 ...
- 基于TCP/IP协议的C++网络编程(API函数版)
源代码:http://download.csdn.net/detail/nuptboyzhb/4169959 基于TCP/IP协议的网络编程 定义变量——获得WINSOCK版本——加载WINSOCK库 ...