hdu 4109 Instrction Arrangement 拓扑排序/觉得更像BFS
2015-08-25 20:25
405 查看
Instrction Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1385 Accepted Submission(s): 576
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.
Source
2011 Alibaba-Cup Campus Contest
Recommend
lcy
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<climits> #include<queue> #include<vector> #include<map> #include<sstream> #include<set> #include<stack> #include<utility> #pragma comment(linker, "/STACK:102400000,102400000") #define PI 3.1415926535897932384626 #define eps 1e-10 #define sqr(x) ((x)*(x)) #define FOR0(i,n) for(int i=0 ;i<(n) ;i++) #define FOR1(i,n) for(int i=1 ;i<=(n) ;i++) #define FORD(i,n) for(int i=(n) ;i>=0 ;i--) #define lson num<<1,le,mid #define rson num<<1|1,mid+1,ri #define MID int mid=(le+ri)>>1 #define zero(x)((x>0? x:-x)<1e-15) #define mp make_pair #define _f first #define _s second using namespace std; const int INF =0x3f3f3f3f; const int maxn= 1000+8 ; const int maxm=10000+11 ; //const int INF= ; typedef long long ll; const ll inf =1000000000000000;//1e15; //ifstream fin("input.txt"); //ofstream fout("output.txt"); //fin.close(); //fout.close(); //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); //by yskysker123 int n,m; int e_max; int u[maxm],v[maxm],w[maxm]; int nex[maxm]; int fir[maxn]; int d[maxn]; int dis[maxn]; int q[maxn]; void init() { memset(fir,-1,sizeof fir); e_max=0; } inline void add_edge(int s,int t,int w_) { int e=e_max++; u[e]=s; v[e]=t; w[e]=w_; nex[e]=fir[s]; fir[s]=e; } void top() { memset(d,0,sizeof d); for(int e=0;e<e_max;e++) { int y=v[e]; d[y]++; } int f=0,r=0; for(int i=0;i<n;i++) { dis[i]=1; if(d[i]) continue; q[r++]=i; } while( f<r) { int x=q[--r]; int dist=dis[x]; for(int e=fir[x];~e;e=nex[e]) { int y=v[e]; dis[y]=max(dis[y], dist+ w[e]); if(--d[y ]==0) { q[r++]=y; } } } } int main() { int x,y,w; while(~scanf("%d%d",&n,&m)) { init(); for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&w); add_edge(x,y,w); } top(); int maxi=1; for(int i=0;i<n;i++) maxi=max(dis[i],maxi); printf("%d\n",maxi); } return 0; }
相关文章推荐
- Weka训练数据集.arff大合集
- c++加密解密
- IP地址和域名的关系
- 一步一步学习 Linux 下多线程的调试
- [Netty] - Netty入门(最简单的Netty客户端/服务器程序)
- 信息熵
- 转-JQuery选择器及radio,checkbox,select取值和反选
- Android下SQLite数据库学习笔记5——Android下数据库的事务
- Maven使用经验总结
- poj- 2503
- 二分图最大匹配
- HashMap的两种排序方式
- java框架篇---struts之OGNL详解
- 支持向量机之线性支持向量机(三)
- VLAD局部特征聚合描述符
- CRF技能词识别过程
- bootstrap-js(5)工具提示tooltip
- Java Servlet完全教程
- struct和typedef struct分析
- MySQL合理利用锁机制优化