每次枚举一个顶点,然后计算它到各个点的最大值,然后去顶点中最小的一个。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std;
const int MAXN = 110;
const int INF = 0x3f3f3f3f;
int d[MAXN][MAXN];
int n, m;
void init()
{
for(int i = 1; i < MAXN; i++)
{
for(int j = 1; j < MAXN; j++)
{
d[i][j] = (i == j) ? 0:INF;
}
}
}
void Floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int read_case()
{
init();
scanf("%d", &n);
if(!n) return 0;
for(int u = 1; u <= n; u++)
{
scanf("%d", &m);
while(m--)
{
int v, w;
scanf("%d%d", &v, &w);
d[u][v] = w;
}
}
return 1;
}
void solve()
{
Floyd();
int mintime = INF, index = 0;
for(int i = 1; i <= n; i++)
{
int m = -1;
for(int j = 1; j <= n; j++) if(i != j)
{
m = max(m, d[i][j]);
}
if(mintime > m)
{
mintime = m;
index = i;
}
}
if(mintime >= INF) printf("disjoint\n");
else printf("%d %d\n", index, mintime);
}
int main()
{
while(read_case())
{
solve();
}
return 0;
}
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std;
const int MAXN = 110;
const int INF = 0x3f3f3f3f;
int d[MAXN][MAXN];
int n, m;
void init()
{
for(int i = 1; i < MAXN; i++)
{
for(int j = 1; j < MAXN; j++)
{
d[i][j] = (i == j) ? 0:INF;
}
}
}
void Floyd()
{
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int read_case()
{
init();
scanf("%d", &n);
if(!n) return 0;
for(int u = 1; u <= n; u++)
{
scanf("%d", &m);
while(m--)
{
int v, w;
scanf("%d%d", &v, &w);
d[u][v] = w;
}
}
return 1;
}
void solve()
{
Floyd();
int mintime = INF, index = 0;
for(int i = 1; i <= n; i++)
{
int m = -1;
for(int j = 1; j <= n; j++) if(i != j)
{
m = max(m, d[i][j]);
}
if(mintime > m)
{
mintime = m;
index = i;
}
}
if(mintime >= INF) printf("disjoint\n");
else printf("%d %d\n", index, mintime);
}
int main()
{
while(read_case())
{
solve();
}
return 0;
}
CODE: