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 ...
随机推荐
- Ping、Traceroute工作原理
在工作开发过程中,我们经常会使用到ping和traceroute.在这里,我们将细述其工作原理,让你在会用的基础之上理解其内部工作过程. ICMP应用实例--Ping Ping 是 ICMP 的一个重 ...
- JVM和GC的工作原理
转载于https://uestc-dpz.github.io JVM Java 虚拟机 Java 虚拟机(Java virtual machine,JVM)是运行 Java 程序必不可少的机制.JVM ...
- Hadoop 系列(八)—— 基于 ZooKeeper 搭建 Hadoop 高可用集群
一.高可用简介 Hadoop 高可用 (High Availability) 分为 HDFS 高可用和 YARN 高可用,两者的实现基本类似,但 HDFS NameNode 对数据存储及其一致性的要求 ...
- 1、JAVA的小白之路
大学的时光过得很快,转眼我已经大二了,在大一时,学习了C\C++,对于语言有一定基础,在未来的道路上,我需要攒足干劲,积累足够的知识和技能,去走上社会. 我的第一任大学班主任告诉我:“作为程序员,你至 ...
- Netty学习(二)-Helloworld Netty
这一节我们来讲解Netty,使用Netty之前我们先了解一下Netty能做什么,无为而学,岂不是白费力气! 1.使用Netty能够做什么 开发异步.非阻塞的TCP网络应用程序: 开发异步.非阻塞的UD ...
- HomeKit智能球泡
产品名称: 智能LED灯泡调光调色 接入苹果HomeKit家庭(无需网关).天猫精灵.小爱.小度.Google.ALEXA 产品价格:9.9 本产品是针对HomeKit的产品,没有iphone手机,配 ...
- DedeCMS 5.7 sp1远程文件包含漏洞(CVE-2015-4553)
DedeCMS 5.7 sp1远程文件包含漏洞(CVE-2015-4553) 一.漏洞描述 该漏洞在/install/index.php(index.php.bak)文件中,漏洞起因是$$符号使用不当 ...
- Docker入门-docker compose的使用
Compose简介 Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排.其代码目前在https://github.com/docker/compose 上开源. ...
- 拼写单词[哈希表]----leetcode周赛150_1001
题目描述: 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我 ...
- linux+jenkins+python+svn 自动化测试集成之路
本文背景: 背景1---个人基础: 本机win7上安装pycharm,使用python搭建API自动化测试框架,本地运行Pass.本机上搭建jenkins,创建测试任务,定时构建Pass. 背景2-- ...