天梯赛 L2 功夫传人

题目链接:点击打开链接

思路:广搜,从祖师爷开始搜索,搜到得道者加上其功力值即可。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int n,add[100000];
double total,rate,mul,re;
vector<int> v[100000];
void bfs(int r){
	re = 0.0;
	queue<pair<int,double> > q;
	q.push(make_pair(r,total));
	while(!q.empty()){
		pair<int,double> t = q.front();
		q.pop();
		if(add[t.first] != 0){
			re += t.second * add[t.first];
		}
		for(int i = 0;i < v[t.first].size();i++){
			q.push(make_pair(v[t.first][i],t.second * mul));
		}
	}
}
int main(){
	scanf("%d%lf%lf",&n,&total,&rate);
	mul = 1.0 - rate / 100.0;
	memset(add,0,sizeof(add));
	for(int i = 0;i < n;i++){
		int k;
		scanf("%d",&k);
		if(k == 0){
			scanf("%d",&add[i]);
		}
		else{
			int t;
			for(int j = 1;j <= k;j++){
				scanf("%d",&t);
				v[i].push_back(t);
			}
		}
	}
	bfs(0);
	printf("%d\n",(int)re);
	return 0;
}

edition 2:

#include <bits/stdc++.h>
using namespace std;
int n;
double total,rate,re;
vector<int> v[100010];
int book[100010];
struct node{
	int to;
	double sum;
	node(int a,double b):to(a),sum(b){} 
};
void bfs(){
	queue<node> q;
	q.push(node(0,total));
	while(!q.empty()){
		node t = q.front();
		q.pop();
		for(int i = 0;i < v[t.to].size();i++){
			if(book[v[t.to][i]] != 0){
				re += (double)book[v[t.to][i]] * t.sum * rate;
				q.push(node(v[t.to][i],(double)book[v[t.to][i]] * t.sum * rate));
				continue;
			}
			q.push(node(v[t.to][i],t.sum * rate));
		}
	}
}
int main(){
	scanf("%d%lf%lf",&n,&total,&rate);
	for(int i = 0;i < n;i++){
		v[i].clear();
		book[i] = 0;
	}
	re = 0.0;
	rate = 1.0 - rate / 100.0;
	for(int i = 0;i < n;i++){
		int k,son;
		scanf("%d",&k);
		if(k == 0){
			scanf("%d",&book[i]);
			continue;
		}
		for(int j = 0;j < k;j++){
			scanf("%d",&son);
			v[i].push_back(son);
		}
	}
	bfs();
	if(n == 1){
		re += total * book[0];
	}
	printf("%d\n",(int)floor(re));
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值