1106. Lowest Price in Supply Chain (25)
来源:互联网 发布:手机新三板交易软件 编辑:程序博客网 时间:2024/06/05 17:37
PAT上一道题目。原题题意是给定供应商到零售商的供应树结构,每经过一级供应商价格都会提高百分之r,初始供应商提供的价格为p,求最终到达零售商的最低价格,及最低零售商的个数。实际上就是求到达叶子节点的最短路径。深度优先搜索可以解决,但要求正确剪枝并且记录最短路径个数,容易超时。采用广度优先反而更快更简洁,因为最短路径的叶节点肯定在同一层上,记录个数也方便。代码如下:
// 1106. Lowest Price in Supply Chain (25).cpp : 定义控制台应用程序的入口点。////#include "stdafx.h"#include<vector>#include<queue>#include<math.h>#include<stdio.h>using namespace std;int main(){ int n; double p,r; scanf("%d%lf%lf",&n,&p,&r); vector<vector<int>> AdjSupplier(n); for(int i=0;i<n;++i) { int k; scanf("%d",&k); for(int j=0;j<k;++j) { int t; scanf("%d",&t); AdjSupplier[i].push_back(t); } } int levels = 0; queue<int>Q; Q.push(0); bool flag = false; int num = 0; while(Q.empty()==false&&!flag) { queue<int>H; while(Q.empty()==false) { int t = Q.front(); Q.pop(); if(AdjSupplier[t].size()==0){ flag = true; num++; } for(int i=0;i<AdjSupplier[t].size();++i) { H.push(AdjSupplier[t][i]); } } if(flag) break; ++levels; while(H.empty()==false) { Q.push(H.front()); H.pop(); } } double ans = p*pow((1+r/100.0),double(levels)); printf("%.4lf %d\n",ans,num); return 0;}
0 0
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain(25)
- 1106. Lowest Price in Supply Chain (25)
- [按钮点击事件的几种方法]
- Python学习笔记----面向对象(类)
- JAVA与指针
- VMware中鼠标键盘偶尔失灵的原因
- 设计模式总结
- 1106. Lowest Price in Supply Chain (25)
- ZOJ 3430 detect the virus AC自动机
- PHP、JS间json数据的处理
- 年终盘点:2015年人工智能的五大关键词
- mysql修改表、字段、库的字符集
- iOS --- 使用CADisplayLink来实现定时任务
- mfc进度条与计时器关联
- 从零开始到合格的测试工程师(图)
- online_judge_1146