1106. Lowest Price in Supply Chain (25)
2016-12-10 02:15
260 查看
考前练习之深度搜索
这题就是前面有的用Dijkstra算法找出节点pre
数组,递归遍历求最优路径的部分一样
当成树处理,深度递归,最后叶子节点后继用个负数
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 100010;
vector<int> nextchildren[MAX];
int pathlength=MAX;
int num=0;
vector<int> path;
void DFS(int root){
if(root==-1){
int temppathlength;
temppathlength=path.size()-1;
if(temppathlength<pathlength){
num=1;
pathlength=temppathlength;
}else if(temppathlength==pathlength){
num++;
}
return;
}
path.push_back(root);
for(int i=0;i<nextchildren[root].size();i++){
DFS(nextchildren[root][i]);
}
path.pop_back();
}
int main(){
int n;
double p,rate;
scanf("%d%lf%lf",&n,&p,&rate);
for(int i=0;i<n;i++){
int numnext;
scanf("%d",&numnext);
if(numnext==0){
nextchildren[i].push_back(-1);
}
else{
for(int j=0;j<numnext;j++){
int temp;
scanf("%d",&temp);
nextchildren[i].push_back(temp);
}
}
}
int root=0;
DFS(root);
//double price=pow(1+rate/100,(double)pathlength)*p;
double price=p;
for(int i=0;i<pathlength;i++){
price*=(1+rate/100);
}
printf("%.4lf %d",price,num);
}
这题就是前面有的用Dijkstra算法找出节点pre
数组,递归遍历求最优路径的部分一样
当成树处理,深度递归,最后叶子节点后继用个负数
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 100010;
vector<int> nextchildren[MAX];
int pathlength=MAX;
int num=0;
vector<int> path;
void DFS(int root){
if(root==-1){
int temppathlength;
temppathlength=path.size()-1;
if(temppathlength<pathlength){
num=1;
pathlength=temppathlength;
}else if(temppathlength==pathlength){
num++;
}
return;
}
path.push_back(root);
for(int i=0;i<nextchildren[root].size();i++){
DFS(nextchildren[root][i]);
}
path.pop_back();
}
int main(){
int n;
double p,rate;
scanf("%d%lf%lf",&n,&p,&rate);
for(int i=0;i<n;i++){
int numnext;
scanf("%d",&numnext);
if(numnext==0){
nextchildren[i].push_back(-1);
}
else{
for(int j=0;j<numnext;j++){
int temp;
scanf("%d",&temp);
nextchildren[i].push_back(temp);
}
}
}
int root=0;
DFS(root);
//double price=pow(1+rate/100,(double)pathlength)*p;
double price=p;
for(int i=0;i<pathlength;i++){
price*=(1+rate/100);
}
printf("%.4lf %d",price,num);
}
相关文章推荐
- SCOM2012功能测试(25)—发布报表和收藏报表
- PAT (Advanced Level) Practise 1113 Integer Set Partition (25)
- PTA甲 1101. Quick Sort (25)
- 【每天一个Linux命令】25. 查看文件命令(cat/more/less/tail/tac/nl/od)
- 3月全球域名商新增注册量25强:万网夺冠 增速加快
- 我的ACM之路 7-25(字符串)
- 1024. Palindromic Number (25)
- 1040. 有几个PAT(25)
- PAT乙级 1025. 反转链表 (25)--结构体、容器vector、容器pair
- 1052. Linked List Sorting (25)
- LeetCode(25)-symmetric tree
- 1005. 继续(3n+1)猜想 (25)
- 《Effective C++》item25:考虑写出一个不抛异常的swap函数
- PAT1025反转链表 (25)
- 25 JS基础之--5个数组元素遍历的方法
- ENGLISH资料收集(25)-经典电影告诉我们的
- SDAU 搜索专题 25 推箱子
- SegmentFault 技术周刊 Vol.25 - Swift 开发:从入门到重新入门
- Python 学习入门(25)—— 闭包
- 1009. Product of Polynomials (25)