AcWing 849. Dijkstra shortest path I seek simple

 

 

 

// negatively weights
 // If we can find out the negative cycles is generally absent
 // if negative loop that the minimum distance may be negative infinity 
#include <CString> 
#include <the iostream>
 the using  namespace STD;
 const  int N = 1E4 + . 1 ;
 struct Edge {
     int A;
     int B;
     int W; 
} Edge [N]; 
int n-, m, K;
 int dist [N], Backup [N];
 void bellman_ford () { 
    Memset (dist, 0x3F , the sizeof dist); 
    dist [ . 1 ] = 0 ;
    /// / k th iteration, indicates no longer than k edges come from each point 
    for ( int I = 0 ; I <k; I ++ ) { 
        the memcpy (Backup, dist, the sizeof dist); // backup, may occur without backup series 
        for ( int J = 0 ; J <m; J ++ ) {
             IF (dist [Edge [J] .B]> backup [Edge [J] II.A] + Edge [J] .W) {
                 int A = Edge [J] II.A, B = Edge [J] .B, W = Edge [J] .W; 
                dist [B] = min (dist [B], Backup [A] + W); 
            } 
        } 
    } 
    IF (dist [n-]> 0x3f3f3f3f / 2) cout << "impossible";
    else cout << dist[n];
}
int main() {
    cin >> n >> m >> k;
    for (int i=0; i<m; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        edge[i] = {a, b, w};
    }
    bellman_ford();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11842142.html