专栏首页CSDN旧文图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement

图论--差分约束--HDU\HDOJ 4109 Instrction 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.

题意:一台电脑需要执行N条指令(0到N-1),每条指令都要花费一单位时间,可以同时执行无限条指令。有M个约束条件(X,Y,Z),表示指令Y必须在指令X执行后过Z单位时间才能执行。问执行完所有的指令需要的最短时间。

思路:显然就是差分约束嘛,设Si为指令i的开始时间,对每条约束可以得到不等式 Sy >= Sx + Z。

这道题目建的图不一定是连通的,采用初始时将所有结点加入队列的方法代替超级源点,将图变成虚连通的。

求最短时间所有跑最长路,因为图中不含负边,所以可以直接将初始的距离dis置为1,不用置为-inf。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn =1000+10;
const int maxm =20000+10;
 
struct Edge
{
    int from,to,dist;
    Edge(){}
    Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
 
struct BellmanFord
{
    int n,m;
    int head[maxn],next[maxm];
    Edge edges[maxm];
    int d[maxn];
    bool inq[maxn];
 
    void init(int n)
    {
        this->n=n;
        m=0;
        memset(head,-1,sizeof(head));
    }
 
    void AddEdge(int from,int to,int dist)
    {
        edges[m]=Edge(from,to,dist);
        next[m]=head[from];
        head[from]=m++;
    }
 
    void bellmanford()
    {
        memset(inq,0,sizeof(inq));
        for(int i=0;i<n;i++) d[i]= i==0?0:INF;
        queue<int> Q;
        Q.push(0);
 
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=head[u];i!=-1;i=next[i])
            {
                Edge &e=edges[i];
                if(d[e.to] > d[u]+e.dist)
                {
                    d[e.to] = d[u]+e.dist;
                    if(!inq[e.to])
                    {
                        inq[e.to]=true;
                        Q.push(e.to);
                    }
                }
            }
        }
    }
}BF;
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        BF.init(n+1);
        while(m--)
        {
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            ++u,++v;
            BF.AddEdge(v,u,-d);
        }
        for(int i=1;i<=n;i++)
            BF.AddEdge(0,i,0);
        BF.bellmanford();
        int max_v=-1,min_v=INF;
        for(int i=1;i<=n;i++)
        {
            max_v=max(max_v,BF.d[i]);
            min_v=min(min_v,BF.d[i]);
        }
        printf("%d\n",max_v-min_v+1);
    }
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Codeforce 322E Ciel the Commander (点分治)

    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, ha...

    风骨散人Chiam
  • 图论--差分约束--POJ 3159 Candies

    风骨散人Chiam
  • Codeforce-Ozon Tech Challenge 2020-D. Kuroni and the Celebration(交互题+DFS)

    After getting AC after 13 Time Limit Exceeded verdicts on a geometry problem, Ku...

    风骨散人Chiam
  • Codeforce 322E Ciel the Commander (点分治)

    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, ha...

    风骨散人Chiam
  • codeforces 1382C1(思维)

    This is the easy version of the problem. The difference between the versions is ...

    dejavu1zz
  • P2871 [USACO07DEC]手链Charm Bracelet

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of ...

    attack
  • codeforces1322A(括号匹配)

    A bracketed sequence is called correct (regular) if by inserting “+” and “1” you...

    dejavu1zz
  • HDUOJ-----(1162)Eddy's picture(最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327...

    Gxjun
  • Planning mobile robot on Tree (EASY Version)(UVA12569,状态压缩)

    题目链接:https://vjudge.net/problem/UVA-12569

    ACM算法日常
  • Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

    To become the king of Codeforces, Kuroni has to solve the following problem.

    风骨散人Chiam

扫码关注云+社区

领取腾讯云代金券