1. 程式人生 > >(待補充)HDU-4109 Instrction Arrangement(拓撲排序)

(待補充)HDU-4109 Instrction Arrangement(拓撲排序)

隊友開的,我還沒整明白呢!!待補充 

Instrction Arrangement

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

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define Pi acos(-1)
#define eps 1e-8
using namespace std;
typedef long long int ll;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m,in[1005];
int sum[1005],a[1005][1005];
void topu()
{
    int pos;
    go(i,0,n-1)
    {
        go(j,0,n-1) if(!in[j]) pos=j;
        in[pos]=-1;
        go(j,0,n-1)
        {
            if(a[pos][j])
            {
                sum[j]=max(sum[j],sum[pos]+a[pos][j]);
                in[j]--;
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int x,y,z; mem(in,0);mem(a,0);
        go(i,0,n-1) sum[i]=1;
        go(i,0,m-1)
        {
            scanf("%d%d%d",&x,&y,&z);
            a[x][y]=max(z,a[x][y]);
        }
        go(i,0,n-1)
        go(j,0,n-1)
        if(a[i][j]) in[j]++;
        int ans=0;
        topu();
        go(i,0,n-1) ans=max(ans,sum[i]);
        printf("%d\n",ans);
    }
    return 0;
}