资源依赖

资源依赖

(!!!注意:这个是自己写的代码,不知道是否能通过所有测试用例,若有问题请留言告知)
给资源的占用空间大小和依赖关系,求占用资源最大的名称和大小。

输入描述:

//总资源
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值