zoukankan      html  css  js  c++  java
  • poj1125

    floyd算法,floyd过后查看每个点传播消息需要的时间,把最小的点输出即可。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    const int maxn = 101;
    
    int dist[maxn][maxn];
    int n;
    
    void init()
    {
    	memset(dist, -1, sizeof(dist));
    	for (int i = 0; i < n; i++)
    	{
    		int m;
    		scanf("%d", &m);
    		for (int j = 0; j < m; j++)
    		{
    			int a, b;
    			scanf("%d%d", &a, &b);
    			a--;
    			dist[i][a] = b;
    		}
    	}
    }
    
    void floyd()
    {
    	for (int i = 0; i < n; i++)
    		for (int j = 0; j < n; j++)
    			if (dist[j][i] != -1)
    				for (int k = 0; k < n; k++)
    					if (dist[i][k] != -1)
    						if (dist[j][k] == -1 || dist[j][k] > dist[j][i] + dist[i][k])
    							dist[j][k] = dist[j][i] + dist[i][k];
    }
    
    void work()
    {
    	int ans = 1000000000, ansi = -1;
    
    	for (int i = 0; i < n; i++)
    	{
    		bool ok = true;
    		int maxdist = 0;
    		for (int j = 0; j < n; j++)
    		{
    			if (i == j)
    				continue;
    			if (dist[i][j] == -1)
    			{
    				ok = false;
    				break;
    			}
    			if (maxdist < dist[i][j])
    				maxdist = dist[i][j];
    		}
    		if (maxdist < ans && ok)
    		{
    			ans = maxdist;
    			ansi = i;
    		}
    	}
    	if (ansi == -1)
    		printf("disjoint\n");
    	else
    		printf("%d %d\n", ansi + 1, ans);
    }
    
    int main()
    {
    	//freopen("D:\\t.txt", "r", stdin);
    	while (scanf("%d", &n) != EOF && n != 0)
    	{
    		init();
    		floyd();
    		work();
    	}
    	return 0;
    }
  • 相关阅读:
    原创 计算机系学生大学四年应该这样过
    ff3f34fq34f
    指针 引用
    POJ3352Road Construction
    POJ3308Paratroopers
    北大ACM试题分类 实时更新我所有的解题报告链接
    POJ2516Minimum Cost
    【转】一位ACMer过来人的心得
    POJ2528Mayor's posters
    POJ2186Popular Cows
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948667.html
Copyright © 2011-2022 走看看