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 ;
}

最新文章

  1. Samba文件服务器详细配置步骤
  2. IOS 本地通知推送消息
  3. Python程序员的进化史
  4. C++强制类型转换操作符 static_cast
  5. Linux 网络编程九(select应用--大并发处理)
  6. NET中MSMQ的使用----附例子
  7. 获取Ad用户信息
  8. php入门变量之字符串
  9. procps工具集 ----Linux中的可用内存指的是什么?
  10. CTO这点事(技术,业务,管理,情商,周期,趋势)转
  11. 用Ultraiso刻录U盘装系统
  12. Swift 2.0 UItableView 的简单使用
  13. [补档]暑假集训D8总结
  14. 网络编程之TCP编程
  15. curl_multi实现并发
  16. Python简单爬虫获取岗位招聘人数
  17. 关于getdate()的不同的日期格式
  18. 函数式编程语言(Functional Program Language)
  19. 使用pace监控ajax踩过的坑
  20. Java对称与非对称加密解密,AES与RSA

热门文章

  1. laravel中判断当前页面与连接地址是否一致,并添加效果:
  2. mac mamp环境 和linux下 安装redis 和可视化工具 Redis Desktop Manager
  3. oracle sqlserver mysql 通过sql查看表及字段注释
  4. C# 表达式树学习笔记
  5. [Leetcode 392]判断子序列 Is Subsequence
  6. Java垃圾回收理解
  7. 二、求水仙花数,打印出100-999之间所有的&quot;水仙花数&quot;
  8. Android开发 ---xml布局元素
  9. tf之变量与作用域
  10. C++四种类型之间的转换