POJ 1125 Stockbroker Grapevine 最短路 难度:0

http://poj.org/problem?id=1125

#include <iostream>
#include <cstring>
using namespace std;
int d[101][101];// dag ATTENTION
int num[101];//the number of contracts
int edge[101][101];// adjecent edge table
int n;//always represent the maxnum of single data
bool input(){
    if((cin>>n)==NULL)return false;
    if(n==0)return false;
    for(int i=0;i<n;i++){
        memset(d+i,0x3f,n*sizeof(int));
        cin>>num[i];
        int temp;
        for(int j=0;j<num[i];j++){
            cin>>temp;
            temp--;
            edge[i][j]=temp;
            cin>>d[i][temp];
        }
    }
    return true;
}
void solve(){
    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                d[i][j]=min(d[i][k]+d[k][j],d[i][j]);
            }
        }
    }
    int maxn=0;
    int ans=0x3f3f;
    int ansn;
    for(int i=0;i<n;i++){
        maxn=0;
        for(int j=0;j<n;j++){
            if(j!=i)maxn=max(maxn,d[i][j]);
        }
        if(ans>maxn){
            ansn=i;
            ans=maxn;
        }
    }
    cout<<ansn+1<<" "<<ans<<"
";
}
int main(){
    while(input()){
        solve();
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/xuesu/p/4754397.html