hdu4109(拓扑排序,dp)
2015-08-18 19:21
309 查看
Description
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
Sample Output
Hint
这题有dp思想在里面,需要自己分析,画个图一层一层分析,一层一层的过程就是dp!
然后还是拓扑排序的方法,中间加了点儿dp,没什么难度。
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 21 3 4 1
Sample Output
2
Hint
In the 1st ns, instruction 0, 1 and 3 are executed; In the 2nd ns, instruction 2and 4 are executed. So the answer should be 2.
这题有dp思想在里面,需要自己分析,画个图一层一层分析,一层一层的过程就是dp!
然后还是拓扑排序的方法,中间加了点儿dp,没什么难度。
#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[1010][1010],rd[1010],jl[1010],oo[1010]; int t=0; void bfs(int n) { int x=0; t=0; while(x<n) { for(int i=0;i<n;i++) if(rd[i]==0) { x++; rd[i]--;int flag=0; for(int j=0;j<n;j++) if(a[i][j]!=0) { rd[j]--; jl[j]=max(jl[j],jl[i]+a[i][j]); flag=1; } if(flag==0) oo[t++]=jl[i]+1; break; } } } int main() { int m,n; while(~scanf("%d%d",&n,&m)) { memset(a,0,sizeof(a)); memset(jl,0,sizeof(jl)); memset(rd,0,sizeof(rd)); memset(oo,0,sizeof(oo)); for(int i=0;i<m;i++) { int c,b,w;scanf("%d%d%d",&b,&c,&w); if(a[b][c]==0) a[b][c]=w,rd[c]++; } bfs(n); int s=0; for(int i=0;i<t;i++) s=max(oo[i],s); cout<<s<<endl; } return 0; }
相关文章推荐
- android客户端从服务器端获取json数据并解析的实现代码(重要)
- 给phpcms v9加入一个主题radio无线电button,它可反复使用,以创建不同的专题部分内容编辑器,添加一个主题来定义自己的领域
- leetcode---different ways to add parentheses
- [Unity]SQLite-C#调用
- TCP快速重传与快速恢复原理分析
- HUD-5399 Too Simple(数学)
- HDU 1865 六度分离 <裸的迪杰斯特拉算法>
- springMVC 配置jdbcTemplate连接Oracle数据库出错
- Bag Problem
- hadoop native本地库问题总结
- 初遇 dotcloud
- Bag Problem
- Children’s Queue 1297 (大数)
- iOS开发中的神技能
- 迈出从3K到1W的重要一步——掌握设计模式
- Eclipse Outline视图你所不知道的icon含义
- thinkphp 上传单张图片
- hdu 3555数位DP 入门题
- hdu2647(拓扑排序)
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载