【TJOI2014】匹配(match)
2017-03-29 22:09
267 查看
Description
Solution
这道题目是比较裸的完备匹配的最大权值和,用KM算法(戳这里)轻松解决。对于求交集,明显可以通过删除完备匹配中的一条边,然后再跑一次KM,若是答案减小了,就说明只是一条必须使用的边。Code
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; #define fo(i,a,b) for(i=a;i<=b;i++) const int N=100,INF=0x7fffffff; int a ,eg ,eb ,dis ,match ,c ; bool visg ,visb ; int n,i,j,ans,tot,num; int dfs(int x){ int i,j,tot; visb[x]=true; fo(i,1,n){ if(visg[i]) continue; tot=eb[x]+eg[i]; if(tot==a[x][i]){ visg[i]=true; if((!match[i])||(dfs(match[i]))){ match[i]=x; return true; } } else{ dis[i]=min(dis[i],tot-a[x][i]); } } return false; } int KM(){ int i,j,tot; memset(eb,0,sizeof(eb)); memset(eg,0,sizeof(eg)); memset(match,0,sizeof(match)); fo(i,1,n) fo(j,1,n) eb[i]=max(eb[i],a[i][j]); fo(i,1,n){ memset(dis,127,sizeof(dis)); while(true){ memset(visb,0,sizeof(visb)); memset(visg,0,sizeof(visg)); if(dfs(i)) break; tot=INF; fo(j,1,n) if(!visg[j]) tot=min(tot,dis[j]); fo(j,1,n){ if(visb[j]) eb[j]-=tot; if(visg[j]) eg[j]+=tot; else dis[j]-=tot; } } } tot=0; fo(i,1,n) tot+=a[match[i]][i]; return tot; } int main(){ freopen("match.in","r",stdin); freopen("match.out","w",stdout); scanf("%d",&n); fo(i,1,n) fo(j,1,n) scanf("%d",&a[i][j]); ans=KM(); printf("%d\n",ans); fo(i,1,n) c[match[i]]=i; fo(i,1,n){ num=a[i][c[i]],a[i][c[i]]=0; tot=KM(); a[i][c[i]]=num; if(tot<ans) printf("%d %d\n",i,c[i]); } }
相关文章推荐
- 【TJOI2014】匹配(match)
- 【TJOI2014】匹配(match)
- 【TJOI2014】匹配(match)
- 【TJOI2014】匹配(match) 完美匹配KM算法
- 【jzoj3739】【TJOI2014】【匹配(match)】【网络流】
- JZOJ 3739. 【TJOI2014】匹配
- bzoj5154 [Tjoi2014]匹配(枚举+费用流)
- 【TJOI2014】匹配
- BZOJ5154 [Tjoi2014]匹配 【KM算法 + 枚举】
- 【JZOJ 3739】【TJOI2014】匹配
- bzoj 1264: [AHOI2006]基因匹配Match (DP+树状数组)
- Elasticsearch - 短语匹配(match_phrase)以及slop参数
- 【BZOJ1264】[AHOI2006]基因匹配Match【DP】【LCS】【树状数组】
- Scala中的match(模式匹配)
- bzoj3507 [Cqoi2014]通配符匹配
- Match One of Two Alternatives Based on a Condition (基于条件匹配多个选择中的一个)
- Test if a Match Can Be Found Within a Subject String (测试匹配是否可以在字符串中找到)
- Find a Match Within Another Match (在一个匹配中寻找另一个匹配)
- VBScript 中匹配多行, 并进行子操作的正则实例 /ASP VBScript Match Multiline/ By Stabx
- 内核添加dts后,device和device_driver的match匹配的变动:通过compatible属性进行匹配