赞
踩
题目 A1079 Total Sales of Supply Chain
- #include<bits/stdc++.h>
- using namespace std;
- int N;
- double P,R;
- double res = 0;
- struct node{
- double data;//货物量
- vector<int> child;
- }Node[100110];
- void DFS(int index,int depth)
- {
- //递归边界
- if(Node[index].child.size() == 0)
- {
- res += Node[index].data*pow(1+R,depth);
- return;
- }
- //递归式
- for(int i=0;i<Node[index].child.size();i++)
- {
- DFS(Node[index].child[i],depth+1);
- }
- }
-
- void BFS(int index)
- {
- queue<int> q;
- q.push(index);
- int height = 0;
- while(!q.empty())
- {
-
- int n = q.size();
- for(int i=0;i<n;i++)
- {
- int temp = q.front();
- q.pop();
- if(Node[temp].child.size()==0)
- {
- res += Node[temp].data*pow(1+R,height);
- }
- for(int i=0;i<Node[temp].child.size();i++)
- {
- q.push(Node[temp].child[i]);
- }
-
- }
- height++;
-
- }
- }
- int main()
- {
- cin>>N>>P>>R;
- R/=100;
- for(int i=0;i<N;i++)
- {
- int num;
- cin>>num;
- if(num==0)
- {
- cin>>Node[i].data;
- }
- else
- {
- for(int j=0;j<num;j++)
- {
- int x;
- cin>>x;
- Node[i].child.push_back(x);
- }
- }
-
- }
- BFS(0);
- printf("%.1lf",P*res);
- return 0;
- }
A1090 Highest Price in Supply Chain (25分)
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn = 100010;
- vector<int> child[maxn];
-
-
- int n;
- double p,r;
- double res = 0;
- int maxDepth = 0,num=0;
-
- void DFS(int index,int depth)
- {
- if(child[index].size()==0)
- {
- if(depth>maxDepth)
- {
- maxDepth = depth;
- num = 1;
- }
- else if(depth == maxDepth)
- {
- num++;
- }
- return;
- }
- for(int i=0;i<child[index].size();i++)
- {
- DFS(child[index][i],depth+1);
- }
- }
-
- void BFS(int index)
- {
- queue<int> q;
- q.push(index);
- while(!q.empty())
- {
-
- int n = q.size();
- //每一次for循环就是层序遍历的一行
- for(int i=0;i<n;i++)
- {
- int temp = q.front();
- q.pop();
- for(int j=0;j<child[temp].size();j++)
- {
- q.push(child[temp][j]);
- }
- }
- maxDepth++;
- num = n;
- }
- maxDepth = maxDepth - 1;
- }
- int main()
- {
- cin>>n>>p>>r;
- r/=100;
- int x,root;
- for(int i=0;i<n;i++)
- {
- cin>>x;
- if(x==-1)
- {
- root = i;
- }
- else
- {
- child[x].push_back(i);
- }
-
- }
- BFS(root);
- printf("%.2lf %d\n",p*pow(1+r,maxDepth),num);
- return 0;
- }
A1094 The Largest Generation (25分)
- #include<bits/stdc++.h>
- using namespace std;
-
-
- const int maxn = 101;
- int hashTable[maxn] = {0};
- int n,m;
- vector<int> child[maxn];
-
-
- void DFS(int index,int level)
- {
- hashTable[level]++;
- for(int i=0;i<child[index].size();i++)
- {
- DFS(child[index][i],level+1);
- }
- }
-
- void BFS(int index,int level)
- {
- queue<int> q;
- q.push(index);
- while(!q.empty())
- {
- int nsize = q.size();
- //每一次for循环就代表是层序遍历的一行
- for(int i=0;i<nsize;i++)
- {
- int temp = q.front();
- hashTable[level]++;
- q.pop();
- for(int j=0;j<child[temp].size();j++)
- {
- q.push(child[temp][j]);
- }
-
- }
- level++;
-
- }
- }
-
- int main()
- {
- cin>>n>>m;
- for(int i=0;i<m;i++)
- {
- int id,k;
- cin>>id>>k;
- for(int j=0;j<k;j++)
- {
- int x;
- cin>>x;
- child[id].push_back(x);
- }
- }
- BFS(1,1);
- int maxLevel = -1,maxNum = 0;
- for(int i=1;i<maxn;i++)
- {
- if(hashTable[i]>maxNum)
- {
- maxNum = hashTable[i];
- maxLevel = i;
- }
- }
- cout<<maxNum<<" "<<maxLevel<<endl;
- return 0;
- }
A1106 Lowest Price in Supply Chain (25分)
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- vector<int> child;
- };
- int n;
- double p,r;
- vector<node> v;
- int num=0,minDepth=INT_MAX;
- void DFS(int index,int depth)
- {
- if(v[index].child.size()==0) //到达叶子结点
- {
- if(depth<minDepth)
- {
- minDepth = depth;
- num = 1;
- }
- else if(depth==minDepth)
- {
- num++;
- }
- return;
- }
- for(int i=0; i < v[index].child.size(); i++)
- {
- DFS(v[index].child[i],depth+1); //递归访问子节点
- }
- }
- int main()
- {
- int k,child;
- cin>>n>>p>>r;
- v.resize(n);
- r /= 100;
- for(int i=0;i<n;i++)
- {
- cin>>k;
- if(k!=0) //不是叶结点
- {
- for(int j=0;j<k;j++)
- {
- int x;
- cin>>x;
- v[i].child.push_back(x);
- }
- }
-
- }
- DFS(0,0); //DFS入口
- printf("%.4f %d\n", p*pow(1+r,minDepth),num);
- return 0;
- }
A1004 Counting Leaves (30分)
- #include<bits/stdc++.h>
- using namespace std;
-
-
- const int maxn = 101;
- int hashTable[maxn] = {0};
- int n,m;
- int level = 1;
- vector<int> child[maxn];
-
-
-
- void DFS(int index,int depth)
- {
- if(child[index].size()==0)
- {
- hashTable[depth]++;
- level = max(depth,level);
- return;
- }
- for(int i=0;i<child[index].size();i++)
- {
- DFS(child[index][i],depth+1);
- }
- }
-
- void BFS(int index)
- {
- queue<int> q;
- q.push(index);
- while(!q.empty())
- {
- int nsize = q.size();
- for(int i=0;i<nsize;i++)
- {
- int temp = q.front();
- if(child[temp].size()==0)
- {
- hashTable[level]++;
- }
- q.pop();
- for(int j=0;j<child[temp].size();j++)
- {
- q.push(child[temp][j]);
- }
-
- }
- level++;
-
- }
- level-=1;//最后会多加一行删掉
- }
-
- int main()
- {
- cin>>n>>m;
- for(int i=0;i<m;i++)
- {
- int id,k;
- cin>>id>>k;
- for(int j=0;j<k;j++)
- {
- int x;
- cin>>x;
- child[id].push_back(x);
- }
- }
- // DFS(1,1);
- BFS(1);
- int maxLevel = -1,maxNum = 0;
- for(int i=1;i<=level;i++)
- {
-
- cout<<hashTable[i];
- if(i!=level)
- cout<<" ";
- }
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。