AOJ grl_1_c:all Pairs Shortest path (Floyd-warshall algorithm to find the shortest path between any two points) (Bellman-ford algorithm to determine negative circle)

Source: Internet
Author: User

Title Link: Http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C

All Pairs Shortest Path

Input

An edge-weighted graph G ( V , E ).

|V| |E|s0t0d0s1t1d1:s|E|?1t|E|?1d|E|?1

|V|Is the number of vertices and are the number of |E| edges in G . The graph vertices is named with the numbers 0, 1,..., |V|?1 respectively.

siand ti represent source and target vertices of i -th edge (directed) and represents the cost of the di i -th E Dge.

Output

If The graph contains a negative cycle (a cycle whose sum of edge costs is a negative value), print

Negative CYCLE

In a line.

Otherwise, Print

D0,0D0,1D0,|V|?1D1,0D1,1D1,|V|?1:D|V|?1,0D1,1D|V|?1,|V|?1

The output consists of |V| lines. For each i th line, print the cost of the shortest path from vertex to each i vertex j ( j=0,1,…|V|?1 ) respectively . If there is no path from vertex i to vertex j , print "INF". Print a space between the costs.

Constraints
    • 1 ≤ |V| ≤100
    • 0≤ |E| ≤9900
    • -2x107≤ di ≤2x107
    • There is no parallel edges
    • There is no self-loops
Sample Input 1
4 60 1 10 2 51 2 21 3 42 3 13 2 7
Sample Output 1
0 1 3 4INF 0 2 3INF inf 0 1INF inf 7 0

Sample Input 2
4 60 1 10 2-51 2 21 3 42 3 13 2 7
Sample Output 2
0 1-5 -4inf 0 2 3INF inf 0 1INF inf 7 0

Sample Input 3
4 60 1 10 2 51 2 21 3 42 3 13 2-7
Sample Output 3
Negative CYCLE

This problem first uses the Bellman-ford algorithm to judge the negative circle, then uses the Floyd-warshall algorithm to seek any two points between the shortest circuit.

Code:

#include <iostream>#include<algorithm>#include<map>#include<vector>using namespaceStd;typedefLong Longll;#defineINF 2147483647structedge{int  from, To,cost;}; Edge es[10000];intd[ the][ the];//D[i][j] Indicates the shortest path of point I to JintV,e;//number of points and edges//Judging negative CirclesBOOLFind_negative_loop () {ints[ the]; Fill (s,s+v,0);  for(inti =0; i < V; i++){         for(intj =0; J < E; J + +) {Edge e=Es[j]; if(S[e.to] > S[e. from] +e.cost) {S[e.to]= S[e. from] +E.cost; if(i = = V1)return false; }        }    }    return true;}//Shortest path between any two pointsvoidWarshall_floyd () { for(intK =0; k < V; k++){         for(inti =0; i < V; i++){             for(intj =0; J < V; J + +){                if(D[i][k]! = INF && d[k][j]! =INF) D[i][j]= Min (D[i][j], d[i][k] +D[k][j]); }        }    }}intMain () {CIN>> V >>E;  for(inti =0; i < V; i++){         for(intj =0; J < V; J + +) {D[i][j]=INF; } D[i][i]=0; }     for(inti =0; i < E; i++) Cin >> Es[i]. from>> es[i].to >> Es[i].cost,d[es[i]. from][es[i].to] =Es[i].cost; if(Find_negative_loop ()) {Warshall_floyd ();  for(inti =0; i < V; i++){             for(intj =0; J < V; J + +){                if(J! =0) cout <<" "; if(D[i][j] = = INF) cout <<"INF"; Elsecout <<D[i][j]; } cout<<Endl; }    }Else{cout<<"Negative CYCLE"<<Endl; }        return 0;} 

AOJ grl_1_c:all Pairs Shortest path (Floyd-warshall algorithm to find the shortest path between any two points) (Bellman-ford algorithm to determine negative circle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.