HDU5727.Necklace-二分图匹配匈牙利

Posted Persistent.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5727.Necklace-二分图匹配匈牙利相关的知识,希望对你有一定的参考价值。

好久没写过博客了,把以前的博客补一下。

Necklace

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3603    Accepted Submission(s): 1097


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 integers N(0N9),M(0MN?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 Ywith Yin energy.
 

 

Output
One line per case, an integer indicates that how many gem will become somber at least.
 

 

Sample Input
2 1
1 1
3 4
1 1
1 2
1 3
2 1
 

 

Sample Output
1
1
 

 

Author
HIT
 

 

Source

 

 

 

 

 

 

具体怎么写的忘记了

代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <ctime>
11 #include <vector>
12 using namespace std;
13 typedef long long ll;
14 const int INF=0x3f3f3f3f;
15 const int N=20;
16 int vis[N],a[N];
17 int n,m,ret;
18 int mp[N][N],g[N][N],match[N],used[N];
19 bool dfs(int u){
20     for(int v=1;v<=n;++v){
21         if(!g[u][v]||used[v])continue;
22         used[v]=true;
23         if(match[v]==-1||dfs(match[v])){
24             match[v]=u;
25             return true;
26         }
27     }
28     return false;
29 }
30 void solve(){
31     memset(match,-1,sizeof(match));
32     memset(g,0,sizeof(g));
33     for(int i=2;i<=n;++i){
34         for(int j=1;j<=n;++j){
35             if(mp[a[i]][j]||mp[a[i-1]][j])
36                 continue;
37             g[i][j]=true;//把珠子之间的关系做处理,褪色的两个珠子标记为0,不褪色的珠子为1,利用二分图最大匹配找到最大不消退数。
38         }
39     }
40     for(int i=1;i<=n;++i){
41         if(mp[a[1]][i]||mp[a[n]][i])
42             continue;
43         g[1][i]=true;
44     }
45     for(int i=1;i<=n;++i){
46         if(mp[a[1]][i]||mp[a[n]][i])
47             continue;
48         g[1][i]=true;
49     }
50     int ans=0;
51     for(int i=1;i<=n;++i){
52         memset(used,0,sizeof(used));
53         if(dfs(i))++ans;
54     }
55     ret=min(ret,n-ans);
56 }
57 void get(int x){
58     if(ret==0)return;
59     if(x==n+1){
60        solve();return;
61     }
62     for(int i=1;i<=n;i++){
63         if(vis[i])continue;
64         vis[i]=1;
65         a[x]=i;
66         get(x+1);
67         vis[i]=0;
68     }
69 }
70 int main(){
71     int v,u,i;
72     vis[1]=1;a[1]=1;
73     while(~scanf("%d%d",&n,&m)){
74         if(n==0){
75             printf("0\n");
76             continue;
77         }
78         memset(mp,0,sizeof(mp));
79         for(i=0;i<m;i++){
80             scanf("%d%d",&u,&v);
81             mp[v][u]=1;
82         }
83         ret=INF;
84         get(2);//n个阴珠子的全排列(注意:环形序列的全排列)
85         printf("%d\n",ret);
86     }
87     return 0;
88 }

 

以上是关于HDU5727.Necklace-二分图匹配匈牙利的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5727 Necklace(全排列+二分图匹配)

HDU 5727 Necklace ( 2016多校二分图匹配 )

二分图的最大匹配(匈牙利算法)HDU1083

hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

hdu 3605(二分图多重匹配,匈牙利算法)

HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)