HDU 5727 Necklace (二分图匹配hungary)
来源:互联网 发布:长沙seo推广 编辑:程序博客网 时间:2024/05/17 03:32
Necklace
Problem Description
SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid making the necklace too Yin or too Yang, he must place these magic gems Yin after Yang and Yang after Yin, which means two adjacent gems must have different kind of energy. But he finds that some gems with Yang energy will become somber adjacent with some of the Yin gems and impact the value of the neckless. After trying multiple times, he finds out M rules of the gems. He wants to have a most valuable neckless which means the somber gems must be as less as possible. So he wonders how many gems with Yang energy will become somber if he make the necklace in the best way.
Input
Multiple test cases.
For each test case, the first line contains two integersN(0≤N≤9),M(0≤M≤N∗N) , descripted as above.
ThenM lines followed, every line contains two integers X,Y , indicates that magic gem X with Yang energy will become somber adjacent with the magic gem Y with Yin energy.
For each test case, the first line contains two integers
Then
Output
One line per case, an integer indicates that how many gem will become somber at least.
Sample Input
2 11 13 41 11 21 32 1
Sample Output
11题意:有一些阴阳球组成一个项链,一些阳球在和某些球相邻时会变暗,要求最少的变暗个数分析:难点还是在于建模,一开始想用暴搜去解决,但是没有想到一个好的剪枝策略,再自己看看题目,似乎和二分图有关,对于一个组合,我们可以这样想,如果阳球的左右都没有与之影响的阴球,那么那个位置一定可行,否则那个位置不可取,那么题目就转换成了阳球与插入位置的二分匹配了,现在就只需要确定阴球位置,就可以建图,直接套hungary算法分析一下复杂度: 8!*81 可行#include<cstring>#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[15];int G[15][15];int g[10][10];int uN,vN;int linker[15];bool used[15];bool dfs(int u){ for(int v=1; v<=vN; v++) { if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false;}int hungary(){ int res=0; memset(linker,-1,sizeof linker); for(int u=1; u<=uN; u++) { memset(used,false,sizeof used); if(dfs(u)) res++; } return res;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0) { printf("0\n"); continue; } memset(G,0,sizeof G); for(int i=1; i<=m; i++) { int a,b; scanf("%d%d",&a,&b); G[a][b]=1; } uN=vN=n; int ans=100; for(int i=1; i<=n; i++) a[i]=i; do { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { g[i][j]=0; if(j==n) { if(!G[i][a[j]]&&!G[i][1]) g[i][j]=1; } else if(!G[i][a[j]]&&!G[i][a[j+1]]) { g[i][j]=1; } } } int num=hungary(); ans=min(ans,n-num); }while(next_permutation(a+2,a+n+1)); printf("%d\n",ans); } return 0;}
0 0
- HDU 5727 Necklace (二分图匹配hungary)
- HDU 5727 Necklace(二分匹配-hungary)
- HDU 5727 Necklace (二分图匹配)
- HDU 5727 Necklace(枚举+二分图匹配)
- HDU 5727 Necklace (二分图匹配)
- HDU 5727 Necklace(二分图匹配)
- Hdu 5727 Necklace(二分图匹配)
- 二分图匹配Hungary算法
- hdu 5727 - Necklace ( 环排列 + 二分图匹配)
- [HDU 5727] Necklace (暴力 + 二分图匹配)
- HDU 5727 Necklace (全排列+二分图最大匹配)
- hdu 5727 Necklace 状压dp/二分图匹配
- HDU 5727 Necklace(二分匹配)
- HDU 2063 过山车(二分匹配-hungary)
- POJ 2239 二分图匹配/Hungary
- Selecting Courses(二分图匹配 Hungary算法)
- 二分图最大匹配Hungary算法详解
- hdu 5727 Necklace (全排列+二分匹配)
- iOS 边缘滑动返回
- HDOJ 2089 不要62
- MinGW 下编译 libcurl
- 开发人员的网页汇总
- Codeforces Round #363 C (Div. 2) Vacations
- HDU 5727 Necklace (二分图匹配hungary)
- 分批加载
- HDU 5733 tetrahedron
- 40.You have statistics collected for some selected tables. Your requirement is that the statistics
- 导航条菜单制作总结
- Angular__Module
- 设置布局不随输入框键盘出现而上移
- 未检测到Java SDK 环境-Java环境变量设置-Java开发环境搭建
- 用AFNetworking3.0封装网络请求