URAL 1072 Routing (建图+迪杰斯特拉)

#include <stdio.h>
#define INF 99999999
#define MAX_COMPUTERS (100)
#define MAX_INTERFACES (10)


 struct COMPUTER{
    int numOfInterfaces;
    long long interface[MAX_INTERFACES];
};
COMPUTER computerArray[MAX_COMPUTERS];

int connectMatrix[MAX_COMPUTERS][MAX_COMPUTERS];
int distFromOrigin[MAX_COMPUTERS];
int found[MAX_COMPUTERS];
int path[MAX_COMPUTERS];
int stack[MAX_COMPUTERS];
int top;

int main()
{
    int numOfComputers;
    scanf("%d", &numOfComputers);
    int indexOfComputer;
    for (indexOfComputer = 1; indexOfComputer <= numOfComputers; indexOfComputer++){
        int numOfInterfaces;
        scanf("%d", &numOfInterfaces);
        computerArray[indexOfComputer].numOfInterfaces = numOfInterfaces;
        int indexOfInterface;
        for (indexOfInterface = 1; indexOfInterface <= numOfInterfaces; indexOfInterface++){
            int ip[5], mask[5];
            scanf("%d.%d.%d.%d", &ip[1], &ip[2], &ip[3], &ip[4]);
            scanf("%d.%d.%d.%d", &mask[1], &mask[2], &mask[3], &mask[4]);
            long long interface = 0;
            int index;
            for (index = 1; index <= 4; index++)
                interface = (interface * 1000) + (ip[index] & mask[index]);
            computerArray[indexOfComputer].interface[indexOfInterface] = interface;
        }
    }

    int startComputer, endComputer;
    scanf("%d %d", &startComputer, &endComputer);

    int from, to;
    for (from = 1; from <= numOfComputers; from++){
        int numOfInterfacesFrom = computerArray[from].numOfInterfaces;
        for (to = 1; to <= numOfComputers; to++){
            if (from == to)
                continue;
            int numOfInterfacesTo = computerArray[to].numOfInterfaces;

            int isConnected = 0;
            int start, end;
            for (start = 1; start <= numOfInterfacesFrom; start++){
                for (end = 1; end <= numOfInterfacesTo; end++)
                    if (computerArray[from].interface[start] ==
                        computerArray[to].interface[end]){
                        isConnected = 1;
                        break;
                    }
                if (isConnected)
                    break;
            }

            if (isConnected){
                connectMatrix[from][to] = 1;
                if (from == startComputer){
                    distFromOrigin[to] = 1;
                    path[to] = from;
                }

            } else {
                connectMatrix[from][to] = INF;
                if (from == startComputer)
                    distFromOrigin[to] = INF;
            }
        }
    }

    found[startComputer] = 1;
    int relax;
    for (relax = 1; relax < numOfComputers; relax++){
        int minDist = INF;
        int nearestComputer = 0;
        for (indexOfComputer = 1; indexOfComputer <= numOfComputers; indexOfComputer++){
            if (found[indexOfComputer] == 0 &&
                distFromOrigin[indexOfComputer] < minDist){
                minDist = distFromOrigin[indexOfComputer];
                nearestComputer = indexOfComputer;
            }
        }

        found[nearestComputer] = 1;

        for (indexOfComputer = 1; indexOfComputer <= numOfComputers; indexOfComputer++){
            if (found[indexOfComputer] == 0 &&
                minDist + connectMatrix[nearestComputer][indexOfComputer] < distFromOrigin[indexOfComputer]){

                distFromOrigin[indexOfComputer] =  minDist + connectMatrix[nearestComputer][indexOfComputer];
                path[indexOfComputer] = nearestComputer;
            }
        }
    }

    if (distFromOrigin[endComputer] == INF)
        printf("No");
    else {
        printf("Yes\n");
        stack[top++] = endComputer;
        int computer = path[endComputer];
        while (computer != 0){
            stack[top++] = computer;
            computer = path[computer];
        }
        top--;
        int index;
        for (index = top; index >= 0; index--)
            printf("%d%c", stack[index],  index == 0 ? '\n' : ' ');

    }

    return 0;
}

你可能感兴趣的:(最短路径,Routing,迪杰斯特拉,ural,1072)