HDU4109 instrction arrangement topological sort key path

Source: Internet
Author: User


Problem Descriptionali have taken the computer Organization and Architecture course this term. He learned that there is dependence between instructions, like the WAR (write after Read), WAW, RAW.
If the distance between and instructions are less than the Safe distance, it'll result in hazard, which may cause wrong r Esult. So we need to design special circuit to eliminate hazard. However the most simple-to-solve-problem is-to-add bubbles (useless operation), which means wasting time to Ensur E that the distance between and instructions are not smaller than the Safe distance.
The definition of the distance between and 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 has a very strong CPU with infinite number of cores, so 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, the CPU can finish all the instructions using minimum time.
Inputthe input consists several testcases.
The first line has both integers n, m (n <=, M <= 10000), means that there is N instructions and M dependent re Lations.
The following M lines, each contains three integers x, y, Z, means the Safe Distance between X and Y are Z, and y should r Un after X. The instructions is numbered from 0 to N-1.

Outputprint one integer, the minimum time the CPU needs to run.

Sample Input
5 21 2 13 4 1


Sample Output
2Hintin the 1st NS, instruction 0, 1 and 3 is executed;in the 2nd NS, instruction 2 and 4 are executed. So the answer should is 2.

Test Instructions:
It can be understood that there are M engineering and n production lines, each production line will take a certain amount of time, each project begins with the premise of connecting its production line is complete. Multiple production lines can be operated simultaneously, the earliest time for all projects to be completed (the project begins immediately).

Ideas:
The topology sorts the points for each search-in degree of 0, calculates the end time of all projects in turn, and the maximum time is the earliest.
Here, the adjacency table is used to store all the edges associated with each point.

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector > #include <algorithm>using namespace std;struct node{int to,value;};      vector<node>next[1005];          Vector + struct adjacency table int in_degree[1005];            The degree of the entry of the node int finish[1005];    Start time of the node int m,n;void topsort () {queue<int>q;    int i,j;    int now;       for (i=0;i<m;i++) if (in_degree[i]==0) {finish[i]=1;            Test Instructions Q.push (i);      } while (!q.empty ()) {Now=q.front ();        cout<<now<<endl;        Q.pop ();            For (I=0;i<next[now].size (); i++) {int b=next[now][i].to;            int v=next[now][i].value;     Finish[b]=max (FINISH[B],FINISH[NOW]+V);            The start time of a project is in_degree[b]--;        if (in_degree[b]==0) Q.push (b); }} return;    int main () {int i,j; while (~SCANF ("%d%d", &m,&n)) {memSet (finish,0,sizeof (finish));        memset (in_degree,0,sizeof (In_degree));        for (i=0; i<=m; i++) next[i].clear ();        int a,b,c;        Node D;            while (n--) {scanf ("%d%d%d", &a,&b,&c);            D.to=b;            D.value=c;            Next[a].push_back (d);        in_degree[b]++;        } topsort ();        int ans=0;            for (i=0;i<=m;i++) if (Finish[i]>ans) ans=finish[i];    cout<<ans<<endl; } return 0;}


HDU4109 instrction arrangement topological sort key path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.