NOI模拟(5.8) HNOID2T3 道路 (bzoj5290)

道路

题目背景:

5.8 模拟 HNOI2018D2T3

分析:记忆化搜索

 

没什么难度的裸DP,直接定义dp[i][j][k]表示以i为根的子树中,i到跟有j条没有修过的公路,k条没有修过的铁路,然后如果是乡村则直接返回计算过的答案,否则

dp[i][j][k] = std::min(dp[lc[i]][j][k] + dp[rc[i]][j][k + 1]

            , dp[lc[i]][j+ 1][k], dp[rc[i]][j][k])

(lc[i]表示i的公路下的儿子,rc[i]是铁路下的)

直接记忆化搜索就好了,注意要开long long

时间复杂度O(n * 40 * 40)

 

Source:

 

/*
    created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
 
inline char read() {
    static const int IN_LEN = 1024 * 1024;
    static char buf[IN_LEN], *s, *t;
    if (s == t) {
        t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
        if (s == t) return -1;
    }
    return *s++;
}
 
///*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return ;
        if (c == '-') iosig = true; 
    }
    for (x = 0; isdigit(c); c = read()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN];
char *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}


template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout), oh = obuf;
}
 
/*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
        if (c == '-') iosig = true; 
    for (x = 0; isdigit(c); c = getchar()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/

const int MAXN = 20000 + 10;
const int MAXX = 40 + 2;

int n;
long long dp[MAXN][MAXX][MAXX];
int a[MAXN], b[MAXN], c[MAXN], lc[MAXN], rc[MAXN];

inline long long calc(int id, int x, int y) {
    return (long long)c[id] * (a[id] + x) * (b[id] + y);
}

inline long long dfs(int cur, int x, int y) {
    if (cur < 0) return calc(-cur, x, y);
    if (dp[cur][x][y] != -1) return dp[cur][x][y];
    dp[cur][x][y] = std::min(dfs(lc[cur], x, y) + dfs(rc[cur], x, y + 1)
        , dfs(lc[cur], x + 1, y) + dfs(rc[cur], x, y));
    return dp[cur][x][y];
}

inline void read_in() {
    R(n), memset(dp, -1, sizeof(dp));
    for (int i = 1; i < n; ++i) R(lc[i]), R(rc[i]);
    for (int i = 1; i <= n; ++i) R(a[i]), R(b[i]), R(c[i]);
}

int main() {
    freopen("road.in", "r", stdin);
    freopen("road.out", "w", stdout);
    read_in();
    std::cout << dfs(1, 0, 0);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用[1]和引用的描述,这是一道关于图论的问题,需要设计一个软件来计算给定的建造方案所需要的费用,或者计算在W星球上修建n-1条双向道路使得国家之间连通的方案。具体来说,对于引用,需要计算每条道路的修建费用,而对于引用,需要构建一个连通的图,使得图中任意两个节点之间都有一条路径。下面是两个问题的解答: 1. 对于引用,我们可以使用图论中的最小生成树算法来解决。最小生成树算法可以保证在连接所有节点的情况下,总的修建费用最小。常见的最小生成树算法有Prim算法和Kruskal算法。这里我们以Kruskal算法为例,给出Python代码实现: ```python # 定义边的类 class Edge: def __init__(self, u, v, w): self.u = u self.v = v self.w = w # 定义并查集类 class UnionFind: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): px, py = self.find(x), self.find(y) if px == py: return False if self.rank[px] < self.rank[py]: self.parent[px] = py elif self.rank[px] > self.rank[py]: self.parent[py] = px else: self.parent[py] = px self.rank[px] += 1 return True # Kruskal算法 def kruskal(n, edges): uf = UnionFind(n) edges.sort(key=lambda x: x.w) res = 0 for e in edges: if uf.union(e.u, e.v): res += e.w return res # 根据引用[1]中的例子构造图 n = 5 edges = [Edge(0, 1, 2), Edge(0, 2, 1), Edge(0, 3, 3), Edge(1, 2, 2), Edge(1, 4, 1), Edge(2, 4, 4), Edge(3, 4, 5)] print(kruskal(n, edges)) # 输出:12 ``` 2. 对于引用,我们可以使用随机化算法来构造一个连通的图。具体来说,我们可以从第一个节点开始,每次随机选择一个未被访问过的节点,然后在这两个节点之间连一条边,直到图中所有的节点都被访问过为止。这样构造出来的图一定是连通的,并且边的数量为n-1。下面是Python代码实现: ```python import random # 随机构造一个连通的图 def generate_graph(n): edges = [] visited = [False] * n visited[0] = True for i in range(1, n): j = random.randint(0, i - 1) edges.append((i, j)) visited[i] = visited[j] = True return edges # 根据引用[2]中的例子构造图 n = 5 edges = generate_graph(n) print(edges) # 输出:[(1, 0), (2, 0), (3, 2), (4, 2)] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值