题目描述

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.

分析

这道题至少有两种方法

拓扑排序

第一种方法就是拓扑排序

显然没有任何约束的指令可以在第一秒同时执行。

对有一个或多个约束指令我们要满足最远的那个约束之后

定义dp[i],表示执行指令i的最早时间,则有:dp[i]=max(dp[i],dp[j]+a[j][i]),a[j][i]表示i必须在j执行后a[j][i]秒后执行。

临界没有任何约束的指令在第一秒时执行,dp[]=1

阶段很明显,当前入度为0点,下个阶段为这些点的临界点。

代码

#include <bits/stdc++.h>
const int maxn=1000+5,maxm=1e4+5;
struct Node{int to;int dis;int next;}e[maxm];
int n,m,len,head[maxn],rd[maxn],dp[maxn];
void Insert(int x,int y,int z){
e[++len].to=y;e[len].dis=z;e[len].next=head[x];head[x]=len;
}
void Kahn(){
std::stack<int> q;
for(int i=0;i<n;++i){
if(!rd[i])q.push(i),dp[i]=1;
else dp[i]=0;
}
int ans=1;
while(!q.empty()){
int u=q.top();q.pop();
for(int i=head[u];i;i=e[i].next){
int v=e[i].to,w=e[i].dis;rd[v]--;
dp[v]=std::max(dp[v],dp[u]+w);
ans=std::max(ans,dp[v]);
if(!rd[v])q.push(v);
}
}
printf("%d\n",ans);
}
void Solve(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,0,sizeof(head));
memset(rd,0,sizeof(rd));
len=0;
for(int i=1;i<=m;++i){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
Insert(x,y,z);rd[y]++;
}
Kahn();
}
}
int main(){
Solve();
return 0;
}

最长路

其实细细思考一下,建一个超级源点跑最长路也是可以的

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int maxd=1005,maxb=20005;
int ru[maxd],chu[maxd];
int head[maxd],tot=1;
int n,m;
struct asd{
int from,to,next,val;
}b[maxb];
void ad(int aa,int bb,int cc){
b[tot].from=aa;
b[tot].to=bb;
b[tot].next=head[aa];
b[tot].val=cc;
head[aa]=tot++;
}
bool vis[maxd];
int dis[maxd];
void SPFA(){
queue<int> q;
q.push(n);
for(int i=0;i<maxd;i++){
dis[i]=-0x3f3f3f3f;
}
dis[n]=0;
while(!q.empty()){
int xx=q.front();
q.pop();
vis[xx]=0;
for(int i=head[xx];i!=-1;i=b[i].next){
int u=b[i].to;
if(dis[u]<dis[xx]+b[i].val){
dis[u]=dis[xx]+b[i].val;
if(!vis[u])q.push(u),vis[u]=1;
}
}
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
memset(&b,0,sizeof(struct asd));
memset(ru,0,sizeof(ru));
memset(chu,0,sizeof(chu));
tot=1;
for(int i=1;i<=m;i++){
int aa,bb,cc;
scanf("%d%d%d",&aa,&bb,&cc);
ad(aa,bb,cc);
ru[bb]++;
chu[aa]++;
}
for(int i=0;i<n;i++){
if(ru[i]==0){
ad(n,i,1);
}
}
SPFA();
int ans=1;
for(int i=0;i<n;i++){
ans=max(ans,dis[i]);
}
printf("%d\n",ans);
}
return 0;
}

Instrction Arrangement UDH 4109 拓扑排序 or 最长路的更多相关文章

  1. 2017 ACM-ICPC(乌鲁木齐赛区)网络赛 H.Skiing 拓扑排序+最长路

    H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...

  2. [模板]tarjan缩点+拓扑排序

    题目:给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 题目简述:先t ...

  3. BZOJ 1194 [HNOI2006]潘多拉的盒子 (图论+拓扑排序+tarjan)

    题面:洛谷传送门 BZOJ传送门 标签里三个算法全都是提高组的,然而..这是一道神题 我们把这道题分为两个部分解决 1.找出所有咒语机两两之间的包含关系 2.求出咒语机的最长上升序列 我们假设咒语机$ ...

  4. 2018.11.06 bzoj1093: [ZJOI2007]最大半连通子图(缩点+拓扑排序)

    传送门 先将原图缩点,缩掉之后的点权就是连通块大小. 然后用拓扑排序统计最长链数就行了. 自己yyyyyy了一下一个好一点的统计方法. 把所有缩了之后的点都连向一个虚点. 然后再跑拓扑,这样最后虚点的 ...

  5. BZOJ1880:[SDOI2009]Elaxia的路线(最短路,拓扑排序)

    Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w**每天都要奔波于宿舍和实验室之间, ...

  6. 【CSP模拟赛】益智游戏(最短路(DJSPFA)&拓扑排序)

    题目描述 小P和小R在玩一款益智游戏.游戏在一个正权有向图上进行. 小P 控制的角色要从A 点走最短路到B 点,小R 控制的角色要从C 点走最短路到D 点. 一个玩家每回合可以有两种选择,移动到一个相 ...

  7. HDU 4109 Instrction Arrangement

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

  8. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  9. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

随机推荐

  1. vi命令总结

    VI常用技巧 ​ VI命令可以说是Unix/Linux世界里最常用的编辑文件的命令了,但是因为它的命令集众多,很多人都不习惯使用它,其实您只需要掌握基本命令,然后加以灵活运用,就会发现它的优势,并会逐 ...

  2. CMD指令和GIT指令

    CMD指令 dir 显示当前文件夹的所有的文件目录 mkdir 创建文件夹 cd> 创建文件 rd 删除文件夹 del 删除文件 cls 清屏 Linux指令 查看版本 node -v node ...

  3. 蒲公英 · JELLY技术周刊 Vol.10 为什么你还不使用 TypeScript

    登高远眺 天高地迥,觉宇宙之无穷 基础技术 直播延迟?为什么流媒体直播的延迟那么高 随着通信技术的发展,视频点播.直播业务也逐渐兴起.在这些业务形态中,流媒体技术扮演着重要的角色.但在实际使用中,即便 ...

  4. R调用python模块

    明明已经安装了sctransfer,但仍然显示没有该模块 Error in py_module_import(module, convert = convert) : ModuleNotFoundEr ...

  5. fedora gtk+ 2.0环境安装配置

    1.安装gtk yum install gtk2 gtk2-devel gtk2-devel-docs 2.测试是否安装成功 pkg-config --cflags --libs gtk+-2.0 执 ...

  6. Docker——基于Docker安装Drupal博客系统

    Docker--基于Docker安装Drupal博客系统 向脚本文件追加内容 cat << EOF > build.sh #设置主机名 hostnamectl set-hostnam ...

  7. 使用navicat连接mysql连接错误:Lost connection to Mysql server at 'waiting for initial communication packet'

    使用navicat时,报错截图如下: 原因分析: mysql开启了DNS的反向解析功能,这样mysql对连接的客户端会进行DNS主机名查找. mysql处理客户端解析过程: 当mysql的client ...

  8. 当小程序的flex布局遇到button时,justify-content不起作用的原因及解决方案

    当小程序的flex布局遇到button时 发现justify-content不起作用,无论怎么设置都是space-around的效果. 经过排查,发现原因是小程序button中的默认样式中的margi ...

  9. NASH:基于丰富网络态射和爬山算法的神经网络架构搜索 | ICLR 2018

    论文提出NASH方法来进行神经网络结构搜索,核心思想与之前的EAS方法类似,使用网络态射来生成一系列效果一致且继承权重的复杂子网,本文的网络态射更丰富,而且仅需要简单的爬山算法辅助就可以完成搜索,耗时 ...

  10. 使用 Kafka + Spark Streaming + Cassandra 构建数据实时处理引擎

    Apache Kafka 是一个可扩展,高性能,低延迟的平台,允许我们像消息系统一样读取和写入数据.我们可以很容易地在 Java 中使用 Kafka. Spark Streaming 是 Apache ...