2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路
Tree and Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
The set { 1,2,3,…,N } contains a total of N! unique permutations, let’s say the i-th permutation is Pi and Pi,j is its j-th number.
For the i-th permutation, it can be a traverse sequence of the tree with N vertices, which means we can go from the Pi,1-th vertex to the Pi,2-th vertex by the shortest path, then go to the Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,N-th vertex, let’s define the total distance of this route as D(Pi) , so please calculate the sum of D(Pi) for all N! permutations.
The first line of each test case contains one integer N ( 1≤N≤105 ) .
For the next N−1 lines, each line contains three integer X, Y and L, which means there is an edge between X-th vertex and Y-th of length L ( 1≤X,Y≤N,1≤L≤109 ) .
1 2 1
2 3 1
3
1 2 1
1 3 2
题意:求最小的n个点的全排列路径之和
分析:将所有n个点的排列写出来,容易发现每两个点的最小路径对答案贡献了2*(n-1)!*dis[i][j]
所以求一遍每两个点的最短路径,即2*(n-1)!*dis[i][j]
求任意两点的最短路径参考博客:https://blog.csdn.net/acmore_xiong/article/details/51958921
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<memory.h>
using namespace std;
#define ll long long
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e5+10; struct nd
{
int v,len;
};
vector<nd>vet[N];
long long n,dp[N],sum[N],c[N]; void DFS(int rt,int fa)
{
sum[rt]=1;
for(int i=0; i<vet[rt].size(); i++)
{
int son=vet[rt][i].v;
int len=vet[rt][i].len;
if(son==fa) continue;
DFS(son,rt);
(sum[rt]+=sum[son])%=mod;
dp[rt]+=(dp[son]+(sum[son]*(n-sum[son]))%mod*len%mod)%mod;
dp[rt] = (dp[rt]+mod)%mod;
}
} void init() {
memset(sum,0,sizeof(sum));
memset(dp,0,sizeof(dp));
} void solve() {
ll u,v,len;
while(cin>>n)
{
for(int i=0; i<=n; i++)vet[i].clear();
for(int i=1; i<=n-1; i++)
{
cin>>u>>v>>len;
u--,v--;
nd p1,p2;
p1.v=v,p2.v=u;
p1.len=p2.len=len;
vet[u].push_back(p1);
vet[v].push_back(p2);
}
init();
DFS(0,-1);
cout<<dp[0]*c[n]%mod<<endl;
}
} int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
c[2]=2;
for(int i=3;i<N;i++) c[i]=(i-1)*c[i-1]%mod;
solve();
return 0;
}
2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路的更多相关文章
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟
Dream Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Find Integer 数论
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛
给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法
http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...
随机推荐
- 【iOS】代码片段库
若在某个 Objective-C 的实现文件中输入 init,Xcode 会自动列出一系列备选项,如图所示: Xcode 自动加入的这段代码源自代码片段库(code snippet library), ...
- codeforces 339 D.Xenia and Bit Operations(线段树)
这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了. 题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然 ...
- 用机智云做PWM占空比控制电机,物联网智能家居应用
因为是新申请的博客,所以申请了总想往里面加点东西,所以把我之前在机智云写的帖子复制了过来 (各位抱歉,由于之前上传的文件可能有错误,之前上传的文件PWM不能用,那么我又重新上传了一个文件,这个文件 ...
- 在vue中监听storage的变化
1.首先在main.js中给Vue.protorype注册一个全局方法,其中,我们约定好了想要监听的sessionStorage的key值为’watchStorage’,然后创建一个StorageEv ...
- Java的几种常见排序算法
一.所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法.排序算法在很多领域得到相当地重视,尤其是在大量数据的处理方面. ...
- Linux基础进程管理优先级
一.进程优先级 Linux进程调度及多任务 每个cpu(或者cpu核心)在一个时间点上只能处理一个进程,通过时间片技术,Linux实际能够运行的进程(和线程数)可以超出实际可用的cpu及核心数量.Li ...
- kubernetes lowB安装方式
kubernetes离线安装包,仅需三步 基础环境 关闭防火墙 selinux $ systemctl stop firewalld && systemctl disable fire ...
- 【作品】超实用C++分数类
引言 我们说,编程语言的精髓在于封装,而面向对象语言完胜面向过程语言的原因就是具有更好的可封装性,而C++就是这样的一种多范型语言,常用而复杂的工作完全不必在每一份源文件中重敲,就好像我们不需要自己手 ...
- centOS 如何查看知道自己的版本号
今天遇到一个尴尬的问题 , 竟然找不到centOS7x这个版本系统 然后我就问大佬们,大佬们1810 是哪哪哪个版本说的我还是懵逼 然后我就发挥我那不要脸的精神 问:'这是有什算发算的吗' 很是尴尬 ...
- 页面性能监控之performance
页面性能监测之performance author: @TiffanysBear 最近,需要对业务上的一些性能做一些优化,比如降低首屏时间.减少核心按钮可操作时间等的一些操作:在这之前,需要建立的就是 ...