HDU 4109 【差分约束系统】 ||【拓扑排序】

原题链接

Instrction Arrangement

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


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
 

题意:

有N条指令的系统,该系统具有M个如下形式的依赖关系:X Y Z,表示Y指令必须

在X指令后面Z纳秒执行. 现在问你该系统的指令运行完至少需要多少纳秒?

(每条指令需要运行1纳秒,每一纳秒可以执行无数个程序)

差分约束系统:

一、首先对于x,y,z。有d[y]-d[x]>=z;d[x]<=d[y]-z;

     故建图 y->x,权值为-z

设一个起点为s,一个终点点为t;对任意程序x有 d[x]-d[s]>=0;d[t]-d[x]>=0;

d[s]<=d[x]+0;d[x]<=d[t]+0;

x->s,权值为0,t->x,权值为0;

由于建的是逆向负权图,所以从t到s求最短路,就能得出答案。


#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
struct edge
{
    int to,cost,next;
}e[50000];
int head[50000],d[1005],n,tot,m;
bool vis[1005];
void add(int from,int to,int w)
{
    e[tot].to=to;
    e[tot].cost=w;
    e[tot].next=head[from];
    head[from]=tot++;
}
int spfa(int s,int t)
{
    queue<int> que;
    que.push(s);
    for(int i=0;i<=t;i++)
    {
        d[i]=INF;
        vis[i]=false;
    }
    vis[s]=true;
    d[s]=0;
    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
           if(d[v]>d[u]+e[i].cost)
           {
               d[v]=d[u]+e[i].cost;
               if(!vis[v])
               {
                   que.push(v);
                   vis[v]=true;
               }
           }
        }
        vis[u]=false;
    }
    return d[t];
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       memset(head,-1,sizeof(head));
       tot=0;
      for(int i=1;i<=m;i++)
      {
         scanf("%d%d%d",&x,&y,&z);
         x++;y++;
         add(y,x,-z);
      }
      for(int i=1;i<=n;i++)
      {
          add(0,i,0);
          add(i,n+1,0);
      }
      printf("%d\n",1-spfa(0,n+1));
    }
    return 0;
}

方法二,拓扑排序代码

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
struct edge
{
    int to,cost,next;
}e[50000];
int head[50000],d[1005],n,tot,m,dag[1005];
void add(int from,int to,int w)
{
    e[tot].to=to;
    e[tot].cost=w;
    e[tot].next=head[from];
    head[from]=tot++;
}
void topu()
{
    queue<int> que;
    for(int i=1;i<=n;i++)
    {
        if(dag[i]==0)
        {
            que.push(i);
            d[i]=1;
        }
    }
    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;d[v]=max(d[v],d[u]+e[i].cost);
            dag[v]--;
            if(dag[v]==0)
            {
                que.push(v);
                
            }
        }
    }
}
int main()
{
    int x,y,z;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
       memset(head,-1,sizeof(head));
       memset(d,0,sizeof(d));
       memset(dag,0,sizeof(dag));
       tot=0;
      for(int i=1;i<=m;i++)
      {
         scanf("%d%d%d",&x,&y,&z);
         x++;y++;
         add(x,y,z);
         dag[y]++;
      }
      topu();
      int maxx=0;
      for(int i=1;i<=n;i++)
        maxx=max(maxx,d[i]);
      printf("%d\n",maxx);
    }
    return 0;
}





 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值