码迷,mamicode.com
首页 > 编程语言 > 详细

(拓扑排序+DP) hdu 4109

时间:2015-04-08 12:38:18      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

Instrction Arrangement

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


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
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4103 4105 4104 4102 4106 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
int n,m,in[1010],dp[1010];
vector<int> e[1010],w[1010];
int main()
{
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int ans=0;
        for(int i=0;i<n;i++)
            e[i].clear(),in[i]=0,dp[i]=0,w[i].clear();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            e[a].push_back(b);
            w[a].push_back(c);
            in[b]++;
        }
        queue<int> q;
        for(int i=0;i<n;i++)
        {
            if(in[i]==0)
                q.push(i),dp[i]=1;
            else
                dp[i]=0;
        }
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<e[x].size();i++)
            {
                int v=e[x][i];
                dp[v]=max(dp[v],dp[x]+w[x][i]);
                if(--in[v]==0)
                {
                    q.push(v);
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            if(ans<dp[i])
                ans=dp[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

(拓扑排序+DP) hdu 4109

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4401899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!