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. Linux第八周作业

    一 理解编译链接的过程和ELF可执行文件格式 这张图说明了可执行程序的产生 大致过程为 .c文件汇编成汇编代码.asm, 然后再汇编成目标码.o, 然后链接成可执行文件a.out, 这时可执行文件就可 ...

  2. nyoj-0613-免费馅饼(dp)

    nyoj-0613-免费馅饼 G. 免费馅饼 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在 ...

  3. linux磁盘管理 磁盘查看操作

    df查看磁盘分区使用状况 df --显示磁盘分区使用状况 'l' 仅显示本地磁盘(默认) 'a' 显示所有文件系统的磁盘使用情况,包含比如/proc/ 'h' 以1024进制计算最合适的单位显示磁盘容 ...

  4. ECMAscript5中的map

    今天看到到这样一个问题: ["1", "2", "3"].map(parseInt) 执行结果是什么? 结果是[1,NAN,NAN],很出乎 ...

  5. Win10系列:JavaScript综合实例1

    上面几个小节讲解了使用HTML5和JavaScript语言开发Windows 应用商店应用时会用到的一些技术,本小节将前面介绍的知识融合在一起创建一个菜谱应用程序,帮助读者更进一步地理解和掌握这些知识 ...

  6. jsp 中文乱码

    解决jsp中文乱码问题的几个步骤 1 jsp页面设置        <%@ page language="java" contentType="text/html; ...

  7. bootstrapTable 学习使用

    Bootstrap离线API Bootstrap Table 离线API <input type="button" id="btn_searcher" v ...

  8. innerHTML和innerText的区别,以及select元素中怎么取出被选中的option。

    一.innerHTML和innerText的区别. 元素.innerHTML = 字符串,是将一对或一个标签所标识的内容全部替换为所赋予的字符串,如果字符串中有标签,浏览器将自动识别其中的标签. 元素 ...

  9. Qt 查询字符串数据

    (1)函数QString::startsWith(),判断某一个字符串是否以某个字符串开头:该函数具有两个参数,第一个参数制定了一个字符串,第二个参数指定是否大小写敏感,默认大小写敏感: eg: QS ...

  10. python点滴:判断字符串是否为合法json格式

    在一些情况下,我们需要判断字符串是否为合法json格式. 思路很简单:尝试对字符串使用json.loads(),如果不是合法json格式,则会抛出ValueError异常. 示例如下: import ...