数据结构3

7月7号下午做完7-2 关键路径

#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
#define maxx 200
#define INF 10000
int edges[maxx][maxx];
int in[maxx] = { 0 };
int out[maxx] = { 0 };
int early[maxx] = { 0 };
int late[maxx];
int max1[1]={0};
int s[maxx]={0};
int ne, nv;
int l=0;
int temp1=0;
int earlytime()
{
    int cnt = 0;
    queue<int>qu;
    for (int i = 1; i <= nv; i++)
    {
        if (in[i] == 0)
            qu.push(i);//找到入度为零的节点,就是起点
    }
    while (!qu.empty())
    {
        int temp = qu.front();
        cnt++;
        qu.pop();
        for (int i = 1; i <= nv; i++)
        {
            if (edges[temp][i] != INF)
            {
                in[i]--;

                early[i] = max(early[i], early[temp] + edges[temp][i]);
                if (in[i] == 0)qu.push(i);
            }
        
        }
    
    }
    if (cnt != nv)
        return -1;//当无法完成任务
    else
    {
        int amount = -999;
        for (int i = 1; i <= nv; i++)
        {
            if (early[i] > amount)
                amount = early[i];
        }
        return amount;
    }


}

void latetime(int count)
{
    queue<int>qu;
    for (int i = 1; i <= nv; i++)
    {
        if (out[i] == 0)
        {
            qu.push(i);
            late[i] = count;
        }
    }
    while (!qu.empty())
    {
        int temp = qu.front();
        qu.pop();
        for (int i = nv; i >=1; i--)
        {
            if (edges[i][temp] != INF)//edges[i][temp] 
            {
                out[i]--;
                late[i] = min(late[i], late[temp] - edges[i][temp]);

                if (out[i] == 0)
                    qu.push(i);
            }
        }
    }




}

int main()
{
    int n, m;
    while(cin >> n >> m){
        nv = n;
        ne = m;
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        memset(early,0,sizeof early);
        memset(late,0,sizeof late);
        memset(s,0,sizeof s);
        
        for (int i = 0; i < maxx; i++)
        {
            late[i] = INF;
            for (int j = 0; j < maxx; j++)
            {
                edges[i][j] = INF;
                
            }
        }
    
        for (int i = 1; i <= ne; i++)
        {
            int a, b, c;
            cin >> a >> b >> c;
            edges[a][b] = c;
            in[b]++;
            out[a]++;
        }
        int count = earlytime();
        if (count == -1)
        {
            cout << "unworkable project"<<endl;
            continue;
        }
        latetime(count);
        cout << count << endl;
        for (int i = 1; i <= nv; i++)//从小到大保证任务开始的交接点编号从小的开始
        {
            for (int j = nv; j >= 1; j--)//从大到小保证起点编号相同时,与输入时任务的顺序相反。
            {
                if (edges[i][j] != INF&&late[j] - early[i]==edges[i][j])
                { s[2*l]=i;
                    s[2*l+1]=j;
                    ++l;
                    
                    max1[0]++;
                    
                }
    
            }
        }
      
      for(int k =0;k<max1[0];k++){
          if(s[2*k]==s[2*k+2]){
              if(s[2*k+1]>s[2*k+3]){
                  temp1=s[2*k+1];
                  s[2*k+1]=s[2*k+3];
                  s[2*k+3]=temp1;
              }
          }
          
         
      }
         for(int k=0;k<max1[0];k++){
              cout<<s[2*k]<<"->"<<s[2*k+1]<<endl;
          }
    }
}

 

posted @ 2022-07-08 11:18  清梦韶华  阅读(30)  评论(0编辑  收藏  举报