PAT (Advanced Level) 1079. Total Sales of Supply Chain (25) 总叠加售价 BFS

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence their ID's are numbered from 0 to N-1, and the root supplier's ID is 0); P, the unit price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki ID[1] ID[2] ... ID[Ki]

where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID's of these distributors or retailers. Kj being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 1010.

Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
Sample Output:
42.4
建立树后,进行BFS。
/*2015.7.29cyq*/
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
using namespace std;

//ifstream fin("case1.txt");
//#define cin fin

struct node{
	int val;//产品数量
	vector<int> next;
};

int main(){
	int N;
	double P,r;
	cin>>N>>P>>r;
	vector<node> a(N);
	int buyer,m,x;
	for(int i=0;i<N;i++){
		cin>>buyer;
		if(buyer==0){
			cin>>a[i].val;
		}else{
			a[i].val=0;
			while(buyer--){
				cin>>x;
				a[i].next.push_back(x);
			}
		}
	}

	queue<int> cur,next;
	cur.push(0);
	double sum=0;
	double price=P;//当前售价
	while(!cur.empty()){
		while(!cur.empty()){
			int tmp=cur.front();
			cur.pop();
			if(a[tmp].val>0)//零售商
				sum+=price*a[tmp].val;
			else{
				for(auto it=a[tmp].next.begin();it!=a[tmp].next.end();it++)
					next.push(*it);
			}
		}
		swap(cur,next);
		price=price*(r/100+1);//下一层售价
	}
	printf("%.1f",sum);
	return 0;
}


好的,以下是一个基于C++的BFS求最短路径的示例代码: ```cpp #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 1005; // 最大节点数 const int INF = 0x3f3f3f3f; // 无穷大 int n, m; // n表示节点个数,m表示边数 int dis[MAXN]; // 存储起点到每个节点的距离 bool vis[MAXN]; // 记录每个节点是否被访问过 int head[MAXN], to[MAXN<<1], nxt[MAXN<<1], edge[MAXN<<1], cnt; // 邻接表存图 void addEdge(int u, int v, int w) { to[++cnt] = v; nxt[cnt] = head[u]; edge[cnt] = w; head[u] = cnt; } void bfs(int start) { memset(dis, INF, sizeof(dis)); // 初始化距离为无穷大 memset(vis, false, sizeof(vis)); // 初始化所有节点都未被访问过 dis[start] = 0; // 起点到自己的距离为0 vis[start] = true; // 起点已经访问过了 queue<int> q; // 定义一个队列,用于广度优先搜索 q.push(start); // 将起点加入队列 while (!q.empty()) { int u = q.front(); q.pop(); // 取出队首节点 for (int i = head[u]; i; i = nxt[i]) { int v = to[i], w = edge[i]; if (!vis[v]) { // 如果这个节点未被访问过 dis[v] = dis[u] + w; // 更新距离 vis[v] = true; // 标记为已访问 q.push(v); // 将其加入队列 } } } } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; addEdge(u, v, w); addEdge(v, u, w); // 无向图需要加两条边 } int start; cin >> start; bfs(start); for (int i = 1; i <= n; ++i) { if (dis[i] != INF) { cout << start << " 到 " << i << " 的最短距离为:" << dis[i] << endl; } } return 0; } ``` 这段代码使用邻接表存图,时间复杂度为O(n+m),其中n表示节点数,m表示边数。在实际使用中,可以根据自己的需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值