题目描述

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

最新文章

  1. 利用Python进行数据分析(1) 简单介绍
  2. SCVMM中Clone虚拟机失败显示Unsupported Cluster Configuration状态
  3. WebService初学
  4. Sprint3(12.18)总结
  5. ELCImagePickerController 的集成
  6. RecycleView + CardView 控件简析
  7. perl Can&#39;t use string Cxxx) as a symbol ref while &quot;strict refs&quot; in use at XXXX.pl错误
  8. struts2 &lt;s:property/&gt;标签的使用--输出时间格式转换
  9. php empty isset is_null
  10. 慕课网-安卓工程师初养成-4-3 Java条件语句之多重 if
  11. 基于jquery的页面代码的优化
  12. poj 1797 Heavy Transportation(最短路变种2,连通图的最小边)
  13. C语言学习之笔记
  14. MVC部署-发布本地数据库(Localdb)时连接异常
  15. Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理
  16. css实现未知高度水平垂直居中
  17. spring框架应用系列三:切面编程(带参数)
  18. 1.YAF 的安装
  19. layui:数据表格如何合并单元格
  20. epoll函数

热门文章

  1. Jmeter之Json提取器详解(史上最全)
  2. lambda表达式操作DataTable
  3. 官宣!ASF官方正式宣布Apache Hudi成为顶级项目
  4. leetcode之两数相加解题思路
  5. 源码分析(4)-ConcurrentHashMap(JDK1.8)
  6. 源码分析(1)-HashMap(JDK1.8)
  7. VS2019制作的安装包,默认安装到C盘快捷方式无法打开
  8. MYSQL SQL 语句修改字段默认值
  9. Laravel中Homestead添加多站点时遇到问题
  10. 从零开始的Spring Boot(6、Thymeleaf内置对象及表达式大全)