zoukankan      html  css  js  c++  java
  • poj1125 Stockbroker Grapevine *

    /*
    * 水题~ floyd算法 0ms
    *
    * 求每对顶点间的最短路, 然后选择 最大值 最小的那个顶点作为起点~
    */

    #include
    <cstdio>
    #include
    <cstring>
    using namespace std;

    const int maxN = 100 + 5;
    const int inf = 10000000;
    int n, totEdgeNum, w[maxN][maxN];

    /*
    struct SEdge{
    int u, v, w;
    };
    SEdge edge[maxN * maxN];
    */

    void floyd(){
    for(int k=1; k<=n; k++){
    for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++){
    if(w[i][j] > w[i][k] + w[k][j])
    w[i][j]
    = w[i][k] + w[k][j];
    }
    }
    }
    }

    void getAns(){
    int totMin = inf, beg;
    for(int i=1; i<=n; i++){
    int lineMax = -1;
    bool flag = 1;
    for(int j=1; j<=n; j++){
    if(w[i][j] == -1){
    flag
    = 0; break;
    }
    if(lineMax < w[i][j]) lineMax = w[i][j];
    }
    if(flag && totMin > lineMax){
    totMin
    = lineMax; beg = i;
    }
    }

    if(totMin == inf)
    printf(
    "disjoint\n");
    else
    printf(
    "%d %d\n", beg, totMin);

    }

    int main(){
    while(scanf("%d", &n)){
    if(n == 0) return 0;

    for(int i=1; i<=n; i++)
    for(int j=1; j<=n; j++)
    w[i][j]
    = inf;

    int tmpN, tmpE, tmpW;
    for(int i=1; i<=n; i++){
    scanf(
    "%d", &tmpN);
    for(int j=1; j<=tmpN; j++){
    scanf(
    "%d%d", &tmpE, &tmpW);
    w[i][tmpE]
    = tmpW;
    }
    w[i][i]
    = 0;
    }

    floyd();

    getAns();
    }

    return 0;
    }
  • 相关阅读:
    3164 质因数分解
    codevs3249搭积木
    codevs 2964公共素数因数
    爱改名的小融1
    单链表基础练习
    并查集(union-find sets)
    string类中字符的大小写转换
    蒜头君学英语--set()练习
    打印锯齿矩阵
    堆积木
  • 原文地址:https://www.cnblogs.com/longdouhzt/p/2165392.html
Copyright © 2011-2022 走看看