首页 技术 正文
技术 2022年11月22日
0 收藏 427 点赞 3,067 浏览 2375 个字

传送门:点我

Tree and Permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2191    Accepted Submission(s): 826

Problem DescriptionThere 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. InputThere 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≤10^9 ) . OutputFor each test case, print the answer module 109+7 in one line. Sample Input31 2 12 3 131 2 11 3 2Sample Output1624   

  • 题目大意:

         第一行给定n,表示n个顶点,后面n-1行代表边的两个端点和边的权值。         要输出的是n个顶点全排列对所有情况,按点依次访问的时候,经过的权值和。         比如说样例1:         三个顶点,即对1,2,3全排列,共有6种方式。(1->2->3,1->3->2…….)         按每种情况的访问点的顺序累加权值。

  • 思路:

         这题考虑的是每条边对答案的贡献。         考虑顶点有n个的情况:         假设顶点x和顶点y有边相连,那么在全排列中这条边会被访问几次?

  • 固定x,对其余所有顶点进行全排列,得到(n-1)!种情况。
  • 固定y,对其余所有顶点进行全排列,得到(n-1)!种情况。

         显而易见,每条边在全排列中贡献了2*(n-1)!次,再乘上每2个点的最小路径dis[x][y],就是每条边对答案的贡献,即2*(n-1)!*dis[x][y]         任意两点距离最短路径参考HDU5723  HDU2376。代码:

#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
#define pi acos(-1.0)
#define Max_N 1001
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9+;
LL dp[];
int sum[];
int n;
vector<pair<int,LL> >v[];
LL f[];
void dfs(int now,int father){
sum[now] = ;
for(int i = ; i < v[now].size() ; i++){
int son = v[now][i].first;
LL len = v[now][i].second;
if(son == father)continue;
dfs(son,now);
sum[now] += sum[son];
dp[now] += (dp[son]+((n-sum[son])%mod*sum[son])%mod*len%mod*2LL*f[n-]%mod)%mod;
dp[now] = (dp[now]+mod)%mod;
}
}
int main()
{
f[]= ; f[] = ;
for(int i = ; i <= ; i ++){
f[i]=(f[i-]*i)%mod;
}
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
for(int i = ; i <= n ; i++)v[i].clear();
for(int i = ; i < n-; i ++){
int x,y;
LL w;
scanf("%d %d %lld",&x,&y,&w);
v[x].push_back(make_pair(y,w));
v[y].push_back(make_pair(x,w));
}
dfs(,-);
printf("%lld\n",dp[]);
}
}
/*
3
1 2 1
2 3 1
3
1 2 1
1 3 2
*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848