hdu4109拓扑Instrction Arrangement
来源:互联网 发布:孙宏斌的钱哪来的知乎 编辑:程序博客网 时间:2024/06/05 07:55
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.
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
如果要完成y事件,则要先完成x事件,多个事件可同时进行;
样例:
先完成 0 1 3再完成2 4,时间为2
拓扑排序,找关键路径;
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[1010][1010],dis[1010],topo[1010],in[1010];
int t;
int ma(int x,int y)
{
return x>y?x:y;
}
void bfs(int n)
{
int x=0;
t=0;
while(x<n)
{
for(int i=0;i<n;i++)
{
if(in[i]==0)
{
x++;
in[i]--;
int flag=0;//判断是否排序到最后
for(int j=0;j<n;j++)
{
if(a[i][j]!=0)
{
in[j]--;
flag=1;
dis[j]=ma(dis[j],dis[i]+a[i][j]);
}
}
if(flag==0)
topo[t++]=dis[i]+1;//dis[i]为距离,而题目要求的是点的个数
break;
}
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(dis,0,sizeof(dis));
memset(topo,0,sizeof(topo));
memset(a,0,sizeof(a));
memset(in,0,sizeof(in));
int x,y,z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
if(a[x][y]==0)
{
a[x][y]=z;
in[y]++;
}
}
bfs(n);
int s=0;
for(int i=0;i<t;i++)
s=ma(topo[i],s);
printf("%d\n",s);
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int a[1010][1010],dis[1010],topo[1010],in[1010];
int t;
int ma(int x,int y)
{
return x>y?x:y;
}
void bfs(int n)
{
int x=0;
t=0;
while(x<n)
{
for(int i=0;i<n;i++)
{
if(in[i]==0)
{
x++;
in[i]--;
int flag=0;//判断是否排序到最后
for(int j=0;j<n;j++)
{
if(a[i][j]!=0)
{
in[j]--;
flag=1;
dis[j]=ma(dis[j],dis[i]+a[i][j]);
}
}
if(flag==0)
topo[t++]=dis[i]+1;//dis[i]为距离,而题目要求的是点的个数
break;
}
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(dis,0,sizeof(dis));
memset(topo,0,sizeof(topo));
memset(a,0,sizeof(a));
memset(in,0,sizeof(in));
int x,y,z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
if(a[x][y]==0)
{
a[x][y]=z;
in[y]++;
}
}
bfs(n);
int s=0;
for(int i=0;i<t;i++)
s=ma(topo[i],s);
printf("%d\n",s);
}
return 0;
}
阅读全文
0 0
- hdu4109拓扑Instrction Arrangement
- HDU4109 Instrction Arrangement 拓扑排序 关键路径
- HDU4109 Instrction Arrangement @Z
- HDU4109 Instrction Arrangement 拓扑排序求关键路径
- HDU4109 Instrction Arrangement【差分约束】【拓扑排序】
- HDU4109——Instrction Arrangement(关键路径)
- HDU_4109 Instrction Arrangement(拓扑)
- Instrction Arrangement
- hdu 4109 Instrction Arrangement 拓扑排序 关键路径
- hdu 4109 Instrction Arrangement 拓扑排序 关键路径
- HDU 4109 Instrction Arrangement拓扑排序 关键路径模板
- hdu 4109 Instrction Arrangement 拓扑排序/觉得更像BFS
- HDU 4109 Instrction Arrangement(拓扑排序+bfs)
- hdu4109拓扑
- HDU 4109 Instrction Arrangement
- Hdu 4109 Instrction Arrangement
- HDU 4109 Instrction Arrangement
- HDU 4109 Instrction Arrangement
- 请实现一个函数用来找出字符流中第一个只出现一次的字符
- NYQJ 520 最大素因子
- solr项目中添加elasticsearch5.4.0记录
- springboot学习笔记4( 前后台分离,ajax跨域问题)
- caioj.1037 搜索初步
- hdu4109拓扑Instrction Arrangement
- bootscript部署到服务器后图标不显示
- 求一个集合所有子集的Python实现
- Pthread线程
- PAT团体程序设计天梯赛GPLT题解目录
- 独立思考的重要性
- Java实现一直二叉树前序和中序,还原二叉树
- 最大公约数模板
- 【新坑已填】后缀数组总结篇