ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Instrction Arrangement UDH 4109 拓扑排序 or 最长路

2020-05-01 18:57:16  阅读:275  来源: 互联网

标签:int tot Arrangement Instrction between 4109 include dp instructions


题目描述

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

Hint

In the 1st ns, instruction 0, 1 and 3 are executed;
In the 2nd ns, instruction 2 and 4 are executed.
So the answer should be 2.

分析

这道题至少有两种方法

拓扑排序

第一种方法就是拓扑排序
显然没有任何约束的指令可以在第一秒同时执行。
对有一个或多个约束指令我们要满足最远的那个约束之后
定义dp[i],表示执行指令i的最早时间,则有:dp[i]=max(dp[i],dp[j]+a[j][i]),a[j][i]表示i必须在j执行后a[j][i]秒后执行。
临界没有任何约束的指令在第一秒时执行,dp[]=1
阶段很明显,当前入度为0点,下个阶段为这些点的临界点。

代码

#include <bits/stdc++.h>
const int maxn=1000+5,maxm=1e4+5;
struct Node{int to;int dis;int next;}e[maxm];
int n,m,len,head[maxn],rd[maxn],dp[maxn];
void Insert(int x,int y,int z){
    e[++len].to=y;e[len].dis=z;e[len].next=head[x];head[x]=len;
}
void Kahn(){
    std::stack<int> q;
    for(int i=0;i<n;++i){
        if(!rd[i])q.push(i),dp[i]=1;
        else dp[i]=0;
    }
    int ans=1; 
    while(!q.empty()){
        int u=q.top();q.pop();
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to,w=e[i].dis;rd[v]--;
            dp[v]=std::max(dp[v],dp[u]+w);
			ans=std::max(ans,dp[v]); 
            if(!rd[v])q.push(v);
        }
    }
    printf("%d\n",ans);
}
void Solve(){
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(head,0,sizeof(head));
		memset(rd,0,sizeof(rd));
		len=0;
		for(int i=1;i<=m;++i){
        	int x,y,z;scanf("%d%d%d",&x,&y,&z);
        	Insert(x,y,z);rd[y]++;
    	}    
    	Kahn();
	}    
}
int main(){
    Solve();
    return 0;
}

最长路

其实细细思考一下,建一个超级源点跑最长路也是可以的

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int maxd=1005,maxb=20005;
int ru[maxd],chu[maxd];
int head[maxd],tot=1;
int n,m;
struct asd{
    int from,to,next,val;
}b[maxb];
void ad(int aa,int bb,int cc){
    b[tot].from=aa;
    b[tot].to=bb;
    b[tot].next=head[aa];
    b[tot].val=cc;
    head[aa]=tot++;
}
bool vis[maxd];
int dis[maxd];
void SPFA(){
    queue<int> q;
    q.push(n);
    for(int i=0;i<maxd;i++){
		dis[i]=-0x3f3f3f3f;
	}
    dis[n]=0;
    while(!q.empty()){
        int xx=q.front();
        q.pop();
        vis[xx]=0;
        for(int i=head[xx];i!=-1;i=b[i].next){
            int u=b[i].to;
            if(dis[u]<dis[xx]+b[i].val){
                dis[u]=dis[xx]+b[i].val;
                if(!vis[u])q.push(u),vis[u]=1;
            }
        }
    }
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    memset(&b,0,sizeof(struct asd));
    memset(ru,0,sizeof(ru));
    memset(chu,0,sizeof(chu));
    tot=1;
    for(int i=1;i<=m;i++){
        int aa,bb,cc;
        scanf("%d%d%d",&aa,&bb,&cc);
        ad(aa,bb,cc);
        ru[bb]++;
        chu[aa]++;
    }
    for(int i=0;i<n;i++){
        if(ru[i]==0){
            ad(n,i,1);
        }
    }
    SPFA();
    int ans=1;
    for(int i=0;i<n;i++){
        ans=max(ans,dis[i]);
    }
    printf("%d\n",ans);
	}
    return 0;
}

标签:int,tot,Arrangement,Instrction,between,4109,include,dp,instructions
来源: https://www.cnblogs.com/liuchanglc/p/12814319.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有