果断按自己的思路写~~~BFS~~不过貌似是数据太弱了吧。。。。。。。。。。。。。。。。
我只能承认自己狗屎运了。。。。。。。。。。。。。。。。。。
//2616209 2011-08-05 15:10:44 Accepted 1082 C++ 0 612 ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n, Q[99999], d[101], pre[101], map[101][101], ans, mins, maxs;;
void input()
{
int i, j, k, t;
for(i=1; i<=n; i++)
{
scanf("%d", &j);
while(j--)
{
scanf("%d%d", &k, &t);
map[i][k] = t;
}
}
}
void start(int x)
{
int i;
maxs = 0;
for(i=1; i<=n; i++)
{
d[i] = 200000000;
pre[i] = 0;
}
d[x] = 0;
}
void BFS(int x)
{
int head, tail, i, k;
head = tail = 0;
Q[head] = x;
d[x] = 0;
while(head <= tail)
{
k = Q[head];
for(i=1; i<=n; i++)
if(map[k][i] && d[i] > d[k] + map[k][i] )
{
d[i] = d[k] + map[k][i];
Q[++tail] = i;
}
head++;
}
for(i=1; i<=n; i++)
if(maxs < d[i])
maxs = d[i];
}
int main()
{
int i, j, k;
while(scanf("%d", &n), n)
{
ans = 0;
mins = 20000000;
memset(map, 0, sizeof(map));
input();
for(i=1; i<=n; i++)
{
start(i);
BFS(i);
if(maxs < mins)
{
mins = maxs;
ans = i;
}
}
printf("%d %d\n", ans, mins);
}
return 0;
}
下边的应该是正确的(Bellman-Ford算法):
//2616421 2011-08-05 16:36:54 Accepted 1082 C++ 0 220 ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int n, map[101][101], d[101], pre[101];
void input()
{
int i, j, k, x, y;
for(i=1; i<=n; i++)
{
scanf("%d", &k);
while(k--)
{
scanf("%d%d", &x, &y);
map[i][x] = y;
}
}
}
void relax(int x, int y, int z)
{
if(d[y] > d[x] + z)
{
d[y] = d[x] + z;
pre[y] = x;
}
}
int main()
{
int i, j, k, ii, maxs, mins, ans;
while(scanf("%d", &n), n)
{
memset(map, 0, sizeof(map));
input();
mins = 200000000;
ans = 0;
for(ii = 1; ii<=n; ii++)
{
for(i=1; i<=n; i++)
{
d[i] = 200000000;
pre[i] = 0;
}
d[ii] = 0;
for(i=1; i<=n-1; i++)
{
for(j=1; j<=n; j++)
for(k=1; k<=n; k++)
if(map[j][k])
relax(j, k, map[j][k]);
}
maxs = 0;
for(i=1; i<=n; i++)
if(d[i] > maxs)
maxs = d[i];
if(maxs < mins)
{
mins = maxs;
ans = ii;
}
}
if(mins == 200000000)
printf("disjoint\n");
else
printf("%d %d\n", ans, mins);
}
return 0;
}