Instrction Arrangement (hdu 4109 差分约束)

23 篇文章 0 订阅

Instrction Arrangement

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


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  4101  4102 
 
题意:安排n个任务在CPU上工作,告诉m个限制,u,v,z表示v必须在u指令之后执行,并且u和v之间要间隔z秒,问把所有的任务完成最少时间为多少。
思路:差分约束系统,由题意:dist[v]-dist[u]>=z,变形得:dist[u]<=dist[v]-z,根据这个建图v->u权为-z,源点到i权为0,i到汇点权为-1,然后求最短路,答案为 -dist[n+1].
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;

const int MAXM = 100000;
const int MAXN = 1005;

struct Edge
{
    int u,v,len,next;
}edge[MAXM];

int n,m,num;
int head[MAXN],dist[MAXN],inq[MAXN];

void init()
{
    num=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int len)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].len=len;
    edge[num].next=head[u];
    head[u]=num++;
}

void spfa()
{
    memset(dist,INF,sizeof(dist));
    memset(inq,0,sizeof(inq));
    inq[0]=1;
    dist[0]=0;
    queue<int>Q;
    Q.push(0);
    while (!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        for (int i=head[u];~i;i=edge[i].next)
        {
            int v=edge[i].v;
            if (dist[v]>dist[u]+edge[i].len)
            {
                dist[v]=dist[u]+edge[i].len;
                if (!inq[v])
                {
                    inq[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    printf("%d\n",-dist[n+1]);
}

int main()
{
    int i,j,u,v,w;
    while (~scanf("%d%d",&n,&m))
    {
        init();
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            u++,v++;
            addedge(v,u,-w);
        }
        for (i=1;i<=n;i++)
        {
            addedge(0,i,0);
            addedge(i,n+1,-1);
        }
        spfa();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值