POJ1125 Stockbroker Grapevine

一.原题链接:http://poj.org/problem?id=1125

二.题目大意:一个经纪人可以与其他经济人联系,输入第一行经纪人的数量,接下来每行为从1~n个经济人分别可以跟谁联系,和联系的时间。

三.思路:第一次做最短路,然后看了一下午Dijkstra算法,勉强AC了。

四.代码

Dijkstra算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_SIZE = 128, INF = 1<<30;


int numOfVertex;
int graph[MAX_SIZE][MAX_SIZE];

void Dijkstra(int startPos, int dist[])
{
    int i, j, minPath, nextPos;
    bool visited[MAX_SIZE] = {0};

//initialize first vertex
    visited[startPos] = true;
    for(i = 1; i <= numOfVertex; i++){
        if(graph[startPos][i] > 0 && !visited[i]){
            dist[i] = graph[startPos][i];
        }
        else{
            dist[i] = INF;
        }
    }

    dist[startPos] = 0;

    for(i = 2; i <= numOfVertex; i++){
        minPath = INF;
//find minPath
        for(j = 1; j <= numOfVertex; j++){
            if(dist[j] < minPath && !visited[j]){
                    minPath = dist[j];
                    startPos = j;
            }
        }
//move
        visited[startPos] = true;

        for(j = 1; j <= numOfVertex; j++){
            if(graph[startPos][j] > 0 && !visited[j] &&
               minPath + graph[startPos][j] < dist[j] ){
                    dist[j] = minPath + graph[startPos][j];
               }
        }
    }

}

void findAns(int &minPath, int &pos)
{
//maxPath 表示从每个点开始到其他所有点的最长的路径
    int dist[MAX_SIZE], i, j, maxPath;

    minPath = INF;
    for(i = 1; i <= numOfVertex; i++){ //cout<<endl<<i<<"入口: ";
        Dijkstra(i, dist);
        maxPath = -1;
        for(j = 1; j <= numOfVertex; j++){ // cout<<dist[j]<<" ";
            maxPath = max(maxPath, dist[j]);
            if(INF == maxPath)
                break;
        }
        //cout<<endl;
        //cout<<"maxPath: "<<maxPath;
        if(minPath > maxPath){
            minPath = maxPath;
            pos = i;
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int i, j, nextVertex, weight, numOfNext,
        minPath, minStartPos;

    while(cin>>numOfVertex){

        if(numOfVertex == 0)
            break;
        memset(graph, 0, sizeof(graph));
        minPath = INF;

//initialize the graph
        for(i = 1; i <= numOfVertex; i++){
            scanf("%d", &numOfNext);
            while(numOfNext--){
                scanf("%d %d", &nextVertex, &weight);
                graph[i][nextVertex] = weight;
            }
        }

        findAns(minPath, minStartPos);
        if(minPath != INF){
            printf("%d %d\n", minStartPos, minPath);
        }
        else{
            printf("disjoint\n");
        }
    }

    return 0;
}
Floyd算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_SIZE = 128, INF = 1<<29;


int numOfVertex;
int graph[MAX_SIZE][MAX_SIZE];

void Floyd()
{
    int i, j, k;
    for(k = 1; k <= numOfVertex; k++)
        for(i = 1; i <= numOfVertex; i++)
            for(j = 1; j <= numOfVertex; j++)
                if(graph[i][j] > graph[i][k] + graph[k][j])
                    graph[i][j] = graph[i][k] + graph[k][j];

}

void findAns(int &path, int &pos)
{
    int i, j, maxPath;
    for(i = 1; i <= numOfVertex; i++){
        maxPath = -1;
        for(j = 1; j <= numOfVertex; j++){
            maxPath = max(maxPath, graph[i][j]);
            if(INF == maxPath)
                break;
        }
        if(path > maxPath){
            path = maxPath;
            pos = i;
        }
    }

}

int main()
{
   // freopen("in.txt", "r", stdin);
    int i, j, nextVertex, weight, numOfNext,
        minPath, minStartPos;

    while(cin>>numOfVertex){

        if(numOfVertex == 0)
            break;

        for(i = 0; i <= numOfVertex; i++)
            for(j = 0; j <= numOfVertex; j++)
                if(i != j)
                    graph[i][j] = INF;
                else
                    graph[i][j] = 0;

        minPath = INF;

//initialize the graph
        for(i = 1; i <= numOfVertex; i++){
            scanf("%d", &numOfNext);
            while(numOfNext--){
                scanf("%d %d", &nextVertex, &weight);
                graph[i][nextVertex] = weight;
            }
        }

        Floyd();

        findAns(minPath, minStartPos);
        if(minPath != INF){
            printf("%d %d\n", minStartPos, minPath);
        }
        else{
            printf("disjoint\n");
        }
    }

    return 0;
}



你可能感兴趣的:(POJ1125 Stockbroker Grapevine)