zoukankan      html  css  js  c++  java
  • PTA

    一个项目由若干个任务组成,任务之间有先后依赖顺序。项目经理需要设置一系列里程碑,在每个里程碑节点处检查任务的完成情况,并启动后续的任务。现给定一个项目中各个任务之间的关系,请你计算出这个项目的最早完工时间。

    输入格式:

    首先第一行给出两个正整数:项目里程碑的数量 N(100)和任务总数 M。这里的里程碑从 0 到 N1 编号。随后 M 行,每行给出一项任务的描述,格式为“任务起始里程碑 任务结束里程碑 工作时长”,三个数字均为非负整数,以空格分隔。

    输出格式:

    如果整个项目的安排是合理可行的,在一行中输出最早完工时间;否则输出"Impossible"。

    输入样例 1:

    9 12
    0 1 6
    0 2 4
    0 3 5
    1 4 1
    2 4 1
    3 5 2
    5 4 0
    4 6 9
    4 7 7
    5 7 4
    6 8 2
    7 8 4
    

    输出样例 1:

    18
    

    输入样例 2:

    4 5
    0 1 1
    0 2 2
    2 1 3
    1 3 4
    3 2 5
    

    输出样例 2:

    Impossible

    题意 : 问你最短工期是多少,那么想想这个最短工期要怎么得到,这些全部的任务组成一个项目,那么就是需要我们去完成全部的任务,并且需要只有当前任务完成后才能去开始下一个任务。
    那么我们就可以把完成任务所需最多的时间当成最短的时间即可。
    代码示例 :
    #define ll long long
    const int maxn = 1e6+5;
    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    
    int n, m;
    struct node
    {
        int to, cost;
        node(int _to=0, int _cost=0):to(_to), cost(_cost){}
    };
    vector<node>ve[105];
    int deg[105], ans[105];
    queue<int>que;
    int cnt = 0, an = 0;
    
    void fun(){
        while(!que.empty()){
            int v = que.front();
            que.pop();
            cnt++;
            
            for(int i = 0; i < ve[v].size(); i++){
                int to = ve[v][i].to;
                int cost = ve[v][i].cost;
                deg[to]--;
                if (!deg[to]) que.push(to);
                ans[to] = max(ans[to], ans[v]+cost);
                an = max(an, ans[to]);
            }
        }
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        int x, y, c;
        
        cin >> n >> m;
        for(int i = 1; i <= m; i++){
            scanf("%d%d%d", &x, &y, &c);
            ve[x].push_back(node(y, c));    
            deg[y]++;
        }
        for(int i = 0; i < n; i++) {
            if (!deg[i]) que.push(i);
        }
        fun();
        if (cnt != n) {
            printf("Impossible
    ");
        }
        else printf("%d
    ", an);
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    洛谷P4550 收集邮票 期望dp
    Codeforces Round #748 (Div. 3) G. Changing Brackets
    [Codeforces Round #748 (Div. 3)](https://codeforces.com/contest/1593) F. Red-Black Number 记忆化搜索
    [Codeforces Round #748 (Div. 3)](https://codeforces.com/contest/1593) D2 Half of Same
    HDU 3746 Cyclic Nacklace kmp找循环节
    Codeforces Round #747 (Div.2) D. The Number of Imposters
    Nand2tetris 学习笔记
    怎样解题表
    [省选]知识点板块
    List of Problems to be Solved
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8657182.html
Copyright © 2011-2022 走看看