#include #include #include #include using namespace std; struct Edge{ int from,to,weight; }edges[100000]; int dist[10000],n,m; void init(){ scanf("%d %d",&n,&m); for(int i=0;i dist[edges[j].from] + edges[j].weight){///if the distance can be updated with a better one dist[edges[j].to] = dist[edges[j].from] + edges[j].weight;///we do it somethingHasChanged=true;///and mark if something has changed } } if(!somethingHasChanged)break;///if nothing has changed this iteration then it wont change the next one since we will do the exact same thing } ///we must do one more iterration. somethingHasChanged = false; for(int j=0;j dist[edges[j].from] + edges[j].weight){ dist[edges[j].to] = dist[edges[j].from] + edges[j].weight; somethingHasChanged=true; } } if(somethingHasChanged){/// If something changes in this one then there is a cycle with a negative weight printf("There is a cycle with negative weight\n"); exit(0); } ///the algorithm ends here. dist[i] is the minimal distance from the start node to node i. Note that if there are components that are unreachable from the start node ///the dist to those nodes will be INT_MAX } void print(){ for(int i=0;i