818 - Cutting Chains

26 篇文章 0 订阅

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 Input Output 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
Output for the Sample Input
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

ACM World Finals 2000, Problem C

题意:有n个圆环,其中有一些已经扣在一起,现在需要打开尽量少的圆环,似的所有圆环可以组成一条链。(当然,所有打开的圆环都要再一次合闭,把其他的两个环连在一起)。输入若干组数a b表示a和b之间已经扣在一起。

我有话说:
思路很简单。枚举打开的圆环。然后进行判断是否还有多个分支或者环。在判断利用打开的环能否将已有的短链连接成一条长链,即打开的环数>=剩下的短链数-1。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=15;
const int INF=1000000000;
int n,G[N][N],vis[N],cnt;
void Init()
{
    memset(G,0,sizeof(G));
    int u,v;
    while(scanf("%d%d",&u,&v)==2&&u!=-1&&v!=-1)
    {
        G[u-1][v-1]=G[v-1][u-1]=1;
    }
}
bool have_two(int s)
{
    for(int i=0;i<n;i++)
    {
        if(s&(1<<i))continue;//它是要被切断的。
        int num=0;
        for(int j=0;j<n;j++)
        {
            if(s&(1<<j))continue;
            if(G[i][j])num++;
        }
        if(num>2)return true;//有两个及以上的分支。
    }
    return false;
}
bool dfs(int now,int fa,int s)
{
    //if(vis[now])return false;
    vis[now]=1;
    for(int i=0;i<n;i++)
    {
        if(!G[now][i]||i==fa||(s&(1<<i)))continue;
        if(vis[i])return true;;
        if(dfs(i,now,s))return true;
    }
    return false;
}
bool circle(int s)
{
    for(int i=0;i<n;i++)
    {
        if(s&(1<<i))continue;
        if(vis[i])continue;
        cnt++;//cnt为切断后剩余的短链数量
        if(dfs(i,-1,s))return true;//有圈

    }
    return false;
}

int cal(int s)
{
    int ans=0;
    while(s){
        if(s&1)ans++;
        s=(s>>1);
    }
    return ans;
}

int solve()
{
    int ans=INF;
    int s=(1<<n);
    for(int i=0;i<s;i++)//二进制位表示那些圆环需要断开
    {
         cnt=0;
         memset(vis,0,sizeof(vis));
        if(have_two(i)||circle(i))continue;
        if(cal(i)>=cnt-1){
            ans=min(ans,cal(i));
        }
    }
    return ans;
}
int main()
{
    int kase=0;
    while(scanf("%d",&n)==1&&n){
        Init();
        printf("Set %d: Minimum links to open is %d\n",++kase,solve());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值