码迷,mamicode.com
首页 > 其他好文 > 详细

Instrction Arrangement (hdu 4109 差分约束)

时间:2015-08-25 23:45:50      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:instrction arrangeme   hdu 4109   差分约束   

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Instrction Arrangement (hdu 4109 差分约束)

标签:instrction arrangeme   hdu 4109   差分约束   

原文地址:http://blog.csdn.net/u014422052/article/details/47987273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!