资源依赖
(!!!注意:这个是自己写的代码,不知道是否能通过所有测试用例,若有问题请留言告知)
给资源的占用空间大小和依赖关系,求占用资源最大的名称和大小。
输入描述:
//总资源
5
//资源编号,大小,依赖资源编号
1001,5
1002,10
1000,3,1001,1002
2000,7
3000,2
输出描述:
1000,18
#include <bits/stdc++.h>
using namespace std;
int main()
{
freopen("record.txt", "r", stdin);//输入调试,正式使用要注释掉
int n = 0;
cin >> n;
unordered_map<int, int> resource;
unordered_map<int, vector<int>> depend;
for(int i = 0; i < n; ++i){
string str;
//getline(cin, str);
cin >> str;
istringstream tempstr(str);
vector<int> num;
int inter; //中间变量,存放有效值
char ch; //存放用于分割的逗号
while (tempstr >> inter)
{
num.push_back(inter);
tempstr >> ch;
}
resource[num[0]] = num[1];
int len = num.size();
for(int k = 2; k < len; ++k){
depend[num[0]].push_back(num[k]);
}
}
for(auto it = depend.begin(); it != depend.end(); ++it){
for(int dep : it->second){
resource[it->first] += resource[dep];
}
}
int id = 0, cost = -1;
for(auto it = resource.begin(); it != resource.end(); ++it){
if(it->second > cost){
cost = it->second;
id = it->first;
}
}
cout << id << "," << cost << endl;
return 0;
}
本文介绍了一种算法,用于解决资源管理问题,通过输入资源的占用空间和相互依赖关系,找出占用资源最大的项及其占用空间。代码实现了一个程序来遍历和计算,最后输出占用空间最大的资源名称和大小。

588

被折叠的 条评论
为什么被折叠?



