poj-1125 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;
}

你可能感兴趣的:(broker)