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

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

// 参考https://www.nowcoder.com/profile/873781650/codeBookDetail?submissionId=57839462
#include<iostream>
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<m; i++) {
		cin >> a >> b >> t;
		path_t[a][b] = t;
		path_t[b][a] = t;
	}

	for (int i = 0; i<NUM; i++)
		for (int j = 0; j<K; j++)
			dp[i][j].u = dp[i][j].v = 0;
	happy res;
	for (int i = 1; i <= n; i++) {
		if (k - node_t[i]<0)
			continue;
		happy now;
		now = dfs(i, k - node_t[i]);
		res.u += now.u / n;
		res.v += now.v / n;
	}
	printf("%.5lf %.5lf", res.u, res.v);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值