L2-020 功夫传人 - java

题目解析

给定n个人的徒弟或当前这位得道者武功被放大的倍数。
求所有得道者的功力总值。

解题思路

通过搜索建立师门所有人的关系

然后判断当前这人是否为得道者

  • 是得道者,则计算他们功力值总和
  • 不是得道者, 则递归他的弟子

注: java不知道为什么dfs会出现返回非零,bfs不会

代码

import java.io.*;
import java.util.*;

public class Main
{
	static int N = (int) 1e5 + 10;
	static double shu[] = new double[N];
	static ArrayList<Integer> mp[] = new ArrayList[N + 10];

	static void bfs(double z, double r)
	{
		LinkedList<edge> li = new LinkedList<edge>();
		li.add(new edge(0, z));

		double ans = 0;
		while (li.size() > 0)
		{
			edge t = li.poll();
			int id = t.id;
			z = t.z;
			if (mp[id].size() == 0)
				ans += z * shu[id];

			for (int i : mp[id])
				li.add(new edge(i, z * (1 - r / 100)));
		}
		out.println((int) (ans));
	}

	public static void main(String[] args) throws IOException
	{
		int n = sc.nextInt();
		double z = sc.nextDouble();
		double r = sc.nextDouble();

		for (int i = 0; i < n; i++)
		{
			mp[i] = new ArrayList<Integer>();

			int k = sc.nextInt();
			if (k == 0)
				shu[i] = sc.nextDouble();
			else
			{
				while (k-- > 0)
				{
					int id = sc.nextInt();
					mp[i].add(id);
				}
			}
		}

		bfs(z, r);

		out.flush();
		out.close();
	}

	static class edge
	{
		int id;
		double z;

		public edge(int id, double z)
		{
			this.id = id;
			this.z = z;
		}
	}

	static InputReader sc = new InputReader();
	static PrintWriter out = new PrintWriter(System.out);

	static class InputReader
	{
		private InputStream inputStream = System.in;
		private byte[] buf = new byte[100000];
		private int curChar;
		private int numChars;

		public int getchar()
		{
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = inputStream.read(buf);
				} catch (IOException e)
				{
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt()
		{
			int a = 0, b = 1;
			int c = getchar();
			while (c < '0' || c > '9')
			{
				if (c == '-')
				{
					b = -1;
				}
				c = getchar();
			}
			while (c >= '0' && c <= '9')
			{
				a = (a << 1) + (a << 3) + (c ^ 48);
				c = getchar();
			}
			return a * b;
		}

		public double nextDouble()
		{
			int c = getchar();
			while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1)
				c = getchar();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = getchar();
			}
			double res = 0;
			while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = getchar();
			}
			if (c == '.')
			{
				c = getchar();
				double m = 1;
				while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = getchar();
				}
			}
			return res * sgn;
		}

	}

}

ArrayList
ArrayList

LinkedList

arraylist和linkedlist的区别
arraylist和linkedlist的区别

dfs
dfs

bfs
bfs


团体程序设计天梯赛-练习集-java

赞赏