您的位置:首页 > 大数据 > 人工智能

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT DFS