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. K8S入门系列之集群二进制部署-->node篇(三)

    node节点组件 docker kubelet kube-proxy kubernetes-server-linux-amd64.tar.gz(相关的这里都能找到二进制文件!) falnnel 1. ...

  2. CentOS7 编码编译安装或卸载http2.4.25 一键脚本

    待完善 CentOS 7测试 哈哈 #!/bin/bash #************************************************************** #Autho ...

  3. ChickenLegend Image

  4. C# II: Class ViewModelBase and RelayCommand in MVVM

    好久不写WPF和MVVM,新建一个Project后,想起来ViewModelBase和RelayCommand没有.以下Code摘自MSDN上的Article:Patterns - WPF Apps ...

  5. hdu 1028 Sample Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. Python标准类型的分类

    Python有3种不同的模型可以帮助对基本类型进行分类,这些类型更好的理解类型之间的相互关系以及他们的工作原理. 1 存储模型    能保存单个字面对象的类型,称为原子或标量存储:    能保存多个对 ...

  7. Extjs导入Excel文件之后grid自动刷新显示刚插入的数据

    var winUpload = new Ext.Window({ title: '导入excel文件', width: 400, height:200, listeners: { close: fun ...

  8. SQLite性能 - 意想不到,但又情理之中的测试结果。

    win7(64) sata2 希捷 MINGW32_NT-(/) cat: /proc/cpuinfo: No such file or directory ------ in disk ---- r ...

  9. .NET开发者的机遇与WebAssembly发展史(有彩蛋)

    一.唠唠WebAssembly的发展历程 目前有很多支持WebAssembly的项目,但发展最快的是Blazor,这是一个构建单页面的.NET技术,目前已经从Preview版本升级到了beta版本,微 ...

  10. Spring Boot 2.X(十八):集成 Spring Security-登录认证和权限控制

    前言 在企业项目开发中,对系统的安全和权限控制往往是必需的,常见的安全框架有 Spring Security.Apache Shiro 等.本文主要简单介绍一下 Spring Security,再通过 ...