POJ 1125 Stockbroker Grapevine(DP)

来源:互联网 发布:网络厂商排名 编辑:程序博客网 时间:2024/06/03 08:16

题意:Stockbrokers要散布一个股票的谣言,谣言只能在相互认识的人中传递,给出人与人的关系(是否认识),以及传言在某两个认识的人中传递所需的时间。求出以哪个人作为散布谣言的起点,能使得所有人都受到谣言的时间最短。

思路:先floyd求出传递闭包,然后枚举每个人,求出传递给其他人的最大时间的最小值,保存下答案即可

代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 105;const int INF = 0x3f3f3f3f;int n, g[N][N];int main() {    while (~scanf("%d", &n) && n) {int tot;memset(g, INF, sizeof(g));for (int i = 1; i <= n; i++) {    g[i][i] = 0;    scanf("%d", &tot);    int a, b;    while (tot--) {scanf("%d%d", &a, &b);g[i][a] = min(g[i][a], b);    }}for (int k = 1; k <= n; k++) {    for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {    g[i][j] = min(g[i][j], g[i][k] + g[k][j]);}    }}int ans = INF, ansv;for (int i = 1; i <= n; i++) {    int s = -INF;    for (int j = 1; j <= n; j++)s = max(s, g[i][j]);    if (s < ans) {ans = s;ansv = i;    }}if (ans == INF) printf("disjoint\n");else printf("%d %d\n", ansv, ans);    }    return 0;}


0 0