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