ZOJ-1082

来源:互联网 发布:淘宝店铺经营范围 编辑:程序博客网 时间:2024/06/05 03:50

依然floyd,依然弱弱水过。。题目读了半天,就是找出一个人,这个人对所有人都有一条路径并且最长的最短路径长度在所有人中最短,打印这个人的序号和最长的最短路径,如果不存在一个人对所有人都可达,就打印disjoint

#include<stdio.h>#include<limits.h>static int n, map[101][101];static void floyd(){int i, j, k, count, dst, time;for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)map[i][j] = i == j ? 0 : INT_MAX;for (i = 1; i <= n; i++){scanf("%d", &count);for (j = 0; j < count; j++){scanf("%d %d", &dst, &time);map[i][dst] = time;}}for (k = 1; k <= n; k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (map[i][k] != INT_MAX && map[k][j] != INT_MAX&& map[i][k] + map[k][j] < map[i][j])map[i][j] = map[i][k] + map[k][j];}int main(){while (scanf("%d", &n), n){floyd();int i, j, res = 0, min = INT_MAX, temp;for (i = 1; i <= n; i++){temp = -1;for (j = 1; j <= n; j++)if (map[i][j] > temp)temp = map[i][j];if (temp < min){min = temp;res = i;}}if (res)printf("%d %d\n", res, min);elseputs("disjoint");}return 0;}


0 0
原创粉丝点击