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

图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement的更多相关文章

  1. 【HDOJ】4109 Instrction Arrangement

    差分约束. /* 4109 */ #include <iostream> #include <queue> #include <vector> #include & ...

  2. POJ 3169 Layout (图论-差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Descriptio ...

  3. 差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

    [bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特 ...

  5. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  6. 图论--差分约束--POJ 3169 Layout(超级源汇建图)

    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...

  7. HDU 4109 Instrction Arrangement(DAG上的最长路)

    把点编号改成1-N,加一点0,从0点到之前任意入度为0的点之间连一条边权为0的边,求0点到所有点的最长路. SPFA模板留底用 #include <cstdio> #include < ...

  8. HDU 4109 Instrction Arrangement

    题目链接:https://vjudge.net/problem/HDU-4109 题目大意 有 N 个指令,标号从 0 ~ N - 1,和 M 个指令间的先后关系,每个关系都有一个权值 w,表示后一个 ...

  9. POJ 1201 Intervals(图论-差分约束)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Descri ...

随机推荐

  1. java web数据库的增删改查详细

    本次课上实验是完成数据库的增删改查. 包括增加用户信息.删除用户信息.多条件查找用户信息.修改用户信息(主要是复选框单选框等的相关操作.) 下面下看一下各个界面的样子. 总页面:显示全部页面:增加页面 ...

  2. python 函数--闭包函数

    一.闭包函数: 在一个外函数中定义一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用. 二.实例: def outer(a): #外函数 b = 10 #临时变量 def i ...

  3. VirtualBox的四种网络连接方式【转】

    VirtualBox中有4中网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实他跟VMWare 的网络连接方式都是 ...

  4. PyCharm 项目打开窗口设置为当前还是新开一个怎么办?

     前言:       我找这个设置找了好久,后来在一篇博文中才找到,现在记录下来一下,顺便带图解释一下   设置步骤: File -> Setting -> Appearance & ...

  5. AJ学IOS(44)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配

    AJ分享,必须精品 效果: 注意图里面了吗,其实那个效果做起来真的很简单,在iOS中苹果给我们封装的很好,关键是那个按钮 系统的按钮的图片是在左边的,这里我们需要把他调整到右边,然后呢需要我们自己做一 ...

  6. 1 - Apache HttpClient 简单使用

    Apache HttpClient 是Apache 开源的实现Http协议的java开源库. HttpClien 是客户端的HTTP通信实现库,实现HTTP GET 和POST请求,获取响应内容. A ...

  7. 王者荣耀英雄全皮肤4K高清大图,python爬虫帮你保存下来

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...

  8. Gun N' Rose Team Review

    一看到这个项目就被他的功能给吸引了.回忆起以前看到一个东西很新奇想去网上查询它是什么,但是又不知道应该怎样去描述它,于是在搜索引擎的输入框中键入.删除.键入.删除的可笑经历的时候,我就越发感觉到这个a ...

  9. mapstruct使用详解

    我们都知道,随着一个工程的越来越成熟,模块划分会越来越细,其中实体类一般存于 domain 之中,但 domain 工程最好不要被其他工程依赖,所以其他工程想获取实体类数据时就需要在各自工程写 mod ...

  10. React AntDesign 引入css

    React项目是用umi脚手架搭建的AntDesign,用到一个第三方表格组件Jexcel,npm install 之后组件的样式加载不上,犯了愁,翻阅各种资料,踏平两个小坑. 大家都知道,安装完成的 ...