AcWing 853. There shortest bellman-ford limiting the number of sides

// negatively weight negatively handling ring 
 // 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 ++ ) {
             int A = Edge [J] II.A, B = Edge [J] .B, W = Edge [J] .W; 
            dist [ B] = min (dist [B], Backup [a] + W); // only spend time 
        } 
    } 
    IF (dist [n-]> 0x3f3f3f3f / 2 ) COUT << " Impossible " ;
     the 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/11842161.html