CCF CSP202212-2训练计划【100分】详细注释

该代码实现了一个程序,用于处理项目间的依赖关系,计算每个项目在满足所有依赖条件下的最早开始时间和最晚开始时间。它首先读取项目及其依赖项和持续时间,然后计算最早开始时间。如果所有项目的最早开始时间都符合设定的总天数限制,则进一步计算最晚开始时间。
摘要由CSDN通过智能技术生成

满足输出最晚开始时间的三个样例:

10 5

0 1 0 3 3

1 2 3 2 2

1 2 1 4 4

8 9 6 9 9

10 5

0 1 0 3 1

1 2 1 4 4

1 2 1 2 2

6 9 6 7 7

10 5

0 1 2 0 3

1 2 3 2 2

1 2 4 1 7

3 4 6 9 9

#include<iostream> 
#include<cmath>
#include<map>
#include<climits>
using namespace std;
const int maxn = 365+5;
const int maxm = 100+5;
int p[maxm];
int t[maxm];
int early[maxm];
int last[maxm];
typedef multimap<int,int>::iterator Iterator;
int main(){
    multimap<int,int> rel;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++) {
        cin>>p[i];
        /*
        (p[i],i)表示项目i的依赖项目为p[i],称i是依赖对象,p[i]是被依赖对象
        依赖关系可以用multiset表示 
        */
        rel.insert(make_pair(p[i],i)); 
    }
    for(int i=1;i<=m;i++){
        cin>>t[i];
    }
    bool flag = true;//默认最早开始时间满足n天训练 
    for(int i=1;i<=m;i++){
        if(p[i]==0){
            early[i] = 1;
        }else{
            early[i] = early[p[i]]+t[p[i]]; 
        }
        if(early[i]+t[i]-1>n) flag = false;//最早开始时间不满足n天训练 
    }
    for(int i=1;i<=m;i++){
        cout<<early[i]<<" ";
    }
    cout<<endl;
    if(flag==true){
        /*
        由于满足依赖科目编号p[i]小于当前项目i的编号,因此可以从编号大的开始往前计算最晚开始时间 
        */
        for(int i=m;i>=1;i--){
            //找项目i的被依赖对象,如果项目i没有被依赖对象,则依赖关系(i,p[i])不存在 
            pair<Iterator,Iterator> it = rel.equal_range(i);
            if(it.first==it.second){//依赖关系不存在 
                last[i] = n - t[i] +1; 
            }else{//依赖关系存在 
                Iterator it1;
                int value = INT_MAX;
                for(it1 = it.first;it1 != it.second; ++it1) {
//                    cout<<it1->first<<" ==== "<<it1->second<<"---";
                    value = min(value,last[it1->second]);
//                    cout<<value<<endl;
                }
                last[i] = value - t[i];
            }
//            cout<<last[i]<<" ";
        }    
        for(int i=1;i<=m;i++){
            cout<<last[i]<<" ";
        }
    }
    return 0;
}
  • 12
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值