HOJ 3282 skyscraper(拓扑排序)

Skyscraper
Time limit : 1 s    Memory limit : 512 mb

Problem Description


Building a skyscraper is a huge task. It consists of n subtasks, numbered from 1, 2, …, n (n <1000).


 


There are some restrictions on subtasks, for example, after pouring concrete, you have to wait some time before removing the baffle plate. Such constraint could be represented by m (m<1000)inequalitiesTi+b ≤ Tj which means that subtask j has to wait for time b after subtask i starts. b is a constant different for each inequality, but they are ranged from to 100, inclusively.


 

Your task is to write a program to determine whether all the constraints could be satisfied simultaneously. If the answer is “yes”. Find a solution that the beginning time of each subtask is the earliest. If the answer is “no”, simply output “NO SOLUTION” without quote. Note that the earliest time must satisfy that one of the subtask has to start at time 0.

Input


There are multiple test cases. 

For each test case:

The first line contains nm.

The next m lines, each line contains three integers ijb, corresponding to the inequalities in the description. 

Output


For each test case:

If there is a valid plan, output n lines, each line represents the earliest start time of each subtask. 

Otherwise output “NO SOLUTION” without quote.


Sample Input

5 6
l 2 0
1 5 1
2 5 1
3 1 5
4 1 4
4 3 1
5 5
1 2 3
1 5 1
2 5 1
5 1 5
4 1 4

Sample Output

6
6
1
0
7
NO SOLUTION

题目大意:一个人盖楼,分成了很多小问题,不同的小问题之间有做的先后关系,只有在完成上一个小问题,并且隔一段给定时间后,才能实现下一个问题,问是否可以把所有的问题完成,如果可以的话,问完成每个任务的时刻是多少

心路历程:完成任务的逻辑关系,很明显的拓扑排序,可以再开一个times[ ]来记录完成每一个问题的时间,每次入度减一后判断,且保存最大时间。
注意:两个任务可能有多个关系(助教心机boy),时间可能是0,所以初始化为-1

ac代码:
//by NOVA

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

int mat[1001][1001],deg[1001],times[1001],n,m,sum;//times保存时间

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        int i,a,b,c;
        memset(deg,0,sizeof(deg));
        memset(mat,-1,sizeof(mat));
        memset(times,0,sizeof(times));
        sum=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(mat[a][b]==-1)
            {
                mat[a][b]=c;
                deg[b]++;//注意对于每两个任务之间只能有一个入度,所以要预处理
            }
            else
            {
                mat[a][b]=max(mat[a][b],c);
            }

        }
        queue<int> q;
        for(i=1;i<=n;i++)
        {
            if(deg[i]==0)
            {
                q.push(i);
            }
        }
        while(!q.empty())
        {
            int cur=q.front();
            q.pop();
            sum++;
            for(i=1;i<=n;i++)
            {
                if(mat[cur][i]!=-1)
                {
                    deg[i]--;
                    times[i]=max(times[i],times[cur]+mat[cur][i]);//保存最大时间
                    if(deg[i]==0)
                    {
                        q.push(i);
                    }
                }
            }
        }
        if(sum!=n)
        {
            printf("NO SOLUTION\n");
        }
        else
        {
            for(i=1;i<=n;i++)
            {
                printf("%d\n",times[i]);
            }
        }
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值