POJ 1125 Stockbroker Grapevine

解题思路: 首先Floyd算法求出所有顶点之间的最小距离,计算最小最大距离

  
    
#include < iostream >
using namespace std;

#define MaxNum 1e8

int main()
{
int p[ 105 ][ 105 ];
int dist[ 105 ];
int n, m, v, t, start, minTime;
while (cin >> n && n)
{

fill(
& p[ 0 ][ 0 ], & p[ 104 ][ 104 ], MaxNum);
for ( int i = 1 ; i <= n; i ++ )
{
p[i][i]
= 0 ;
cin
>> m;
while (m -- )
{
cin
>> v >> t;
p[i][v]
= t;
}
}

for ( int k = 1 ; k <= n; k ++ )
for ( int i = 0 ; i <= n; i ++ )
for ( int j = 0 ; j <= n; j ++ )
if (p[i][j] > p[i][k] + p[k][j])
p[i][j]
= p[i][k] + p[k][j];

minTime
= MaxNum;
for ( int i = 1 ; i <= n; i ++ )
{
int maxTime = 0 ;
for ( int j = 1 ; j <= n; j ++ )
if (maxTime < p[i][j])
maxTime
= p[i][j];
if (minTime > maxTime)
{
minTime
= maxTime;
start
= i;
}
}
if (minTime == MaxNum)
cout
<< " disjoint " << endl;
else
cout
<< start << " " << minTime << endl;
}
return 0 ;
}

 

你可能感兴趣的:(broker)