hdu4109(拓扑排序,dp)
来源:互联网 发布:悠百佳怎么样知乎 编辑:程序博客网 时间:2024/06/04 20:08
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.
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.
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 21 2 13 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思想在里面,需要自己分析,画个图一层一层分析,一层一层的过程就是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;}
0 0
- hdu4109(拓扑排序,dp)
- HDU4109 Instrction Arrangement 拓扑排序 关键路径
- hdu4109拓扑
- HDU4109 Instrction Arrangement 拓扑排序求关键路径
- HDU4109 Instrction Arrangement【差分约束】【拓扑排序】
- hdu4109拓扑Instrction Arrangement
- poj3249 拓扑排序+DP
- csu1804 拓扑排序+dp
- hdu4109
- poj 3249(DP+拓扑排序)
- 【BZOJ4562】【食物链】【dp+拓扑排序】
- 【DP】【拓扑排序】road 题解
- poj 3249 Test for Job(拓扑排序+DP)
- POJ 3249 Test for Job(拓扑排序+dp)
- csu1804: 有向无环图(拓扑排序+dp)
- 洛谷P1137 旅行计划(拓扑排序+dp)
- 2017 icpc 乌鲁木齐赛区 H.Skiing(拓扑排序+DP)
- BZOJ 4562: [Haoi2016]食物链 拓扑排序+DP(其实是递推)
- jQuery 从input中读取的内容的类型
- hdu 3555数位DP 入门题
- thinkphp 上传单张图片
- 杭电1181
- 迈出从3K到1W的重要一步——掌握设计模式
- hdu4109(拓扑排序,dp)
- HDU 1865 六度分离 <裸的迪杰斯特拉算法>
- Python中有关list的增删改查
- Bag Problem
- TCP快速重传与快速恢复原理分析
- 1638 - Pole Arrangement (递推)
- Libvirt之vfio pci的passthrough
- 黑马程序员------正则表达式
- 再读《C和指针》(笔记3)