POJ- graph theory - the shortest path template (adjacency matrix)

POJ- graph theory - the shortest path template

A, Floyd algorithm

When the data has just been read, G is the adjacency matrix of FIG read, updated, G [i] [j] represents the shortest path to node i to node j length

int G [N] [N]; // two-dimensional array, its initial value is the adjacency matrix of FIG.

1. the init () : Initialization FIG adjacency matrix

void the init ( ) 
{ 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        for ( int J = . 1 ; J <= n-; J ++ ) 
        { 
            G [I] [J] = - . 1 ; // initialize abutment matrix, with -1 for infinite 
        } 
        G [I] [I] = 0 ; // initializes to their own path length is 0 
    } 
}

2. Floyd () : Updates the shortest path

void floyd()
{
    for (int k = 1; k <= n; k++)//从1至n循环k
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)//遍历所有的i,j
            {
                if (G[i][k] == -1 || G[k][j] == -1)continue;
                if (G[i][j] == -1 || G[i][k] + G[k][j] < G[i][j])G[i][j] = G[i][k] + G[k][j];
            }
        }
    }
}

Example 5.5  Shortest 

Template code

#include <cstdio>
 const  int N = 105 ;
 int G [N] [N];
 int n-, m; 

void the init () 
{ 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        for ( int J = . 1 ; J <= n-; J ++ ) 
        { 
            G [I] [J] = - . 1 ; // initializes infinity 
        } 
    } 
    for ( int I = . 1 ; I <= n-; I ++) G [I] [I] = 0 ; // itself to distance himself from 0
}

void floyd()
{
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (G[i][k] == -1 || G[k][j] == -1)continue;
                else if (G[i][j] == -1 || G[i][j] > G[i][k] + G[k][j])G[i][j] = G[i][k] + G[k][j];
            }
        }
    }
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (n == 0 && m == 0)break;
        init();
        int x, y, t;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &x, &y, &t);
            G[x][y] = G[y][x] = t;
        }
        floyd();
        printf("%d\n", G[1][n]);
    }
    return 0;
}

Two, Dijkstra algorithm

FIG adjacency matrix of G, G [i] [j] denotes read distance j to i, the latter is no longer updated. D [] array is the distance, d [i] represents the shortest distance of node i to the starting point. vis [N] to access the array, where each record access node.

int G [N] [N]; // FIG adjacency matrix 
int D [N]; // represents the shortest distance to the starting point of each node 
BOOL VIS [N] = { to false }; // represents each node is visited or not

1. the init () : initialize an adjacency matrix, the distance is 0 to itself

void init() // 初始化
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (i == j) G[i][j] = 0;
            else G[i][j] = INF;
        }
    }
}

2. the Dijkstra (int Start) : After initialization D [], for n cycles, to determine the distance each directly connected to the start point u and the point most summary distance D [u], and then updates the start point to other nodes of v the distance d [v].

void the Dijkstra ( int start) 
{ 
    for ( int I = . 1 ; I <= n-; I ++) D [I] = INF; // up are initialized to infinity distance 
    D [start] = 0 ; // first only start nodes up point 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        int U = - . 1 ; // nearest node u, is initialized to -1 
        int min = INF; // min stored minimum D [ u], is initialized to infinity 
        for ( int J = . 1 ; J <= n-; J ++) // get closest point 
        {
            IF (VIS [J] == to false && D [J] < min) 
            { 
                U = J; // distance from the starting point is directly connected to the most Summary 
                min = D [J]; 
            } 
        } 
        IF (U == - . 1 ) return ; // description of the remaining nodes not been accessed are not up to the beginning 
        VIS [U] = to true ; // set read 
        for ( int V = . 1 ; V <= n-; V ++) / / a later point updating all newly obtained points 
        {
             // here d [u] has an addition operation, so that when setting can not be set large integer 0x7FFFFFFF, will result in an overflow 
            if (vis[v] == false && G[u][v] != INF && d[u] + G[u][v] < d[v]) d[v] = d[u] + G[u][v];
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/yun-an/p/11104314.html