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
 
拓扑排序
还是多线程的
题意:有n个指令m个要求     例如 X Y Z 代表 指令Y必须在指令X后 Z秒执行 输出cpu运行的最小时间
 运行最小时间 也就是要满足最大的时间要求
  vector<node>存图 node 结构体包含 mubiao timm
  入读为零的点 tim[] 初始化为1
   tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm); //确保满足最大时间要求
 
 
#include<bits/stdc++.h>
using namespace std;
int n,m;
int g;
struct node
{
int mubiao;
int timm;
}gg[10005];
vector<node> mp[1005];
int tim[1005];
int in[1005];
queue<int>q;
int max(int ss,int bb)
{
if(ss>bb)
return ss;
return bb;
}
//struct node gg;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
mp[i].clear();
in[i]=0;
}
memset(tim,0,sizeof(tim));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&g,&gg[i].mubiao,&gg[i].timm);
mp[g].push_back(gg[i]);
in[gg[i].mubiao]++;
}
for(int i=0;i<n;i++)
{
if(in[i]==0)
{
q.push(i);
tim[i]=1;
}
}
int re=0;
while(!q.empty())
{
int j=q.front();
q.pop();
int temp=tim[j];
if(re<=temp)
re=temp;
for(unsigned int i=0;i<mp[j].size();i++)
{
tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm);
if(--in[mp[j][i].mubiao]==0)
{
q.push(mp[j][i].mubiao);
}
}
}
printf("%d\n",re);
}
return 0;
}

最新文章

  1. Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0
  2. list[C++]
  3. 测试与log
  4. 【网站开发】在新浪SAE上搭建一个博客
  5. 【Java 基础篇】【第四课】初识类
  6. Revit 二次开发 (二) 倾斜的板
  7. UVALive6571 It Can Be Arranged(最小路径覆盖)
  8. iOS开发——动画编程Swift篇&amp;(三)CATransition动画
  9. 【HDOJ】4585 Shaolin
  10. 【转载】ASP.NET线程安全与静态变量的生命周期浅谈
  11. AngularJs 如何监视外部变量是否改变? 如何使用$cookieStore保存cookie?
  12. XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法
  13. 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)
  14. Android中Handler的消息处理机制以及源码分析
  15. CTF---Web入门第十二题 程序逻辑问题
  16. c++ 之模板进阶
  17. C#设置随机整数
  18. C/C++基础----动态内存
  19. Ice简介+Qt代码示例
  20. DIV字体

热门文章

  1. 完整的正则表达式知识汇总(Python知识不断更新)
  2. 【WXS全局对象】Number
  3. 41. Maximum Subarray
  4. VMWare Workstation新装CentOS 7无法联网解决办法
  5. Redis4.0支持的新功能说明
  6. day-16 CNN卷积神经网络算法之Max pooling池化操作学习
  7. Matlab提速方法
  8. Cortex-M3(NXP LPC 1788) 启动代码
  9. Python中函数的参数-arguments
  10. eg_3