USACO - 牛的旅行(Floyd)

USACO - 牛的旅行

题意

农民John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区不连通。这样,农民John就有多个牧区了。
John想在农场里添加一条路径(注意,恰好一条)。对这条路径有以下限制:
一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区的牧场,牧区用“*”表示,路径用直线表示。每一个牧区都有自己的坐标:

                15,15   20,15
                  D       E
                  *-------*
                  |     _/|
                  |   _/  |
                  | _/    |
                  |/      |
         *--------*-------*
         A        B       C
         10,10   15,10   20,10

这个牧场的直径大约是12.07106, 最远的两个牧区是A和E,它们之间的最短路径是A-B-E。
这里是另一个牧场:

                         *F 30,15
                         / 
                       _/  
                     _/    
                    /      
                   *------ 
                   G      H
                   25,10   30,10

这两个牧场都在John的农场上。John将会在两个牧场中各选一个牧区,然后用一条路径连起来,使得连通后这个新的更大的牧场有最小的直径。
注意,如果两条路径中途相交,我们不认为它们是连通的。只有两条路径在同一个牧区相交,我们才认为它们是连通的。
输入文件包括牧区、它们各自的坐标,还有一个如下的对称邻接矩阵:
A B C D E F G H
A 0 1 0 0 0 0 0 0
B 1 0 1 1 1 0 0 0
C 0 1 0 0 1 0 0 0
D 0 1 0 0 1 0 0 0
E 0 1 1 1 0 0 0 0
F 0 0 0 0 0 0 1 0
G 0 0 0 0 0 1 0 1
H 0 0 0 0 0 0 1 0
输入文件至少包括两个不连通的牧区。
请编程找出一条连接两个不同牧场的路径,使得连上这条路径后,这个更大的新牧场有最小的直径。

思路

首先通过01矩阵计算出每个点到它邻点的距离,然后用 f l o y d floyd floyd 算法求出每个点到其他点的最短距离,一个连通块的直径应该为这个连通块中相距最远的两点之间的距离。首先求出每个点在其所在的连通块中到其他点的最远距离 dmax[i],然后对于所有连通块,枚举他们之间能连的边,然后加上构成这条边的顶点的 dmax[i] ,即 cal(i,j) + dmax[i] + dmax[j],因为要让直径最小所以取一个 min,但同时,最小的直径一定不会小于 max(dmax[i]) 所以再对dmax数组取个max,两者之间的较大值即为答案。

代码
#include<bits/stdc++.h>
#include<unordered_map>
// #define int long long
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define MOD 998244353
#define rep(i, st, ed) for (int (i) = (st); (i) <= (ed);++(i))
#define pre(i, ed, st) for (int (i) = (ed); (i) >= (st);--(i))
#define debug(x,y) cerr << (x) << " == " << (y) << endl;
#define endl '\n'
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lowbit(T x) { return x & -x; }
// template<typename T> T qmi(T a, T b = mod - 2, T p = mod) { T res = 1; b %= (p - 1 == 0 ? p : p - 1); while (b) { if (b & 1) { res = (LL)res * a % p; }b >>= 1; a = (LL)a * a % p; }return res % mod; }

const int N = 200;
int n;
int g[200][200];
int cnt;
int id[N];
bool vis[N];
pair<double,double> pos[N];
double dist[N][N];
double dmax[N];
vector<int>block[N];

void dfs(int u, int cnt) {
	id[u] = cnt;
	block[cnt].push_back(u);
	vis[u] = true;
	for (int i = 1; i <= n; ++i) {
		if (i == u || g[u][i] == 0 || vis[i])continue;
		dfs(i, cnt);
	}
}

double cal(int x, int y) {
	return sqrt((pos[x].first - pos[y].first) * (pos[x].first - pos[y].first) + (pos[x].second - pos[y].second) * (pos[x].second - pos[y].second));
}

void solve() {
	cin >> n;

	rep(i, 1, n)cin >> pos[i].first >> pos[i].second;

	rep(i, 1, n)
		rep(j, 1, n)
		scanf("%1d", &g[i][j]);

	rep(i, 1, n) {
		rep(j, 1, n) {
			if (i == j)dist[i][j] = 0;
			else {
				if (g[i][j])dist[i][j] = cal(i, j);
				else dist[i][j] = INF;
			}
		}
	}

	rep(k, 1, n) {
		rep(i, 1, n) {
			rep(j, 1, n) {
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[j][k]);
			}
		}
	}

	rep(i, 1, n) {
		rep(j, 1, n) {
			if (dist[i][j] < INF) {
				dmax[i] = max(dmax[i], dist[i][j]);
				dmax[j] = max(dmax[j], dist[i][j]);
			}
		}
	}
	double res1 = 0;
	rep(i, 1, n)res1 = max(res1, dmax[i]);

	double res = INF;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if(dist[i][j] >= INF)
				res = min(res, cal(i, j) + dmax[i] + dmax[j]);
		}
	}

	printf("%lf\n", max(res, res1));
}

signed main() {

	//int _; cin >> _;
	//while (_--)
		solve();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzqwtc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值