POJ 1125

很简单的题,不过题面信息冗余,要快速抽取信息

快速复习了一边Floyd算法

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;

const int maxn= 105;
const int INF= 0x3f3f3f3f;

int dv[maxn][maxn];

int main(int argc, char const *argv[])
{
	int n;
	while (~scanf("%d", &n) && n){
		memset(dv, 0x3f, sizeof(dv));
		for (int i= 1; i<= n; ++i){
			int m, p, v;
			scanf("%d", &m);
			for (int j= 1; j<= m; ++j){
				scanf("%d %d", &p, &v);
				dv[i][p]= v;
			}
			dv[i][i]= 0;
		}

		for (int r= 1; r<= n; ++r){
			for (int i= 1; i<= n; ++i){
				for (int j= 1; j<= n; ++j){
					dv[i][j]= min(dv[i][j], dv[i][r]+dv[r][j]);
				}
			}
		}

		int minc= INF, stoc= -1;
		for (int i= 1; i<= n; ++i){
			int t_min= 0, flag= 1;
			for (int j= 1; j<= n; ++j){
				if (INF== dv[i][j]){
					flag= 0;
					break;
				}
				t_min= max(t_min, dv[i][j]);
			}
			if (flag && t_min< minc){
				minc= t_min;
				stoc= i;
			}
		}

		if (-1== stoc){
			printf("disjoint\n");
		}
		else{
			printf("%d %d\n", stoc, minc);
		}
	}
	return 0;
}
posted @ 2021-04-23 16:18  IdiotNe  阅读(43)  评论(0编辑  收藏  举报