UVa 818 Cutting Chains

本文探讨了如何解决将多个链连接成一个连续链条的问题,通过枚举和深度优先搜索算法来确定最少需要打开的链接数量。文章详细介绍了输入格式、输出要求以及算法实现过程,包括初始化、链连接判断、遍历检查和搜索策略。

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

  Cutting Chains 

What a find! Anna Locke has just bought several links of chain some of which may be connected. They are made from zorkium, a material that was frequently used to manufacture jewelry in the last century, but is not used for that purpose anymore. It has its very own shine, incomparable to gold or silver, and impossible to describe to anyone who has not seen it first hand.

Anna wants the pieces joined into a single end-to-end strand of chain. She takes the links to a jeweler who tells her that the cost of joining them depends on the number of chain links that must be opened and closed. In order to minimize the cost, she carefully calculates the minimum number of links that have to be opened to rejoin all the links into a single sequence. This turns out to be more difficult than she at first thought. You must solve this problem for her.

Input 

The input consists of descriptions of sets of chain links, one set per line. Each set is a list of integers delimited by one or more spaces. Every description starts with an integer n, which is the number of chain links in the set, where 1 ≤n ≤15. We will label the links 1, 2,..., n. The integers following n describe which links are connected to each other. Every connection is specified by a pair of integers i,j where 1 ≤i,j ≤n and i ≠j, indicating that chain links i and j are connected, i.e., one passes through the other. The description for each set is terminated by the pair -1 -1, which should not be processed.

The input is terminated by a description starting with n = 0. This description should not be processed and will not contain data for connected links.

Output 

For each set of chain links in the input, output a single line which reads

Set N: Minimum links to open is M

where N is the set number and M is the minimal number of links that have to be opened and closed such that all links can be joined into one single chain.


Sample InputOutput for the Sample Input
5 1 2 2 3 4 5 -1 -1
7 1 2 2 3 3 1 4 5 5 6 6 7 7 4 -1 -1
4 1 2 1 3 1 4 -1 -1
3 1 2 2 3 3 1 -1 -1
3 1 2 2 1 -1 -1
0
Set 1: Minimum links to open is 1
Set 2: Minimum links to open is 2
Set 3: Minimum links to open is 1
Set 4: Minimum links to open is 1
Set 5: Minimum links to open is 1
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
// next[i]代表与第i个链相连的链的编号
vector<int> next[20];

// open[i] = 1 代表第i个链被打开
int open[20];

// connect[i][j] = 1 代表第i,j个链相连
int connect[20][20];

// 链数目
int num;

// 需要最少次数
int min_num;

// 现在所用次数
int now_count;

// 遍历时visit[i] = 1代表第i个链已被遍历过
int visit[20];

// 联通分量个数
int connect_count = 0;

void check(int x);
bool search();
bool dfs_search(int x, int y);

int main()
{
	int s_count = 0;
	while(scanf("%d", &num) && num != 0)
	{
		// 初始化
		memset(open, 0, sizeof(open));
		memset(connect, 0, sizeof(connect));
		for(int i = 1; i <= num; i++)
			next[i] = vector<int>();
		min_num = 15;
		now_count = 0;	
		// 读入情况
		int x, y;
		while(scanf("%d%d", &x, &y) == 2 && !(x == -1 && y == -1))
		{
			if(connect[x][y] == 0)
			{
				connect[x][y] = 1;
				connect[y][x] = 1;
				next[x].push_back(y);
				next[y].push_back(x);	
			}
		}			

		// 枚举所有的情况
		check(1);
		s_count++;
		printf("Set %d: Minimum links to open is %d\n", s_count, min_num);				
	}	
	return 0;
}

// 枚举所有的情况
void check(int x)
{
	if(x == num + 1)
	{/*
		for(int i = 1; i <= num; i++)
			printf("%d: %d ", i, open[i]);
		printf(" now_count : %d\n", now_count);
	*/	// 检查是否有环,连通分量个数是否为去掉点+1,是否有度数大于2的点
		if(search() && now_count < min_num)
		{
			min_num = now_count;
		}	
		return;
	}	

	open[x] = 0;
	check(x+1);
	open[x] = 1;
	now_count++;
	check(x+1);
	now_count--;	
}

// 检查是否有环,连通分量个数是否为去掉点+1,是否有度数大于2的点
bool search()
{
	memset(visit, 0, sizeof(visit));
	connect_count = 0;
	for(int i = 1; i <= num; i++)
	{
		if(open[i] == 0 && visit[i] == 0)
		{
			// 深度优先搜索
			visit[i] = 1;
			connect_count++;
			if(!dfs_search(i, -1))
				return false;
		}			
	}
	if(connect_count <= now_count + 1)	
		return true;	
	else
		return false;
}

// 深度优先搜索
// 由第x个链开始遍历
// y为x的父亲节点,如果x为初始节点,y为-1
bool dfs_search(int x,int y)
{
	int e_count = 0;
	for(int i = 0; i < next[x].size(); i++)
	{
		if(open[next[x][i]] == 0 && visit[next[x][i]] == 0)
		{
			visit[next[x][i]] = 1;
			if(!dfs_search(next[x][i], x))
				return false;
		}
		else if(y != next[x][i] && visit[next[x][i]] == 1)
			return false;
		if(open[next[x][i]] == 0)
			e_count++;
				
	}	
	if(e_count > 2)
		return false;
	else
		return true;	
}

这题没有想出来。枚举量不是很大的时候(2^15),就直接应该试试枚举。

如何判断:1.剩余图没有环 2.剩余图连通分量个数 <= 去掉点+1 3.剩余图中所有点的度数均不大于2.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值