HDOJ4109拓扑OR差分约束求关键路径
Posted MekakuCityActor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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.
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.
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
题目大意:给出工作的先后顺序,求最短时间。
题目分析:典型的关键路径问题。可以使用拓扑求关键路径解决。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 const int maxn = 1e3 + 5; 9 struct node 10 { 11 int to, w; 12 node(){} 13 node(int tt, int ww) : to(tt), w(ww){} 14 }; 15 vector<node> v[maxn]; 16 int e[maxn], deg[maxn], n, m, x, y, z; 17 void TOP() 18 { 19 queue<int> q; 20 for(int i = 0; i < n; i++) 21 if(!deg[i]) 22 q.push(i), e[i] = 1; 23 while(!q.empty()) 24 { 25 int u = q.front(); 26 q.pop(); 27 for(int i = 0; i < v[u].size(); i++) 28 { 29 int to = v[u][i].to, w = v[u][i].w; 30 if(e[to] < e[u]+w) 31 e[to] = e[u]+w; 32 if(--deg[to] == 0) 33 q.push(to); 34 } 35 } 36 } 37 int main() 38 { 39 while(~scanf("%d%d", &n, &m)) 40 { 41 memset(deg, 0, sizeof(deg)); 42 memset(e, 0, sizeof(e)); 43 for(int i = 0; i < maxn; i++) 44 v[i].clear(); 45 for(int i = 1; i <= m; i++) 46 { 47 scanf("%d%d%d", &x, &y, &z); 48 v[x].push_back(node(y, z)); 49 deg[y]++; 50 } 51 TOP(); 52 int ans = 0; 53 for(int i = 0; i < n; i++) 54 ans = max(ans, e[i]); 55 printf("%d\n", ans); 56 } 57 return 0; 58 }
也可以根据题意建立不等关系,利用差分约束解决
1)建立超级源点使之连通【必须进行】
2)建立超级汇点直接就能得到答案
【选择进行,这一步是利用dist【结束时间】- dist【活动I的开始时间】>= 1 来进行的,之后dist【汇点】就是答案,当然也可以不建立汇点而通过for循环遍历找到最大的dist】
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 struct edge{ 7 int to; 8 int len; 9 int next; 10 }qwq[11150]; 11 queue<int>pa; 12 int edge_cnt,head[1005],stk[1005],dist[1005]; 13 void add(int x,int y,int z) 14 { 15 qwq[edge_cnt].to=y; 16 qwq[edge_cnt].len=z; 17 qwq[edge_cnt].next=head[x]; 18 head[x]=edge_cnt++; 19 } 20 void spfa() 21 { 22 while(!pa.empty()) 23 { 24 pa.pop(); 25 } 26 pa.push(0); 27 stk[0]=1; 28 while(!pa.empty()) 29 { 30 int u=pa.front();pa.pop();stk[u]=0; 31 for(int i = head[u]; i != -1 ; i=qwq[i].next) 32 { 33 int v=qwq[i].to; 34 int llen=qwq[i].len; 35 if(dist[v]<llen+dist[u]) 36 { 37 dist[v]=llen+dist[u]; 38 if(!stk[v]) 39 { 40 stk[v]=1; 41 pa.push(v); 42 } 43 } 44 } 45 } 46 } 47 int main() 48 { 49 int n,m; 50 while(scanf("%d%d",&n,&m)==2) 51 { 52 memset(head,-1,sizeof(head)); 53 memset(dist,-1,sizeof(dist)); 54 memset(stk,0,sizeof(stk)); 55 dist[0]=0; 56 edge_cnt=0; 57 while(m--) 58 { 59 int a,b,c; 60 scanf("%d%d%d",&a,&b,&c); 61 add(a,b,c); 62 } 63 for(int i = 1 ; i <= n ;i++) 64 { 65 add(0,i,0); 66 } 67 spfa(); 68 int maxx=-1; 69 for(int i = 0 ; i <= n ; i++) 70 { 71 if(dist[i]>maxx) 72 { 73 maxx=dist[i]; 74 } 75 } 76 cout << maxx+1 << endl; 77 } 78 return 0; 79 }
以上是关于HDOJ4109拓扑OR差分约束求关键路径的主要内容,如果未能解决你的问题,请参考以下文章