hdu 4109(拓扑排序 关键路径)
2015-08-11 10:49
351 查看
与hdu2647有相似之处,需要牢记。
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4109
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1268 Accepted Submission(s): 525
Problem 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
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.
由入度为0的点开始,按照层数递进,算出每个任务点完成所需最少时间,最后一一比较即可。
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4109
Instrction Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1268 Accepted Submission(s): 525
Problem 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
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.
由入度为0的点开始,按照层数递进,算出每个任务点完成所需最少时间,最后一一比较即可。
#include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> using namespace std; struct node{ int y,t; }edge[10005]; vector<node> v[1005]; int n,m; int in[1005],ti[1005]; void solve(){ queue<int> q; for (int i=0; i<n; i++) { if (in[i]==0) { q.push(i); ti[i]=1; } } while (!q.empty()) { int x=q.front();q.pop(); for (int i=0; i<v[x].size(); i++) { int y=v[x][i].y; ti[y]=max(v[x][i].t+ti[x],ti[y]); in[y]--; if (in[y]==0) { q.push(y); } } } int ans=0; for (int i=0; i<n; i++) { ans=max(ans,ti[i]); } cout<<ans<<endl; } int main(){ int x; while (cin>>n>>m) { memset(in, 0, sizeof(in)); memset(ti, 0, sizeof(ti)); for (int i=0; i<n; i++) { v[i].clear(); } for (int i=0; i<m; i++) { cin>>x>>edge[i].y>>edge[i].t; v[x].push_back(edge[i]); in[edge[i].y]++; } solve(); } return 0; }
相关文章推荐
- SSL心跳漏洞的检测
- IOS开发 @property中assign、copy 、retain等关键字的理解
- 算法竞赛入门经典: 第三章 数组和字符串 3.3蛇形填数
- 21-IO流-14-IO流(字符流-缓冲区-BufferedReader-readLine方法原理)
- Oracle 12C -- Invisible Columns
- Error: L6218E: Undefined symbol main (referred from entry9a.o).
- Algorithms-94.Binary Tree Inorder Traversal
- ACM Computer Factory
- 21-IO流-13-IO流(字符流-缓冲区-BufferedReader)
- php5.4和apache2.4.9不在同一台服务器的配置注意事项
- OR1200数据Cache运用情景分析
- php webservice soap
- ACM Computer Factory
- ACM Computer Factory
- HDU 1269 迷宫城堡
- 神奇的口袋(百练2755) ( DFS + DP )
- 图片
- apache 日志切割
- myeclipse ctrl +D 删除当前行失效解决方法
- windows 2012 R2 DHCP FailOver