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;
}

hdu4109 topsort的更多相关文章

  1. 拓扑排序(topsort)

    本文将从以下几个方面介绍拓扑排序: 拓扑排序的定义和前置条件 和离散数学中偏序/全序概念的联系 典型实现算法解的唯一性问题 Kahn算法 基于DFS的算法 实际例子 取材自以下材料: http://e ...

  2. POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)

    题目大意: 为了锻炼自己的儿子 Jiajia 和Wind 把自己的儿子带入到一个洞穴内,洞穴有n个房间,洞穴的道路是单向的. 每一次Wind 选择两个房间  x 和 y,   让他的儿子从一个房间走到 ...

  3. poj1094 topsort

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32275   Accepted: 11 ...

  4. POJ - 3249 Test for Job (DAG+topsort)

    Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...

  5. 拓扑排序 topsort详解

    1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { marg ...

  6. poj 3648 2-SAT建图+topsort输出结果

    其实2-SAT类型题目的类型比较明确,基本模型差不多是对于n组对称的点,通过给出的限制条件建图连边,然后通过缩点和判断冲突来解决问题.要注意的是在topsort输出结果的时候,缩点后建图需要反向连边, ...

  7. Luogu3119 草鉴定-Tarjan+Topsort

    Solution 简单的$Tarjan$题. 有大佬现成博客 就不写了 → 传送门 Code #include<cstdio> #include<cstring> #inclu ...

  8. 图论——topsort

    今天学习topsort,明天强联通分量.topsort是一种在DAG(有向无环图)中来制定顺序的方法,从入度为0开始一个一个编排顺序直至所有的边都有了顺序(或者形成了环)最后如果图中还剩下元素那一定是 ...

  9. 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)

    UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...

随机推荐

  1. 基于 CPython 解释器,为你深度解析为什么Python中整型不会溢出

    前言 本次分析基于 CPython 解释器,python3.x版本 在python2时代,整型有 int 类型和 long 长整型,长整型不存在溢出问题,即可以存放任意大小的整数.在python3后, ...

  2. 在deepin系统中制作桌面快捷方式

    在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...

  3. 【WXS数据类型】String

    属性: 名称 值类型 说明 [String].constructor [String] 返回值为“String”,表示类型的结构字符串 [String].length [Number] 返回该字符串的 ...

  4. Mount qcow2 image

    1.Mount a qcow2 image qemu-nbd - QEMU Disk Network Block Device Server: Export QEMU disk image using ...

  5. Elasticsearch 排序插件的开发

    直接观察到的几个问题 简单expression脚本的执行效率 > java 插件,10000条数据可以测试出1ms左右的差距. Es会不断调用newScript来创建"足够多" ...

  6. “hello world!”团队第三次会议

    团队“hello world!”团队召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 2017年10 ...

  7. DAY4敏捷冲刺

    站立式会议 工作安排 (1)服务器配置 已完成对微信小程序登录凭证储存至云端数据库,计划使用微信接口返回的session_id进行转化返回本地,以保持登录态. (2)数据库配置 单词学习记录+用户信息 ...

  8. 3ds Max学习日记(一)

      暑假闲来无事学习一发3ds Max.为啥要学这玩意?貌似可以用这东西三维建模.暑期生产实习选了一个搞vr的导师,貌似他忙得很,无奈只好先自己研究一下啦~   vr神马的还是有点意思的,虽然自己仅仅 ...

  9. PAT L1-048 矩阵A乘以B

    https://pintia.cn/problem-sets/994805046380707840/problems/994805082313310208 给定两个矩阵A和B,要求你计算它们的乘积矩阵 ...

  10. [计算机网络] DNS何时使用TCP协议,何时使用UDP协议

    DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下使用这两种协议. 先简单介绍下TCP与UDP. ...