C++ version of PTA key activities——Shandong University of Science and Technology

Question:
Assume that an engineering project consists of a group of subtasks, some of which can be executed in parallel, and some must be executed after completing other subtasks. "Task scheduling" includes a set of subtasks and the set of subtasks on which each subtask can be executed.

For example, completing all courses and graduation design of a major can be regarded as a project to be completed by an undergraduate, and each course can be regarded as a subtask. Some courses can be offered at the same time, such as English and C programming, and there is no restriction on which one must be taken first; some courses cannot be offered at the same time, because they have a sequential dependency, such as C programming and data structure. The former must be learned first.

But it should be noted that for a group of subtasks, not arbitrary task scheduling is a feasible solution. For example, if there is "subtask A depends on subtask B, subtask B depends on subtask C, and subtask C depends on subtask A" in the plan, then none of these three tasks can be executed first, which is an infeasibility scheme.

In the task scheduling problem, if the time required to complete each subtask is also given, we can calculate the shortest time required to complete the entire project. Among these subtasks, even if some tasks are delayed for a few days, the overall duration will not be affected; but some tasks must be completed on time, otherwise the duration of the entire project will be delayed. This kind of task is called "key activity".

Please write a program to determine whether the task scheduling of a given engineering project is feasible; if the scheduling scheme is feasible, calculate the shortest time required to complete the entire engineering project, and output all key activities.
Input format:

Input the first line to give two positive integers N (≤100) and M, where N is the task handover point (that is, the node connecting two interdependent subtasks, for example: if task 2 starts after task 1 is completed, Then there must be a handover point between the two tasks). The handover points are numbered by 1 N, and M is the number of subtasks, which are sequentially numbered 1 M. Followed by M lines, each line gives 3 positive integers, which are the number of the handover point involved in the start and completion of the task and the time required for the task, and the integers are separated by spaces.
Output format:

If the task scheduling is not feasible, output 0; otherwise, the first line outputs the time required to complete the entire project, and the second line starts to output all key activities, each key activity occupies one line, and outputs according to the format "V->W", where V and W number the handover points involved in the start and completion of the task. The order rule of the key activity output is: the smaller the number of the handover point at the beginning of the task takes precedence, and when the start number is the same, the order of the tasks at the time of input is reversed.
Input sample:

7 8
1 2 4
1 3 3
2 4 5
3 4 3
4 5 1
4 6 6
5 7 5
6 7 2

Sample output:

17
1->2
2->4
4->6
6->7
#include<bits/stdc++.h>
#define MAX 205
using namespace std;
struct NODE
{
    
    
    int in;
    int out;
    int numto,numfrom;
    int to[MAX],from[MAX];
} node[MAX];
struct EDGE
{
    
    
    int from;
    int to;
    int num;
    int earlytime;
    int latetime;
} edge[MAX*MAX];
int m,n;
int vertex_early[MAX],vertex_late[MAX];
int graph[MAX][MAX];
int vertex[MAX];
int edgeinfo[MAX][MAX];
int edgenum=0;
bool cmp(EDGE a,EDGE b)
{
    
    
    if(a.from==b.from)
    {
    
    
        return a.num>b.num;
    }
    return a.from<b.from;
}
bool get_vertex_late(int index)
{
    
    
    fill(vertex_late,vertex_late+MAX,INT_MAX);
    vertex_late[index]=vertex_early[index];
    queue<int> q;
    bool visnode[MAX];
    fill(visnode,visnode+MAX,false);
    q.push(index);
    int x,cnt;
    while(!q.empty())
    {
    
    

        x=q.front();
        q.pop();
        for(int i=0; i<node[x].numfrom; i++)
        {
    
    
            cnt=node[x].from[i];
            vertex_late[cnt]=min(vertex_late[x]-graph[cnt][x],vertex_late[cnt]);
            if(vertex_late[x]-vertex_early[cnt]==graph[cnt][x])
            {
    
    
                edge[edgenum].from=cnt;
                edge[edgenum].to=x;
                edge[edgenum].num=edgeinfo[cnt][x];
                edgenum++;
                if(!visnode[cnt])
                {
    
    
                    q.push(cnt);
                    visnode[cnt]=true;
                }
            }
        }
    }
}
bool topsort()
{
    
    
    queue<int> q;
    for(int i=1; i<=n; i++)
    {
    
    
        if(node[i].in==0)
        {
    
    
            q.push(i);
            vertex_early[i]=0;
        }
    }
    int x,cnt,num=0,sum=0;
    while(!q.empty())
    {
    
    
        x=q.front();
        q.pop();
        vertex[num++]=x;
        sum++;
        for(int i=0; i<node[x].numto; i++)
        {
    
    
            cnt=node[x].to[i];
            vertex_early[cnt]=max(vertex_early[cnt],vertex_early[x]+graph[x][cnt]);
            node[cnt].in--;
            if(node[cnt].in==0)
                q.push(cnt);
        }
    }
    if(sum!=n)
        return false;
    int maxn=0,index=0;
    for(int i=1; i<=n; i++)
    {
    
    
        if(maxn<vertex_early[i])
        {
    
    
            maxn=vertex_early[i];
            index=i;
        }
    }
    cout<<maxn<<endl;
    get_vertex_late(index);
    return true;
}
void solve()
{
    
    
    //cout<<edgenum;
    sort(edge,edge+edgenum,cmp);
    for(int i=0;i<edgenum;i++)
        cout<<edge[i].from<<"->"<<edge[i].to<<endl;
}
int main()
{
    
    
    //freopen("in.txt","r",stdin);
    cin>>n>>m;
    int a,b,l;
    for(int i=0; i<m; i++)
    {
    
    
        cin>>a>>b>>l;
        graph[a][b]=l;
        node[a].out++;
        node[b].in++;
        node[a].to[node[a].numto++]=b;
        node[b].from[node[b].numfrom++]=a;
        edgeinfo[a][b]=i;
    }
    if(!topsort())
        cout<<0<<endl;
    else
        solve();
}

For more PTA codes, please refer to my blog

ps: The code is for reference only, please do not plagiarize

Guess you like

Origin blog.csdn.net/scorpion_legend/article/details/109540551