关键路径


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct{
  int vexs[10];
  int edges[10][10];
  int n;
  int e;
}MGraph;

#define INFINITE 2048

void CreateGraphM(MGraph *G){

  int N1,N2;
  int i,j,k;

  cout<<"Enter the number of vertexs and edges: "<<endl;
  cin>>(G->n)>>(G->e);
  k=G->n;

  for(i=0;i<k;i++)
    cin>>(G->vexs[i]);

  for(i=0;i<G->n;i++)
    for(j=0;j<G->n;j++)
      G->edges[i][j]=INFINITE;
  

  cout<<"EDGES: "<<endl;

  for(k=0;k<G->e;k++){
    int weight;
    cin>>N1>>N2>>weight;
    G->edges[N1-1][N2-1]=weight;
  }

  return;
}

typedef struct{
  int visited[10];
  int finishing_time[10];
  int discovery_time[10];
  int times;
}DFS_DATA;

typedef struct{
  int weight;
  int parent;
}STORE;

void DFSM(MGraph *G,int index,DFS_DATA *DATA){

  DATA->times++;

  DATA->discovery_time[index]=DATA->times;

  DATA->visited[index]=1;

  for(int i=0;i<G->n;i++)
    if(G->edges[index][i]==1 && DATA->visited[i]==0){
      DFSM(G,i,DATA);
    }

  DATA->finishing_time[index]=DATA->times;
  DATA->times++;
}

void DFS(MGraph *G,DFS_DATA *DATA){

  for(int i=0;i<G->n;i++){
    DATA->visited[i]=0;
  }

  for(int i=0;i<G->n;i++){
    DATA->finishing_time[i]=0;
    DATA->discovery_time[i]=0;
  }

  DATA->times=0;

  for(int i=0;i<G->n;i++){
    if(DATA->visited[i]==0)
      DFSM(G,i,DATA);
  }
}

vector<int> Topological_Sort(MGraph *G){

  DFS_DATA *DATA = new DFS_DATA;

  vector<int> RESULT;
  vector<int> tmp;

  DFS(G,DATA);

  for(int i=0;i<G->n;i++)
    tmp.push_back(DATA->finishing_time[i]);

  sort(tmp.begin(),tmp.end());

  for(int i=G->n-1;i>=0;i--)
    for(int j=0;j<G->n;j++)
      if(DATA->finishing_time[j]==tmp[i]){
        RESULT.push_back(j);
      }

  delete DATA;
  
  return RESULT;
}

int Acyclic(MGraph *G){

  vector<int> CHECK;

  CHECK = Topological_Sort(G);

  for(int i=1;i<G->n;i++)
    for(int j=0;j<i;j++){
      
      if(G->edges[CHECK[i]][CHECK[j]]==1)
        return 1;
    }

  return 0;
}

vector<STORE> Critical_Path(MGraph *G,int from){

  vector<STORE> S(G->n);  

  if(Acyclic(G)==1)
    goto Negacyclic;

  for(int i=0;i<G->n;i++){
    S[i].weight = INFINITE;
    S[i].parent = -1;
  }

  S[from].weight = 0;
  S[from].parent = from;

  for(int i=0;i<((G->n)-1);i++)
    for(int j=0;j<G->n;j++)
      for(int k=0;k<G->n;k++)
        if(G->edges[j][k] < INFINITE){
          
          if(S[k].weight > S[j].weight - G->edges[j][k]){
            S[k].weight = S[j].weight - G->edges[j][k];
            S[k].parent = j;
          }
        }

  for(int i=0;i<S.size();i++)
    S[i].weight = abs(S[i].weight);

  return S;

 Negacyclic:
  cout<<"There is Negacyclic"<<endl;
  S.resize(0);
  return S;
}

void Print_Path(vector<STORE>& S,int index){
  if(index == 0){
    cout<<index+1<<" ";
    return;
  }
  else{
    Print_Path(S,S[index].parent);
    cout<<index+1<<" ";
  }

  return;
}


int main()
{
  vector<STORE> S;
  MGraph *G = new MGraph;

  CreateGraphM(G);
  S = Critical_Path(G,0);

  cout<<endl;

  int MAX = 0;
  int index;
  for(int i=0;i<S.size();i++)
    if(MAX <= S[i].weight){
      MAX = S[i].weight;
      index = i;
    }

  Print_Path(S,index);

  cout<<endl<<endl;

  for(int i=0;i<S.size();i++){
    cout<<i+1<<" ";
    cout<<S[i].weight <<" ";
    cout<<S[i].parent+1;
    cout<<endl;
  }
  delete G;

  return 0;
}



先判断有没环,有环就没有关键路径

S[k].weight > S[j].weight - G->edges[j][k]
这句改成S[k].weight >S[j].weight + G->edges[j][k] 就是BELLMAN-FORD求最短路径算法了

思路就是取相反数.

关键路径可以理解成 单源最长路径  但不能有负循环. 所以bellman-ford就可以用上场了

你可能感兴趣的:(算法,J#)