hdu 4109 Instrction Arrangement【DAG图上的最长路】

Instrction Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1570    Accepted Submission(s): 660

Problem Description

Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW.
If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance.
The definition of the distance between two instructions is the difference between their beginning times.
Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction.
Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.

Input

The input consists several testcases.
The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.
The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1.

Output

Print one integer, the minimum time the CPU needs to run.

Sample Input

5 2

1 2 1

3 4 1

Sample Output

2

Hint

 

In the 1st ns, instruction 0, 1 and 3 are executed;

In the 2nd ns, instruction 2 and 4 are executed.

So the answer should be 2.

Source

2011 Alibaba-Cup Campus Contest

 

 题目大意:给你n个任务,m个限制条件,m个限制条件里边,表示开启任务y的时候,要在x任务结束后z时间开启,开启第一个任务的时候使用1个单位时间,问完成所有任务至多需要多长时间。


思路:明显的全序关系,那么不难想到解题需要用到拓扑排序过程,既然需要使用拓扑排序,那么这个题一定要满足DAG图0.0,既然满足了DAG图,还要再这个图里边求最多需要多长时间,我们其实就可以把题目转化到在DAG图上求最长路的问题了。


在DAG图上求最长路有这样一个特性:按照其图的拓扑排序得到的顺序对每个点进行松弛操作,我们就能得到DAG图上的最长路。


所以这个题我们直接规划到DAG图上求最长路即可。tuopu+松弛、


ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[100000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[100000];
int n,m;
int dis[100000];
int degree[100000];
int vis[100000];
int ans[100000];
int cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void top()
{
    int tot=0;
    queue<int >s;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dis[i]=-0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        if(degree[i]==0)
        {
            s.push(i);
            vis[i]=1;
            dis[i]=0;
            ans[tot++]=i;
        }
    }
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            degree[v]--;
            if(degree[v]==0)
            {
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                    ans[tot++]=v;
                }
            }
        }
    }
    for(int i=0;i<tot;i++)
    {
        int u=ans[i];
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            int w=e[k].w;
            if(dis[v]<dis[u]+w)
            {
                dis[v]=dis[u]+w;
            }
        }
    }
    int out=0;
    for(int i=1;i<=n;i++)
    {
        out=max(out,dis[i]);
    }
    printf("%d\n",out+1);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cont=0;
        memset(head,-1,sizeof(head));
        memset(degree,0,sizeof(degree));
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            x++;y++;
            add(x,y,w);
            degree[y]++;
        }
        top();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值