浙大 PAT 甲级 1079 Total Sales of Supply Chain 最简单的深度优先搜索 DFS
作者:互联网
这几乎是PAT甲级练习题中最简单的一道DFS了…无需赘述,贴上代码供参考就好了。
#include<stdio.h>
#include<vector>
#include<map>
#include<math.h>
using namespace std;
vector<int> edge[100000];
map<int, int> amount;
double P, r, sales = 0.0;
void DFS(int layer,int x)
{
if (edge[x].empty() == true)
{
sales += pow(r, layer)*P*amount[x];
}
else
{
for (int i = 0; i < edge[x].size(); i++)
{
DFS(layer + 1, edge[x][i]);
}
}
}
int main()
{
int N;
scanf("%d%lf%lf", &N, &P, &r);
r = 1 + r / 100;
for (int i = 0; i < N; i++)
{
int k;
scanf("%d",&k);
if (k == 0)
{
int tmp;
scanf("%d", &tmp);
amount[i] = tmp;
}
else
{
for (int j = 0; j < k; j++)
{
int tmp;
scanf("%d", &tmp);
edge[i].push_back(tmp);
}
}
}
DFS(0, 0);
printf("%.1f", round(sales * 10) / 10.0);
return 0;
}
标签:tmp,PAT,Chain,1079,int,scanf,DFS,edge,include 来源: https://blog.csdn.net/qq_39115541/article/details/100176836