hdu4109(拓扑排序,dp)
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 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思想在里面,需要自己分析,画个图一层一层分析,一层一层的过程就是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; }
持续更新博客地址:
blog.csdn.net/martinue