HDU - 6446 Tree and Permutation (树形dp)

这篇博客探讨了如何使用树形动态规划(DP)解决一个与树上的最短路径相关的问题。输入包含一个树的结构信息,要求计算所有可能的顶点排列下,从第一个顶点到最后一个顶点的最短路径之和。通过树形DP的方法,可以高效地计算出答案,并且给出了具体的C++代码实现。
摘要由CSDN通过智能技术生成

There are NN vertices connected by N−1N−1 edges, each edge has its own length. 
The set { 1,2,3,…,N1,2,3,…,N } contains a total of N!N! unique permutations, let’s say the ii-th permutation is PiPi and Pi,jPi,j is its jj-th number. 
For the ii-th permutation, it can be a traverse sequence of the tree with NN vertices, which means we can go from the Pi,1Pi,1-th vertex to the Pi,2Pi,2-th vertex by the shortest path, then go to the Pi,3Pi,3-th vertex ( also by the shortest path ) , and so on. Finally we’ll reach the Pi,NPi,N-th vertex, let’s define the total distance of this route as D(Pi)D(Pi) , so please calculate the sum of D(Pi)D(Pi) for all N!N! permutations.

Input

There are 10 test cases at most. 
The first line of each test case contains one integer NN ( 1≤N≤1051≤N≤105 ) . 
For the next N−1N−1 lines, each line contains three integer XX, YY and LL, which means there is an edge between XX-th vertex and YY-th of length LL ( 1≤X,Y≤N,1≤L≤1091≤X,Y≤N,1≤L≤109 ) .

Output

For each test case, print the answer module 109+7109+7 in one line.

Sample Input

3
1 2 1
2 3 1
3
1 2 1
1 3 2

Sample Output

16
24

找规律。发现答案 = 树上任意两点间的距离和 * 2 * (n - 1) !

树形dp:https://www.cnblogs.com/flyuz/p/9547103.html

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef unsigned long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

ll dp[N], sum[N], n, u, v, w, f[N];

struct Edge {
    ll v, w;
    Edge(ll _v = 0, ll _w = 0) {
        v = _v;
        w = _w;
    }
};

vector<Edge>tree[N];

void dfs(ll cur, ll father) {
    sum[cur] = 1;
    for(ll i = 0; i < tree[cur].size(); ++i) {
        ll son = tree[cur][i].v % mod;
        ll len = tree[cur][i].w % mod;
        if(father == son)
            continue;
        dfs(son, cur);
        sum[cur] = (sum[cur] + sum[son]) % mod;
        dp[cur] = (dp[cur] % mod + dp[son] % mod) % mod;
        ll tmp = ((n - sum[son] + mod) % mod) * (sum[son] % mod) % mod;
        tmp = tmp * len % mod;
        dp[cur] = (dp[cur] + tmp) % mod;
    }
}

void init() {
    f[0] = 1;
    for(int i = 1; i < N; ++i) {
        f[i] = f[i - 1] * i % mod;
    }
}

int main() {
    init();
    while(~scanf("%lld", &n)) {
        memset(tree, 0, sizeof(tree));
        for(ll i = 1; i <= n; ++i) dp[i] = 0;
        for(ll i = 1; i < n; ++i) {
            scanf("%lld%lld%lld", &u, &v, &w);
            tree[u].push_back(Edge(v, w));
            tree[v].push_back(Edge(u, w));
        }
        dfs(1, -1);
        ll ans = dp[1] * 2 * f[n - 1] % mod;
        printf("%lld\n", ans);
    }
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值