UVA 818 Cutting Chains (DFS)

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
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

题意:一个环,不完整,问最少open几个才能组成一个完整的环.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
int mp[15][15];
int vis[15];
int n,cnt;
bool ok(int s)//ok判断一个环接2个以上的环的不合法状态,
{
    REP(i,n)
    {
        if(s&(1<<i))  continue;
        int num=0;
        REP(j,n)
        {
            if(s&(1<<j))   continue;
            if(mp[i][j])  num++;
        }
        if(num>2)   return true;
    }
    return false;
}
bool dfs(int s,int now,int pre)
{
    vis[now]=1;
    REP(i,n)
    {
        if(!mp[now][i]||(s&(1<<i))||i==pre)   continue;
        if(vis[i])  return true;
        if(dfs(s,i,now))  return true;
    }
    return false;
}
bool circle(int s)//circle判断成环的不合法状态
{
    REP(i,n)
    {
        if(vis[i]||(s&(1<<i)))  continue;
        cnt++;
        if(dfs(s,i,-1))  return true;
    }
    return false;
}
int cal(int s)//计算open环的个数
{
    return s==0?0:cal(s>>1)+(s&1);
}
int solve()
{
    int ans=0x3f3f3f;
    int status=1<<n;
    REP(stau,status)//枚举状态1:代表open
    {
        cnt=0;CLEAR(vis,0);
        if(ok(stau)||circle(stau))  continue;//ok判断一个环接2个以上的环的不合法状态,circle判断成环的不合法状态
        if(cal(stau)>=cnt-1)
            ans=min(ans,cal(stau));
    }
    return ans;
}
int main()
{
    int x,y;
    int cas=1;
    while(~scanf("%d",&n)&&n)
    {
        CLEAR(mp,0);
        while(~scanf("%d%d",&x,&y)&&(x!=-1&&y!=-1))
            mp[x-1][y-1]=mp[y-1][x-1]=1;
        printf("Set %d: Minimum links to open is %d\n",cas++,solve());
    }
    return 0;
}
时间: 2024-10-10 17:30:12

UVA 818 Cutting Chains (DFS)的相关文章

UVa 818 Cutting Chains 题解

难度:β 建议用时:40 min 这题应该有迭代加深搜索的解法的,但我参考网友做法,用暴力枚举法. 大致思路是:枚举圆环的每种开闭状态,统计符合要求的最小的打开的圆环的数量. 要判断打开圆环的某一种方法是否符合要求,容易想到的一个是先判断除去这些已打开的圆环外的闭合圆环有没有组成一个环. 如果还有环,那么无论把打开的圆环怎样重新连城一条链套回去,都不能消除环,而题目要求我们够一条链.所以如果任然存在环的方法是不行的. 0000   0 (此时还有一个环没打开,不能组成链) 0    0 0000

UVA 818 Cutting Chains

题意就是给一张无向图,去掉某些结点,然后连成一条链,问最少去掉几个结点. n很小n<=15,所以直接枚举2^15个状态就行啦. 链的条件是1.无环,2.没有度大于2的点,3.把n个散链连起来需要n-1次拼接,去掉的结点数>=n-1. #include<bits/stdc++.h> using namespace std; const int maxn = 15; int G[maxn][maxn]; int n; int c[maxn]; bool dfs(int u,int s,

UVA - 818 Cutting Chains 暴力

题目大意:给出n个环(类似奥运五环的那种环),要求你打开其中的m个环,然后以这m个环为中介,使得所有的环能形成一条链 解题思路:暴力枚举,用二进制表示断开的环的位置和数量. 断开环后,和该环相连都断开了,也就是该环变成了一个孤立的环 接着判断一下非断开的环能否连成一个环,如果能连成一个环,那就不可能通过m个环当中介连成一条链 还得判断一下非断开的环的度,如果度超过2,也不能变成一条链 #include<cstdio> #include<cstring> #include<al

uva 818 (位运算 + 判环)

 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 purpo

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

uva 10003 Cutting Sticks (DP)

uva 10003 Cutting Sticks Description 你的任务是替一家叫Analog Cutting Machinery (ACM)的公司切割木棍.切割木棍的成本是根据木棍的长度而定.而且切割木棍的时候每次只切一段.很显然的,不同切割的顺序会有不同的成本.例如:有一根长10公尺的木棍必须在第2.4.7公尺的地方切割.这个时候就有几种选择了.你可以选择先切2公尺的地方,然后切4公尺的地方,最后切7公尺的地方.这样的选择其成本为:10+8+6=24.因为第一次切时木棍长10公尺,

UVA 10003 Cutting Sticks(区间dp)

Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they onl