牛客:游游的旅行(DP+DFS)

https://www.nowcoder.com/practice/03dbf07704a443cdbfe66253aa1bfe02?tpId=182&&tqId=34885&rp=1&ru=/activity/oj&qru=/ta/exam-all/question-ranking

牛客:游游的旅行(DP+DFS)_第1张图片

// 参考https://www.nowcoder.com/profile/873781650/codeBookDetail?submissionId=57839462
#include
using namespace std;
#define NUM 100+10
#define K 500
struct happy {
	double u; // 游游的愉悦度
	double v; // 小伙伴的愉悦度
	happy() : u(0), v(0) {}
	happy(double x, double y) : u(x), v(y) {}
};
int n; // 节点数目
// node_t保存各节点花费时间,happy_u游游在各节点获取的愉悦度,happy_v小伙伴在各节点获取的愉悦度
int node_t[NUM], happy_u[NUM], happy_v[NUM];
// 两节点之间的时间
int path_t[NUM][NUM];
// dp[i][j]表示在i点同时剩余j时间时,当前获取的愉悦度
happy dp[NUM][K];
happy dfs(int node, int remain_t) {
	if (dp[node][remain_t].u != 0 && dp[node][remain_t].v != 0)
		return dp[node][remain_t];
	int cnt = 0; // 记录可以到达的节点个数
	happy res;
	for (int i = 1; i <= n; i++) {
		if (i == node || path_t[node][i] == 0) continue;
		happy now;
		if (remain_t - path_t[node][i] - node_t[i] >= 0) {
			cnt += 1;
			now = dfs(i, remain_t - path_t[node][i] - node_t[i]);
			res.u += now.u;
			res.v += now.v;
		}
	}
	if (cnt == 0) cnt = 1;
	res.u /= cnt;
	res.u += happy_u[node];
	res.v /= cnt;
	res.v += happy_v[node];
	dp[node][remain_t] = res;
	return res;
}
int main()
{
	int m, k;
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) {
		cin >> node_t[i] >> happy_u[i] >> happy_v[i];
	}
	int a, b, t;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			path_t[i][j] = 0;
	for (int i = 0; i> a >> b >> t;
		path_t[a][b] = t;
		path_t[b][a] = t;
	}

	for (int i = 0; i

 

你可能感兴趣的:(#,--DFS)