匈牙利算法来找最多的没有弱化的!!!!但是初始化那块不知道为什么换了个地方就re。。。尴尬
2016-07-20 21:36
447 查看
#include<cstring> #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[15]; int G[15][15]; int g[10][10]; int n; int linker[15]; bool used[15]; bool dfs(int u) { for(int v=1; v<=n; v++) { if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==0||dfs(linker[v])) { linker[v]=u; return true; } } } return false; } int hungary() { int res=0; memset(linker,0,sizeof linker);//难道非得在这里初始化么? for(int u=1; u<=n; u++) { memset(used,false,sizeof used); if(dfs(u)) res++; } return res; } int main() { int 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]=1; } 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;//这里也是必须在里面一个一个初始化,不能直接在外面memset if(!G[i][a[j]]&&!G[i][a[j%n+1]]) g[i][j]=1; } ans=min(ans,n-hungary()); }while(next_permutation(a+2,a+n+1)); printf("%d\n",ans); } return 0; }
Necklace
[b][b]Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1111 Accepted Submission(s): 361
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Multiple test cases.
For each test case, the first line contains two integers
N(0≤N≤9),M(0≤M≤N∗N),
descripted as above.
Then M
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
4000
Y
with Yin energy.
[align=left]Output[/align]
One line per case, an integer indicates that how many gem will become somber at least.
[align=left]Sample Input[/align]
2 1
1 1
3 4
1 1
1 2
1 3
2 1
[align=left]Sample Output[/align]
1
1
相关文章推荐
- Angular2 架构
- MyEclipse:(4)MyEclipse中怎么设置Add cast to Clazz 快捷键
- apply, call, bind在js中的区别
- Tiny6410学习—Linux命令
- 【杭电1862】excel排序
- javascript jquery對form元素的常見操作
- [169] Majority Element
- Socket异步通信学习三
- Reference Or Value pass in python
- HDU 5729 Rigid Frameworks (from: 2016 Multi-University Training Contest)
- C++不是类型安全的语言
- HDU 5728 PowMod
- 什么是身份管理
- 【转】VS2010中文注释带红色下划线的解决方法
- UOJ 52 元旦激光炮
- Hdu-5726 GCD (二分 + RMQ)
- python chr()和ord()函数
- 284. Peeking Iterator
- 使用Zabbix批量监控域名访问情况
- mysql存储引擎