HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)
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(差分约束-最长路+建立源点,汇点)的更多相关文章
- HDU.1529.Cashier Employment(差分约束 最长路SPFA)
题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...
- POJ.1752.Advertisement(差分约束 最长路SPFA)
题目链接 \(Description\) 有\(n\)个人在一条直线上跑步,每个人的起点 \(Si\).终点 \(Ei\) 已知:每个点可以放一个广告牌,一个人\(i\)能看到的广告牌数量为 \(Ei ...
- 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 ...
- 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa
原文地址:http://www.cnblogs.com/GXZlegend/p/6801470.html 题目描述 背景 小K是个特么喜欢玩MC的孩纸... 描述 小K在MC里面建立很多很多的农场,总 ...
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...
- 2021.07.19 P2294 狡猾的商人(差分约束)
2021.07.19 P2294 狡猾的商人(差分约束) [P2294 HNOI2005]狡猾的商人 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.差分约束最长路与最短 ...
- 【HDOJ4109】【拓扑OR差分约束求关键路径】
http://acm.hdu.edu.cn/showproblem.php?pid=4109 Instrction Arrangement Time Limit: 2000/1000 MS (Java ...
- 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)
洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...
- hdu 1534(差分约束+spfa求最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...
随机推荐
- 深入理解计算机系统 第八章 异常控制流 part1
本章主旨 第八章的目的是阐述清楚应用程序是如何与操作系统交互的(之前章节的学习是阐述应用程序是如何与硬件交互的) 异常控制流 异常控制流,即 ECF(exceptional contril flow) ...
- PHP数组与xml互相转换
1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...
- linux命令指令
1.ls显示目录内容 -a 显示目录下所有文件 -l 显示详细信息 ls *.c 列出当前目录所有的.c文件 2.uname -a 查看内核版本 3.whoami 查看谁登陆虚拟机 4.cd 切 ...
- iOS UIView x Android View
- Microsoft Dynamics CRM 批量上传web资源(非官方WebResourceUtility)并替换实体图标
背景: 去年以前可以按照目录WebResourceUtility批量上传web资源,昨天发现用不了了,拿到WebResourceUtility源码改了一下都不是很方便,感觉官方写的太冗余,太长了,跟我 ...
- 你真的会用JavaScript中的sort方法吗
在平时的业务开发中,数组(Array) 是我们经常用到的数据类型,那么对数组的排序也很常见,除去使用循环遍历数组的方法来排列数据,使用JS数组中原生的方法 sort 来排列(没错,比较崇尚JS原生 ...
- Git的使用和基本概念理解
参考:https://www.liaoxuefeng.com/wiki/896043488029600 一).git的使用: 1.创建版本库(Resopsitory),相当于一个目录,目录中所有的文件 ...
- 天啦!竟然从来没有人讲过 SpringBoot 支持配置如此平滑的迁移
SpringBoot 是原生支持配置迁移的,但是官方文档没有看到这方面描述,在源码中才看到此模块,spring-boot-properties-migrator,幸亏我没有跳过.看到这篇文章的各位,可 ...
- Spring 5 响应式编程
要点 Reactor 是一个运行在 Java8 之上的响应式流框架,它提供了一组响应式风格的 API 除了个别 API 上的区别,它的原理跟 RxJava 很相似 它是第四代响应式框架,支持操作融合, ...
- PowerMock学习(五)之Verifying的使用
前言 Verifying是一个非常强大的测试工具,在mock系列框架中使用广泛,主要用于验证方法是否被调用,下面将举例说明. 场景 模拟这样一个场景,通过Dao查询学生,如果存在更新原来学生,不存在则 ...