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. POJ 1006 生理周期(中国剩余定理)

    POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...

  2. Win10系列:UWP界面布局进阶3

    与以往的Windows操作系统不同,Windows 10操作系统在正式版当中取消了任务栏中的"开始"按钮,将大部分的应用程序图标放置在开始屏中,同时将系统设置等常用功能整合到了Ch ...

  3. vue-2-计算属性和观察者

    <div id="example"> <p>Original message: "{{ message }}"</p> &l ...

  4. 《Python》进程之间的通信(IPC)、进程之间的数据共享、进程池

    一.进程间通信---队列和管道(multiprocess.Queue.multiprocess.Pipe) 进程间通信:IPC(inter-Process Communication) 1.队列 概念 ...

  5. 3.1 C++继承的概念及语法

    参考:http://www.weixueyuan.net/view/6358.html. 总结: 继承可以理解为一个类从另一个类获取方法(函数)和属性(成员变量)的过程. 被继承的类称为父类或基类,继 ...

  6. loadrunner http协议性能测试脚本编写

    性能测试其实测的就是接口的性能,不管是用工具录制还是自己写,都是围绕接口的,录制也是把接口录制下来而已,但是录制下来的脚本比较乱,会把很多相关的请求都录下来. 在这里我们手动写HTTP协议的get.p ...

  7. Centos7单主机部署 LAMP + phpmyadmin 服务

    LAMP -> centos + apache + mysql + php + phpmyadmin 一:搭建yum仓库: 安装utils: yum -y install yum-utils c ...

  8. LVS+OSPF+FULLNAT集群架构

    OSPF:OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(aut ...

  9. ubuntu安装nodejs,npm live-server

    sudo apt-get install curl 先安装的是curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/in ...

  10. django做redis缓存

    django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { "BACKEND": ...