您的位置:首页 > 其它

POJ 1502 MPI Maelstrom

2014-12-09 15:58 225 查看
http://poj.org/problem?id=1502

许久没写最短路 还是1A 哈哈!裸最短路!

#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXV = 110;
const int INF = 0x3f3f3f3f;

int n;
int G[MAXV][MAXV];

void Init(){
    for(int i=0; i<MAXV; i++){
        for(int j=0; j<MAXV; j++)
            G[i][j] = INF;
        G[i][i] = 0;
    }
}

int change(char *p){
    int res = 0;
    for(int i=0; p[i]!='\0'; i++)
        res = res * 10 + p[i] - '0';
    return res;
}

int dis[MAXV],vis[MAXV];

void SPFA(){
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    vis[1] = 1, dis[1] = 0;
    queue <int> Q;
    Q.push(1);
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for(int v=1; v<=n; v++){
            if(G[u][v] == INF)  continue;
            if(dis[v] > dis[u] + G[u][v]){
                dis[v] = dis[u] + G[u][v];
                if(vis[v])  continue;
                Q.push(v);
                vis[v] = 1;
            }
        }
    }
}

int main(){
    while(scanf("%d",&n) == 1){
        Init();
        for(int i=2; i<=n; i++)
            for(int j=1; j<i; j++){
                char tmp[1010];
                cin >> tmp;
                if(tmp[0] == 'x')    G[i][j] = G[j][i] = INF;
                else G[i][j] = G[j][i] = change(tmp);
            }
        SPFA();
        int ans = -INF;
        for(int i=1; i<=n; i++)
            ans = max(ans, dis[i]);
        cout << ans << endl;
    }
    ///system("pause");
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: