UVa 11463 - Commandos

主题:有一个敢死队,要销毁的建筑群,他们从一个特定建筑物离开,最后到一组特定的建筑物的。

            现在,各个建筑物之间进行连接的路由,班车需要在建筑物1时间单位,我问的第一次集合。

分析:图论,最短路径。直接计算起点s和终点e到那个其它全部点的最短路径。

            取min(dist(s,k)+ dist(k,e))就可以(0≤k<n)。

说明:第650题目╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int path[101][101];

int main()
{
	int T,n,m,u,v,b,e;
	while (~scanf("%d",&T))
	for (int t = 1; t <= T; ++ t) {
		scanf("%d%d",&n,&m);
		for (int i = 0; i < n; ++ i) {
			for (int j = 0; j < n; ++ j)
				path[i][j] = 101;
			path[i][i] = 0;
		}
		for (int i = 0; i < m; ++ i) {
			scanf("%d%d",&u,&v);
			path[u][v] = path[v][u] = 1;
		}
		//floyd
		for (int k = 0; k < n; ++ k)
		for (int i = 0; i < n; ++ i)
		for (int j = 0; j < n; ++ j)
			if (path[i][j] > path[i][k]+path[k][j])
				path[i][j] = path[i][k]+path[k][j];
		
		scanf("%d%d",&b,&e);
		int max = 0;
		for (int k = 0; k < n; ++ k)
			if (max < path[b][k]+path[k][e])
				max = path[b][k]+path[k][e];
		printf("Case %d: %d\n",t,max);
	}
    return 0;
}


posted @ 2015-12-11 16:51  lcchuguo  阅读(437)  评论(0编辑  收藏  举报