天梯赛 L2 功夫传人

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccDLlyy/article/details/79808413

题目链接:点击打开链接

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

#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;
}


猜你喜欢

转载自blog.csdn.net/ccDLlyy/article/details/79808413