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.
题解:
加一个源点s指向所有点边权为0,
加一个汇点t,所有点指向t边权为0,
u和v安全距离为w,则加边v->u,边权为-w
(因为 u-v>=w 所以v-u<=-w )
求s到t最短路即可。

参考代码如下:

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
const int maxm = 11005;
int n, m;
struct node
{
int v, w, next;
}edge[maxm];
int no, head[maxn];
int vis[maxn], dis[maxn], cnt[maxn];
queue<int> q;
inline void init()
{
no = 0;
memset(head, -1, sizeof head);
}
inline void add(int u, int v, int w)
{
edge[no].v = v; edge[no].w = w;
edge[no].next = head[u]; head[u] = no++;
}
void SPFA()
{
while(!q.empty()) q.pop();
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
fill(dis, dis+n+1, -inf);
dis[0] = 0; vis[0] = 1;
q.push(0);
while(!q.empty())
{
int u = q.front(); q.pop();
vis[u] = 0; ++cnt[u];
if(cnt[u] > n) return ;
for(int k = head[u]; k != -1; k = edge[k].next)
{
int v = edge[k].v;
if(dis[v] < dis[u]+edge[k].w)
{
dis[v] = dis[u]+edge[k].w;
if(!vis[v]) q.push(v), vis[v] = 1;
}
}
}
}
int main()
{
int u, v, w, ans;
while(~scanf("%d %d", &n, &m))
{
init(); ans = 0;
for(int i = 1; i <= m; ++i)
{
scanf("%d %d %d", &u, &v, &w);
add(u+1, v+1, w);
}
for(int i = 1; i <= n; ++i) add(0, i, 0);
SPFA();
for(int i = 1; i <= n; ++i) ans = max(ans, dis[i]);
printf("%d\n", ans+1);
}
return 0;
}

HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)的更多相关文章

  1. HDU.1529.Cashier Employment(差分约束 最长路SPFA)

    题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...

  2. POJ.1752.Advertisement(差分约束 最长路SPFA)

    题目链接 \(Description\) 有\(n\)个人在一条直线上跑步,每个人的起点 \(Si\).终点 \(Ei\) 已知:每个点可以放一个广告牌,一个人\(i\)能看到的广告牌数量为 \(Ei ...

  3. HDU1529-Casher Emploryment(最最...最经典的差分约束 差分约束-最长路+将环变线)

    A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its n ...

  4. 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...

  5. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]

    题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...

  6. 2021.07.19 P2294 狡猾的商人(差分约束)

    2021.07.19 P2294 狡猾的商人(差分约束) [P2294 HNOI2005]狡猾的商人 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.差分约束最长路与最短 ...

  7. 【HDOJ4109】【拓扑OR差分约束求关键路径】

    http://acm.hdu.edu.cn/showproblem.php?pid=4109 Instrction Arrangement Time Limit: 2000/1000 MS (Java ...

  8. 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)

    洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...

  9. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

随机推荐

  1. linux引导之grub2

    先了解下什么是Bootloader 以下是百度百科释意 在嵌入式操作系统中,BootLoader是在操作系统内核运行之前运行.可以初始化硬件设备.建立内存空间映射图,从而将系统的软硬件环境带到一个合适 ...

  2. 【Elasticsearch 7 探索之路】(三)倒排索引

    上一篇,我们介绍了 ES 文档的基本 CURE 和批量操作.我们都知道倒排索引是搜索引擎非常重要的一种数据结构,什么是倒排索引,倒排索引的原理是什么. 1 索引过程 在讲解倒排索引前,我们先了解索引创 ...

  3. spring源码1

    1.beans核心类 1.DefaultListableBeanFactory xmlBeanFactory xmlBeanFactory继承自DefaultListableBeanFactory,D ...

  4. Arduino 配置 ESP8266环境

    Arduino 配置 ESP8266环境 将 http://arduino.esp8266.com/stable/package_esp8266com_index.json 添加到 [附加开发板管理器 ...

  5. pat 1065 A+B and C (64bit)(20 分)(大数, Java)

    1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed t ...

  6. nyoj 513-A+B Problem IV (java BigDecimal, stripTrailingZeros, toPlainString)

    513-A+B Problem IV 内存限制:64MB 时间限制:1000ms 特判: No 通过数:1 提交数:2 难度:3 题目描述: acmj最近发现在使用计算器计算高精度的大数加法时很不方便 ...

  7. LeetCode 5272. 5272. 统计参与通信的服务器 Count Servers that Communicate

    地址 https://leetcode-cn.com/problems/count-servers-that-communicate/ 题目描述这里有一幅服务器分布图,服务器的位置标识在 m * n  ...

  8. Install zabbix

    - name: Create dir to keep install file file: path=/opt/pacheage state=directory follow=yes force=ye ...

  9. Openlayers 地图定位到相应位置并缩放

    说明: 在地图操作中,有个功能,需要点击一个点,将视图定位到点击点的位置,并放大. 解决方案: 1.可以有openlayers中可以有ol.View来控制,但是在更改时,会将地图初始化时设置的maxZ ...

  10. Python 编程语言要掌握的技能之一:使用数字与字符串的技巧

    最佳实践 1. 少写数字字面量 “数字字面量(integer literal)” 是指那些直接出现在代码里的数字.它们分布在代码里的各个角落,比如代码 del users[0] 里的 0 就是一个数字 ...