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. Java开发工程师(Web方向) - 02.Servlet技术 - 第2章.Cookie与Session

    第2章--Cookie与Session Cookie与Session 浏览器输入地址--HTTP请求--Servlet--HTTP响应--浏览器接收 会话(session):打开浏览器,打开一系列页面 ...

  2. python爬虫基础之一(爬淘宝)

    没想到python如此强大, 今天看一会视频学会了一段python爬虫 这就是我今天学到的内容爬去淘宝网关于书包的一些信息,包括价格, #coding=utf-8 import requests#导入 ...

  3. C++错误:Process returned -1073741571 (0xC00000FD)

    最近写程序时,需要将一个一维数组编程二维数组,很简单,写完之后,运行错误! 提示:Process returned -1073741571 (0xC00000FD) 刚开始写的代码如下: #inclu ...

  4. 数据库Mysql的学习(八)-储存过程和事务和导入导出

    储存过程 DELIMITER // CREATE PROCEDURE pro1() BEGIN SELECT book_id,book_name,category FROM bookinfo t1 J ...

  5. /etc/fstab 文件如何填写(转)

    转载自 http://hi.baidu.com/jingzhongchen/blog/item/8e6f552dcead7ce98b139952.html 看你对/etc/fstab文件了解多少?   ...

  6. Python—集合(在我的世界,你就是唯一)

    一.概念与定义 集合类型与数学中集合的概念一致,即包含0个或多个数据项的无序组合. 元素不可重复,只能是固定数据类型元素. 集合(set)属于Python无序可变序列,使用一对大括号作为定界符,元素之 ...

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

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

  8. <Android>对话框的使用

    Android系统提供四种对话框:警告对话框(AlertDialog),进度对话框(ProgressDialog),日期选择对话框(DatePickerDialog)和时间选择对话框(TimePick ...

  9. 第三部分shell编程3(shell脚本2)

    7. if 判断一些特殊用法 if [ -z $a ] 这个表示当变量a的值为空时会怎么样if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样if ...

  10. QT分析之QApplication的初始化

    原文地址:http://blog.163.com/net_worm/blog/static/1277024192010097430321/ 在开始分析之前交代一下,一是分析的QT在Window平台实现 ...