#include <cstring>
#include <stdio.h>
int m,n;
struct Node
{
int no;
int to;
Node* next;
int weight;
};
Node* List1[100];
Node* List2[100];
int count1[100];
int count2[100];
int El[100];
int Ee[100];
int e[100];
int L[100];
void toposort()
{
memset(Ee,0,sizeof(Ee));
memset(El,0,sizeof(El));
memset(e,0,sizeof(e));
memset(L,0,sizeof(L));
int top1=-1;
int top2=-1;
for(int i=0;i<m;i++)
{
if(count1[i]==0)
{
count1[i]=top1;
top1=i;
}
}
for(int i=0;i<m;i++)
{
if(top1==-1)
{
printf("cycle1");
}
else
{
int j= top1;
top1=count1[top1];
Node *temp1 = List1[j];
while(temp1)
{
int key = temp1->to;
if(--count1[key]==0)
{
count1[key]=top1;
top1=key;
}
if(Ee[key]<Ee[j]+temp1->weight)
Ee[key]=Ee[j]+temp1->weight;
temp1=temp1->next;
}
}
}
// printf("555555%d %d",Ee[m-1],Ee[m]);
for(int i=0;i<m;i++)
{
El[i]=Ee[m-1];
if(count2[i]==0)
{
count2[i]=top2;
top2=i;
}
}
for(int i=0;i<m;i++)
{
if(top2==-1)
printf("cycle2");
else
{
int j=top2;
top2=count2[top2];
Node* temp2 = List2[j];
while(temp2)
{
int key =temp2->to;
if(--count2[key]==0)
{
count2[key]=top2;
top2=key;
}
if(El[key]>El[j]-temp2->weight)
El[key]=El[j]-temp2->weight;
temp2=temp2->next;
}
}
}
for(int i=0;i<m;i++)
{
Node * temp=List1[i];
while(temp!=NULL)
{
int end=temp->to;
int no=temp->no;
e[no]=Ee[i];
L[no]=El[end]-temp->weight;
if(e[no]==L[no])
printf("A%d : %d->%d\n",no,i,end);
temp=temp->next;
}
}
}
int main()
{
freopen("1.txt","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF)
{
int a,b,c;
memset(List1,0,sizeof(List1));
memset(List2,0,sizeof(List2));
memset(count1,0,sizeof(count1));
memset(count2,0,sizeof(count2));
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
Node* temp1 = new Node();
temp1->no=i+1;
temp1->next=NULL;
temp1->to=b;
temp1->weight=c;
count1[b]++;
if(List1[a]==NULL)
{
List1[a]=temp1;
}
else
{
temp1->next=List1[a];
List1[a]=temp1;
}
Node* temp2=new Node();
temp2->next=NULL;
temp2->to=a;
temp2->weight=c;
temp2->no=i+1;
count2[a]++;
if(List2[b]==NULL)
List2[b]=temp2;
else
{
temp2->next=List2[b];
List2[b]=temp2;
}
}
toposort();
for(int i=0;i<m;i++)
{
Node* temp =List1[i];
while(temp!=NULL)
{
List1[i]=List1[i]->next;
delete temp;
temp=List1[i];
}
Node*another =List2[i];
while(another!=NULL)
{
List2[i]=List2[i]->next;
delete another;
another =List2[i];
}
}
}
while(1);
}
关键路径
最新推荐文章于 2022-05-07 23:30:18 发布