【HDOJ4109】【拓扑OR差分约束求关键路径】
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
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.
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.
1 2 1
3 4 1
#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 ;
}
#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差分约束求关键路径】的更多相关文章
- BZOJ4383 [POI2015]Pustynia[线段树优化建边+拓扑排序+差分约束]
收获挺大的一道题. 这里的限制大小可以做差分约束,从$y\to x$连$1$,表示$y\le x-1$即$y<x$,然后跑最长路求解. 但是,如果这样每次$k+1$个小区间每个点都向$k$个断点 ...
- BZOJ 2330 糖果 差分约束求最小值
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2330 题目大意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果 ...
- bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ1275 Cashier Employment 二分、差分约束
传送门 题意太长 为了叙述方便,将题意中的$0$点看作$1$点,$23$点看做$24$点 考虑二分答案(其实从小到大枚举也是可以的) 设$x_i$是我们选的雇员第$i$小时开始工作的人数,$s_i$是 ...
- HDU3592(差分约束)
World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- P3275 [SCOI2011]糖果 && 差分约束(二)
学习完了差分约束是否有解, 现在我们学习求解最大解和最小解 首先我们回想一下是否有解的求解过程, 不难发现最后跑出来任意两点的最短路关系即为这两元素的最短路关系. 即: 最后的最短路蕴含了所有元素之间 ...
- 【拓扑排序或差分约束】Guess UVALive - 4255
题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...
- 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)
洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...
随机推荐
- Maven常见jar包依赖
<!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactI ...
- .clearfix:after(清除浮动)中各个属性及值详细解说
清除浮动.clearfix:after一词,从事web前端的朋友们对此不会陌生吧,下面为大家介绍的是.clearfix:after中用到的所有属性及值的含义,对此感兴趣的朋友可以参考下哈想,希望对大家 ...
- Five Great .NET Framework 4.5 Features (五大特性)
[译].Net 4.5 的五项强大新特性 本文原文:Five Great .NET Framework 4.5 Features译者:冰河魔法师 目录 介绍 特性一:async和await 特性二 ...
- DBProxy 项目全解
转载自:https://github.com/Meituan-Dianping/DBProxy/blob/master/doc/USER_GUIDE.md#2 1 总体信息 1.1 关于 ...
- struts2应用
1.处理表单数据 GreetingAction public class GreetingAction extends ActionSupport{ private String username; ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
- docker 安全性问题
最近项目组成员要在k8s中引入类似于docker --privileged 的功能.显示通过api查询在container和pod层面做了securityContext的设置. 但是没有起到效果.于是 ...
- transform子元素,绝对定位失效
公司项目需要上拉刷新功能, mui下拉刷新组件采用固定布局,无法触发浏览器自带的隐藏地址栏功能. 思路: touchmove事件监听程序中,判断滚动位置:上下顶点使用transform 移动最外层容器 ...
- TextRank算法
TextRank是一种用来做关键词提取的算法,也可以用于提取短语和自动摘要.因为TextRank是基于PageRank的,所以首先简要介绍下PageRank算法. (1)PageRank PageRa ...
- python中字符串的操作方法
python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...