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

Problem Description
There are N vertices connected by N−1 edges, each edge has its own length.
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.
 
Input
There are 10 test cases at most.
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 ) .
 
Output
For each test case, print the answer module 109+7 in one line.
 
Sample Input
3
1 2 1
2 3 1
3
1 2 1
1 3 2
 
Sample Output
16 24

题意:求最小的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 找规律+求任意两点的最短路的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  2. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu 6440 Dream 模拟

    Dream Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Find Integer 数论

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  6. HDU - 6440 Dream 2018中国大学生程序设计竞赛 - 网络选拔赛

    给定的\(p\)是素数,要求给定一个加法运算表和乘法运算表,使\((m+n)^p = m^p +n^p(0 \leq m,n < p)\). 因为给定的p是素数,根据费马小定理得 \((m+n) ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法

    http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...

  9. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...

随机推荐

  1. 初次见面C#排坑记录

    排一次开发C#遇到的坑,同时说一下自己对C#中文件夹构建方式的体会. 一个开发Java的人留下了痛苦的泪水,变量命名规则不一样,连括号打的都不一样,

  2. iOS程序员 如何提升核心竞争力,防止自己被裁员?

    前言: 核心竞争力最早由普拉哈拉德和加里·哈默尔两位教授提出,通常认为核心竞争力,即企业或个人相较于竞争对手而言所具备的竞争优势与核心能力差异, 说白了就是你的优势,而且最好是独一无二的的优势,这就是 ...

  3. 搭建nexus私服

    一.安装 1.从网上下载nexus软件https://www.sonatype.com/download-oss-sonatype  下载Nexus Repository Manager OSS软件包 ...

  4. HttpClientFactory 使用说明 及 对 HttpClient 的回顾和对比

    目录 HttpClient 日常使用及坑点: HttpClientFactory 优势: HttpClientFactory 使用方法: 实战用法1:常规用法 1. 在 Startup.cs 中进行注 ...

  5. JDK容器类List,Set,Queue源码解读

    List,Set,Queue都是继承Collection接口的单列集合接口.List常用的实现主要有ArrayList,LinkedList,List中的数据是有序可重复的.Set常用的实现主要是Ha ...

  6. Android活动(Activity)创建及生命周期

       Activity是Android的门面,可以与用户进行互动的重要模块,凡是在应用中可以看到的东西,都是放在活动中的.   在学习新的技术时,我喜欢将需要学习的技术与自己懂得技术进行类似比较,而活 ...

  7. hadoop学习(二)----HDFS简介及原理

    前面简单介绍了hadoop生态圈,大致了解hadoop是什么.能做什么.带着这些目的我们深入的去学习他.今天一起看一下hadoop的基石--文件存储.因为hadoop是运行与集群之上,处于分布式环境之 ...

  8. redhat linux 5.3安装activeMQ

    安装环境:linux redhat enterprise 5.3 activemq版本:5.9.01.从http://activemq.apache.org/download.html地址下载apac ...

  9. android ——通知管理

    public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle saved ...

  10. AutoCAD.NET中添加图形对象的基本步骤与实例演示

    https://blog.csdn.net/u011170962/article/details/37755201 要创建一个图形对象,需要遵循下面的步骤:1.得到创建对象的图形数据库:2.在内存中创 ...