http://acm.hdu.edu.cn/showproblem.php?pid=4109

Instrction Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2196    Accepted Submission(s): 900

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
题目大意:给出工作的先后顺序,求最短时间。
题目分析:典型的关键路径问题。可以使用拓扑求关键路径解决。
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 1e3 + ;
struct node
{
int to, w;
node(){}
node(int tt, int ww) : to(tt), w(ww){}
};
vector<node> v[maxn];
int e[maxn], deg[maxn], n, m, x, y, z;
void TOP()
{
queue<int> q;
for(int i = ; i < n; i++)
if(!deg[i])
q.push(i), e[i] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = ; i < v[u].size(); i++)
{
int to = v[u][i].to, w = v[u][i].w;
if(e[to] < e[u]+w)
e[to] = e[u]+w;
if(--deg[to] == )
q.push(to);
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
memset(deg, , sizeof(deg));
memset(e, , sizeof(e));
for(int i = ; i < maxn; i++)
v[i].clear();
for(int i = ; i <= m; i++)
{
scanf("%d%d%d", &x, &y, &z);
v[x].push_back(node(y, z));
deg[y]++;
}
TOP();
int ans = ;
for(int i = ; i < n; i++)
ans = max(ans, e[i]);
printf("%d\n", ans);
}
return ;
}
也可以根据题意建立不等关系,利用差分约束解决
1)建立超级源点使之连通【必须进行】
2)建立超级汇点直接就能得到答案
【选择进行,这一步是利用dist【结束时间】- dist【活动I的开始时间】>= 1 来进行的,之后dist【汇点】就是答案,当然也可以不建立汇点而通过for循环遍历找到最大的dist】
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct edge{
int to;
int len;
int next;
}qwq[];
queue<int>pa;
int edge_cnt,head[],stk[],dist[];
void add(int x,int y,int z)
{
qwq[edge_cnt].to=y;
qwq[edge_cnt].len=z;
qwq[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
void spfa()
{
while(!pa.empty())
{
pa.pop();
}
pa.push();
stk[]=;
while(!pa.empty())
{
int u=pa.front();pa.pop();stk[u]=;
for(int i = head[u]; i != - ; i=qwq[i].next)
{
int v=qwq[i].to;
int llen=qwq[i].len;
if(dist[v]<llen+dist[u])
{
dist[v]=llen+dist[u];
if(!stk[v])
{
stk[v]=;
pa.push(v);
}
}
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
memset(head,-,sizeof(head));
memset(dist,-,sizeof(dist));
memset(stk,,sizeof(stk));
dist[]=;
edge_cnt=;
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
for(int i = ; i <= n ;i++)
{
add(,i,);
}
spfa();
int maxx=-;
for(int i = ; i <= n ; i++)
{
if(dist[i]>maxx)
{
maxx=dist[i];
}
}
cout << maxx+ << endl;
}
return ;
}

【HDOJ4109】【拓扑OR差分约束求关键路径】的更多相关文章

  1. BZOJ4383 [POI2015]Pustynia[线段树优化建边+拓扑排序+差分约束]

    收获挺大的一道题. 这里的限制大小可以做差分约束,从$y\to x$连$1$,表示$y\le x-1$即$y<x$,然后跑最长路求解. 但是,如果这样每次$k+1$个小区间每个点都向$k$个断点 ...

  2. BZOJ 2330 糖果 差分约束求最小值

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2330 题目大意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果 ...

  3. bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...

  4. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  5. POJ1275 Cashier Employment 二分、差分约束

    传送门 题意太长 为了叙述方便,将题意中的$0$点看作$1$点,$23$点看做$24$点 考虑二分答案(其实从小到大枚举也是可以的) 设$x_i$是我们选的雇员第$i$小时开始工作的人数,$s_i$是 ...

  6. HDU3592(差分约束)

    World Exhibition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. P3275 [SCOI2011]糖果 && 差分约束(二)

    学习完了差分约束是否有解, 现在我们学习求解最大解和最小解 首先我们回想一下是否有解的求解过程, 不难发现最后跑出来任意两点的最短路关系即为这两元素的最短路关系. 即: 最后的最短路蕴含了所有元素之间 ...

  8. 【拓扑排序或差分约束】Guess UVALive - 4255

    题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...

  9. 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)

    洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...

随机推荐

  1. laravel中对模型和路由做缓存,提高性能

    模型缓存命令: php think optimize:schema 路由缓存命令: php think optimize:route

  2. day28 黏包及黏包解决方案

    今日主要内容: 一 .缓冲区 二.两种黏包现象 三.黏包现象的两种解决方案 四.打印进度条(补充的,了解即可) 1. 缓冲区 缓冲区的作用 : 将程序和网络解耦(这样做的好处是程序不会以为网速的快慢而 ...

  3. Mybatis的二级缓存注意点

    --声明:一下内容都不一定是正确的,只是自己测试的结果,请自己的动手操作得出自己的结论 1.开启Mybatis的二级缓存,不仅要在SqlMapConfig.xml中进行开启总开关,还要在对应的XXXM ...

  4. SpringBoot + JPA 连接MySQL完整实例(一)

    开发工具 1.Eclipse 2.Maven 3.Spring Boot 首先,Eclipse中配置好maven,具体请百度 工程结构: 实现步骤: 1.Eclipse中新建一个maven proje ...

  5. Linux查看当前使用的网卡 以及 查看某进程使用的网络带宽情况 以及 端口占用的情况

    一:Linux查看当前使用的网卡          ifconfig命令可以查看当前linux 系统有多少个网卡. [app@p2-app2 ~]$ ifconfig br-2e5b046a02d5: ...

  6. 从mysql读取数据写入mongo

    # coding:utf-8 # Created by qinlin.liu at 2017/3/14 import pymysql import datetime #pymongo说明文档  : h ...

  7. 学习python二三事儿(二)

    多个变量赋值 Python允许你同时为多个变量赋值.例如: a = b = c = 1 以上实例,创建一个整型对象,值为1,三个变量被分配到相同的内存空间上. 您也可以为多个对象指定多个变量.例如: ...

  8. DevExpress ASP.NET v18.2新功能详解(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...

  9. 查看电脑的IP地址及配置

    自己主机的IP地址查看cmd----ipconfig/all,如下图

  10. Redis部署与基本操作

    1.安装 1)不指定安装位置,则会把redis的可执行文件安装到  redis-2.8.6/src/目录下 [root@CentOS6 ~]# ls anaconda-ks.cfg  httpd-2. ...