Instruction Arrangement

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.

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

using namespace std;

const int MAXN=1001;
const int INF=INT_MAX;

struct Edge{
    int to;
    int length;
    Edge(int t,int l):to(t),length(l){}
};

vector<Edge> graph[MAXN];//邻接表
int earliest[MAXN];//最早开始时间
int latest[MAXN];//最晚开始时间
int inDegree[MAXN];

void CriticalPath(int n){
    vector<int> topology;//拓扑序列
    queue<int> node;
    for(int i=0;i<n;i++){
        if(inDegree[i]==0){
            node.push(i);
            earliest[i]=1;//初始化为1,题目中的要求:1ns
        }
    }
    while(!node.empty()){
        int u=node.front();
        topology.push_back(u);
        node.pop();
        for(int i=0;i<graph[u].size();i++){
            int v=graph[u][i].to;
            int l=graph[u][i].length;
            earliest[v]=max(earliest[v],earliest[u]+l);//正向找最大,形成earliest
            inDegree[v]--;
            if(inDegree[v]==0) node.push(v);
        }
    }
    for(int i=topology.size()-1;i>=0;i--){
        int u=topology[i];
        if(graph[u].size()==0) latest[u]=earliest[u];//汇点的最晚开始时间初始化
        else latest[u]=INF;//非汇点的最晚开始时间的初始化
        for(int j=0;j<graph[u].size();j++){
            int v=graph[u][j].to;
            int l=graph[u][j].length;
            latest[u]=min(latest[u],latest[v]-l);//逆向找最小,形成latest
        }
    }
}

int main(){
    int n,m;
    while(cin>>n>>m){
        memset(graph,0,sizeof(graph));
        memset(earliest,0,sizeof(earliest));
        memset(latest,0,sizeof(latest));
        memset(inDegree,0,sizeof(inDegree));
        while(m--){
            int from,to,length;
            cin>>from>>to>>length;
            graph[from].push_back(Edge(to,length));
            inDegree[to]++;
        }
        CriticalPath(n);
        int answer=0;
        for(int i=0;i<n;i++){
            answer=max(answer,earliest[i]);
        }
        cout<<answer<<endl;
    }
}
发布了23 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38440882/article/details/104378774
今日推荐