刚开始以为是求最小生成树,可是打完代码一看结果根本不对。最后看了一下discuss原来是的那个一个点和m个点连接时,可以同时传送信息。所以只要转换成dijkstra求原点到所有点的最短然后找出最大时间,就是从原点到最后一个点的时间长度了,,循环遍历所有点当做原点情况,然后找出最佳值。。
View Code
#include#include #include #include #define maxn 107 using namespace std; int dis[maxn],map[maxn][maxn]; bool vt[maxn]; const int inf = 9999999; int ans,tmp; int n; void init() { int i,j; for (i = 0; i <= n; ++i) { for (j = 0; j <= n; ++j) { if (i == j) map[i][j] = 0; else map[i][j] = inf; } } } int Dijkstra(int s) { int i,j,k,min; int sum = 0; for (i = 1; i <= n; ++i) { dis[i] = map[s][i]; vt[i] = false; } vt[s] = true; for (k = 1; k < n; ++k) { j = s; min = inf; for (i = 1; i <= n; ++i) { if (!vt[i] && dis[i] < min) { min = dis[i]; j = i; } } vt[j] = true; for (i = 1; i <= n; ++i) { if (!vt[i] && dis[i] > dis[j] + map[j][i]) dis[i] = dis[j] + map[j][i]; } } for (i = 1; i <= n; ++i)//循环找出最长的时间段 { if (sum < dis[i]) sum = dis[i]; } if (sum == inf) return -1; else return sum; } int main() { int i,j,m,a,b; while (~scanf("%d",&n)) { if (!n) break; init(); for (i = 1; i <= n; ++i) { cin>>m; for (j = 0; j < m; ++j) { cin>>a>>b; map[i][a] = b; } } int ct = 0,t = 0; ans = inf; for (i = 1; i <= n; ++i)//枚举所有点为原点 { int tmp = Dijkstra(i); if (tmp == -1)//判断是否可达 { ct++; } else if (tmp < ans)//记录最佳值 { ans = tmp; t = i; } } if (ct == n) { printf("disjoint\n"); } else printf("%d %d\n",t,ans); } return 0; }