comet OJ 热身赛 E. dijkstra dijkstra

题解

利用距离公式建边跑dijkstra
两条线距离c1-c2/sqrt(aa+bb) 线与圆利用点到直线距离-圆半径与0取max 圆到圆直接点距离公式-半径与0取max

AC代码

#include 
#include 
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
int n, a, b, c1, c2;

struct node
{
	int v;
	double w;
	bool operator < (const node &oth) const
	{
		return w > oth.w;
	}
};
vector<node> e[MAXN];
struct node2
{
	int x, y, r;
}d[MAXN];

double dis(int a, int b, int c, int x, int y) //点到直线距离
{
	return  1.0 * abs(a * x + b * y + c) / sqrt(a * a + b * b);
}
double dis2(int x1, int x2, int y1, int y2) //点之间距离
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
double cot[MAXN];
int vis[MAXN];
double dij()
{
	for (int i = 2; i <= n; i++)
		cot[i] = 1LL * INF * INF;
	priority_queue<node> pq;
	pq.push({ 1, 0 });
	while (!pq.empty())
	{
		int u = pq.top().v;
		double w = pq.top().w;
		vis[u] = 1;
		if (u == n)
			return w;
		pq.pop();
		for (node &it : e[u])
		{
			int v = it.v;
			if (!vis[v] && w + it.w < cot[v])
				cot[v] = w + it.w, pq.push({ v, w + it.w });
		}
	}
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	cin >> n >> a >> b >> c1 >> c2;
	n += 2; //1为第一条线 2~n-1为n个圆 n为第二条线
	e[1].push_back({ n, abs(c1 - c2) / sqrt(a * a + b * b) }); //两条线建边
	e[n].push_back({ 1, abs(c1 - c2) / sqrt(a * a + b * b) });
	for (int i = 2; i < n; i++)
	{
		scanf("%d%d%d", &d[i].x, &d[i].y, &d[i].r); //线与原建边
		double res = max(0.0, dis(a, b, c1, d[i].x, d[i].y) - d[i].r);
		e[1].push_back({ i, res }), e[i].push_back({ 1, res });
		res = max(0.0, dis(a, b, c2, d[i].x, d[i].y) - d[i].r);
		e[n].push_back({ i, res }), e[i].push_back({ n, res });
	}
	for (int i = 2; i < n; i++)
		for (int j = i + 1; j < n; j++) //圆与圆建边
		{
			e[i].push_back({ j, max(0.0, dis2(d[i].x, d[j].x, d[i].y, d[j].y) - d[i].r - d[j].r) });
			e[j].push_back({ i, max(0.0, dis2(d[i].x, d[j].x, d[i].y, d[j].y) - d[i].r - d[j].r) });
		}
	printf("%.10lf\n", dij());

	return 0;
}

你可能感兴趣的:(___图论___,_计算几何_,dijkstra,2019,CCPC-Wannafly,Winter,Camp)