I haven't understood it for a long time.
First, give the code of a predecessor.
1 #include <iostream> 2 using namespace std; 3 struct Edge 4 { 5 int x,y,d; 6 }edge[50000+5]; 7 int n; 8 int d[50000+5]; 9 int main()10 { 11 while(scanf("%d",&n)!=EOF)12 {13 int Min=INT_MAX;14 int Max=INT_MIN;15 for(int i=0;i<n;++i)16 {17 scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].d);18 if(--edge[i].x<Min)Min=edge[i].x;19 if(edge[i].y>Max)Max=edge[i].y;20 }21 int tmp=Max-Min+1;22 for(int i=Min;i<=Max;++i)23 d[i]=INT_MIN;24 d[Min]=0;25 for(int i=0;i<tmp;++i)26 {27 bool update=1;28 for(int j=0;j<n;++j)29 {30 if(d[edge[j].x]!=INT_MIN&&d[edge[j].y]<d[edge[j].x]+edge[j].d) 31 {32 update=0;33 d[edge[j].y]=d[edge[j].x]+edge[j].d;34 }35 }36 for(int j=Min;j<Max;++j)37 {38 if(d[j]!=INT_MIN&&d[j]>d[j+1])39 {40 d[j+1]=d[j];41 update=0;42 }43 }44 for(int j=Max;j>Min;--j)45 {46 if(d[j]!=INT_MIN&&d[j]-1>d[j-1])47 {48 update=0;49 d[j-1]=d[j]-1;50 }51 }52 if(update)break;53 }54 printf("%d\n",d[Max]-d[Min]);55 }56 return 0;57 }
The analysis continues today, with some results. The Foundation is that the analysis is very slow. It's been a long time before you can understand it.