zoukankan      html  css  js  c++  java
  • hdu4109 topsort

    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
     
    拓扑排序
    还是多线程的
    题意:有n个指令m个要求     例如 X Y Z 代表 指令Y必须在指令X后 Z秒执行 输出cpu运行的最小时间
     运行最小时间 也就是要满足最大的时间要求
      vector<node>存图 node 结构体包含 mubiao timm
      入读为零的点 tim[] 初始化为1
       tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm); //确保满足最大时间要求
     
     
    #include<bits/stdc++.h>
    using namespace std;
    int n,m;
    int g;
    struct node
    {
        int mubiao;
        int timm;
    }gg[10005];
    vector<node> mp[1005];
    int tim[1005];
    int in[1005];
    queue<int>q;
    int max(int ss,int bb)
    {
        if(ss>bb)
            return ss;
        return bb;
    }
    //struct node gg;
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                mp[i].clear();
                in[i]=0;
            }
            memset(tim,0,sizeof(tim));
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&g,&gg[i].mubiao,&gg[i].timm);
                mp[g].push_back(gg[i]);
                in[gg[i].mubiao]++;
            }
            for(int i=0;i<n;i++)
            {
                if(in[i]==0)
                   {
                     q.push(i);
                     tim[i]=1;
                    }
            }
            int re=0;
            while(!q.empty())
            {
                int j=q.front();
                q.pop();
                int temp=tim[j];
                if(re<=temp)
                    re=temp;
                for(unsigned int i=0;i<mp[j].size();i++)
                {
                tim[mp[j][i].mubiao]=max(tim[mp[j][i].mubiao],temp+mp[j][i].timm);
                    if(--in[mp[j][i].mubiao]==0)
                    {
                        q.push(mp[j][i].mubiao);
                    }
                }
            }
            printf("%d
    ",re);
        }
       return 0;
    }
    
  • 相关阅读:
    【luogu P7112】【模板】行列式求值(数学)(线性代数)(高斯消元)
    【luogu P3506】MOT-Monotonicity 2(线段树)
    【luogu AT5160】Numbers on a Circle(贪心)(堆)
    【luogu AT5159】RGB Balls(贪心)
    【luogu P5435】基于值域预处理的快速 GCD(数学)
    Day-20 基础模块1
    F5学习笔记
    Day-19 面向对象06 多继承 MRO C3算法 super()(重点)
    Day-18 面向对象05 约束、异常处理、日志处理
    MPLS(多协议卷标交换)
  • 原文地址:https://www.cnblogs.com/hsd-/p/4931655.html
Copyright © 2011-2022 走看看