★【最小树形图】Command Network

在战争背景下,解决如何快速建立一个从总部到各分散节点的最短通信线路的问题,利用最小树形图原理实现指挥网络的快速部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and
KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a
total failure of littleken’s command network. A provisional network must be built immediately.
littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to
enable littenken’s commands to reach every disconnected node in the destroyed network and
decides on a plan to build a unidirectional communication network. The nodes are distributed
on a plane. If littleken’s commands are to be able to be delivered directly from a node A to
another node B, a wire will have to be built along the straight line segment connecting the
two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy
wants the plan to require the shortest total length of wires so that the construction can be
done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer
N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs
of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and
yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers
i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for
unidirectional command delivery from the former to the latter. littleken’s headquarter is always
located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two
digits past the decimal point. In the cases that such a network does not exist, just output ‘poor
snoopy’.

Sample Input
4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output
31.19
poor snoopy
此题考察最小树形图的应用。
在开始时先用深搜遍历一遍,判断是否所有点都能达到,若不能,则输出"poor snoopy"。
首先找到除根以外的所有节点的最小入边,若构成环,则将其缩为一个点,并修改相关边。
最后,只需要再统计出所尊不在环上的点的入边值即可。
Accode:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define sqr(x) ((x) * (x))
#define dist(u, v) (sqrt(sqr(x[u] - x[v]) + sqr(y[u] - y[v])))
const int maxN = 110;
const double INF = 1e198;
bool marked[maxN], circle[maxN];
double mp[maxN][maxN], x[maxN], y[maxN], ans;
int pre[maxN], n, m, root;

void Dfs(int u)
{
	marked[u] = 1;
	for (int v = 1; v < n + 1; ++v)
	if (!marked[v] && mp[u][v] < INF)
		Dfs(v);
	return;
}

inline bool ok()
{
	memset(marked, 0, sizeof marked);
	Dfs(root = 1);
	for (int i = 1; i < n + 1; ++i)
		if (!marked[i]) return 0;
	return 1;
}

inline int loop()
{
	pre[root = 1] = 1;
	for (int i = 1; i < n + 1; ++i)
	if (!circle[i] && i - root)
	{
		mp[i][i] = INF; pre[i] = i;
		//排除自环(自环肯定不在最小树形图上)。
		for (int j = 1; j < n + 1; ++j)
			if (!circle[j] && mp[j][i] < mp[pre[i]][i])
				pre[i] = j;
		//找到i的入边。
	}
	for (int i = 1; i < n + 1; ++i)
	if (!circle[i])
	{
		memset(marked, 0, sizeof marked);
		int j = i;
		for (; !marked[j]; j = pre[j]) marked[j] = 1;
		if (j == root) continue;
		return j; //这里是j遇到了环而不是i,所以应该返回j。
	}
	return -1;
} //检查是否有环。

inline void update(int u)
{
	ans += mp[pre[u]][u];
	for (int j = pre[u]; j - u; j = pre[j])
		ans += mp[pre[j]][j], circle[j] = 1;
	//累加环上的边,并把环上的节点除u外全部作上标记。
	for (int i = 1; i < n + 1; ++i)
	if (!circle[i] && mp[i][u] < INF)
		mp[i][u] -= mp[pre[u]][u];
	//这句话的含义是若添加了边<i,u>,则不需要边u的入边
	//(因为此时通过i可以到达u,则不需要其入边。
	for (int j = pre[u]; j - u; j = pre[j])
	for (int i = 1; i < n + 1; ++i)
	if (!circle[i])
	{
		if (mp[i][j] < INF)
			mp[i][u] = std::min(mp[i][u], mp[i][j] - mp[pre[j]][j]);
		mp[u][i] = std::min(mp[u][i], mp[j][i]);
	} //缩点,并把环上所有点都对u做一次松弛。
	return;
}

inline void solve()
{
	memset(circle, 0, sizeof circle); //注意清零。
	int j;
	while ((j = loop()) + 1) update(j);
	for (int i = 1; i < n + 1; ++i)
	if (!circle[i] && i - root)
		ans += mp[pre[i]][i];
	//累加剩余的入边。
	printf("%.2lf\n", ans);
	return;
}

int main()
{
	freopen("Command_Network.in", "r", stdin);
	freopen("Command_Network.out", "w", stdout);
	while (scanf("%d%d", &n, &m) != EOF)
	{
		for (int i = 0; i < n + 1; ++i)
		for (int j = 0; j < n + 1; ++j)
			mp[i][j] = INF;
		//注意mp数组的初始化。
		for (int i = 1; i < n + 1; ++i)
			scanf("%lf%lf", x + i, y + i);
		while (m--)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			mp[u][v] = dist(u, v);
		}
		ans = 0;
		if (!ok()) printf("poor snoopy\n");
		else solve();
	}
	return 0;
}

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值