Instrction Arrangement//关键路径

题目:

 

Instrction Arrangement

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

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.
 
 

思路:

  关键路径问题,主要就是建一张图,每一个任务对应两个点,中间边权为1,然后再添加有限制的边(前一个任务的第二个点和后一个任务的第一个点相连,并赋权),最后加总源点,总终点(使图变成单源DAG)图就建完了。然后变成单源DAG最长路问题,权全部取负(最后结果也取负就行了),变成单源DAG最短路问题。先拓扑排序,再按拓扑顺序进行边松弛就找到最短路了。

代码:

#include <iostream>
#include <vector>
#include <stack>

using namespace std;
const int size = 2*1e3 + 5;
const int NUM = 1e3;
stack<int> order;//存拓扑排序总
bool marked[size];//辅助拓扑排序
bool in[size];//标记有入度的点
bool out[size];//标记有出度的点
int dis[size];

struct edge
{
    int from;
    int to;
    int len;
};
vector<edge> graph[size];//存图

void init(int N)
{
    //因为起点终点不连续,所以用两个循环,然后初始化终点时正好可以顺便建图
    for(int i = NUM; i < NUM+N; i++)//初始化所有起点
    {
        graph[i].clear();
        marked[i] = false;
        in[i] = false;
        out[i] = false;
        dis[i] = 0x3f3f3f3f;
    }
    for(int i = 0; i < N; i++)//初始化所有终点
    {
        graph[i].clear();
        marked[i] = false;
        in[i] = false;
        out[i] = false;
        dis[i] = 0x3f3f3f3f;
        //并建部分图
        edge c;
        c.from = i;
        c.to = i+NUM;
        c.len = -1;//取负
        graph[i].push_back(c);
        out[i] = true;//易漏!!!
        in[i+NUM] = true;//易漏!!!
    }
    for(int i = 2*NUM; i < 2*NUM+2; i++)//初始化总起点和总终点
    {
        graph[i].clear();
        marked[i] = false;
        in[i] = false;
        out[i] = false;
        dis[i] = 0x3f3f3f3f;
    }
}

void topo(int x)//用dfs进行拓扑排序
{
    marked[x] = true;
    for(int i = 0; i < graph[x].size(); i++)
    {
        if(!marked[graph[x][i].to])
            topo(graph[x][i].to);
    }
    order.push(x);
}

void sp(int x)//最短路
{
    dis[x] = 0;
    while(!order.empty())
    {
        int c = order.top();
        order.pop();
        for(int i = 0; i < graph[c].size(); i++)
        {
            if(dis[c]+graph[c][i].len < dis[graph[c][i].to])
            {
                dis[graph[c][i].to] = dis[c] + graph[c][i].len; 
            }
        }
    }
}

int main()
{
    int N, M;
    int x, y, z;
    while(~scanf("%d%d", &N, &M))
    {
        //初始化
        init(N);
        //输入
        for(int i = 0; i < M; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            edge c;
            c.from = x + NUM;
            c.to = y;
            c.len = -(z-1);//取负,因为同取开始任务的时间
            graph[c.from].push_back(c);
            in[c.to] = true;
            out[c.from] = true;
        }
        //创建总源点(2*NUM)和总终点(2*NUM+1),并进行连接
        //(因为起点和终点不是连续的,所以分成两个循环)
        for(int i = 0; i < N; i++)
        {
            if(!in[i])
            {
                edge t;
                t.from = 2*NUM;
                t.to = i;
                t.len = 0;
                graph[2*NUM].push_back(t);
            }
            if(!out[i])
            {
                edge t;
                t.from = i;
                t.to = 2*NUM+1;
                t.len = 0;
                graph[i].push_back(t);
            }
        }
        for(int i = NUM; i < NUM+N; i++)
        {
            if(!in[i])
            {
                edge t;
                t.from = 2*NUM;
                t.to = i;
                t.len = 0;
                graph[2*NUM].push_back(t);
            }
            if(!out[i])
            {
                edge t;
                t.from = i;
                t.to = 2*NUM+1;
                t.len = 0;
                graph[i].push_back(t);
            }
        }
        topo(2*NUM);
        sp(2*NUM);
        printf("%d\n", -dis[2*NUM+1]);
    }
    return 0;
}

 

 

 
posted @ 2018-06-27 10:50  樱花色的梦  阅读(284)  评论(0)    收藏  举报