poj 2594 Treasure Exploration 二分匹配
2015-11-20 19:54
411 查看
[code]#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN=601*601+10; const int MAXM=601*601*2; struct Edge { int to,next; } edge[MAXM]; int head[MAXN],tot,mx[MAXN],cx[MAXN],cy[MAXN],mk[MAXN],mp[600+5][600+5]; void addedge(int from,int to) { edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++; } void init() { memset(head,0xff,sizeof(head)); tot=0; } int dfs(int u) { for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(!mk[v]) { mk[v]=1; if(cy[v]==-1||dfs(cy[v])) { cx[u]=v; cy[v]=u; return 1; } } } return 0; } int Maxmatch(int n) { int res=0; memset(cx,0xff,sizeof(cx)); memset(cy,0xff,sizeof(cy)); for(int i=1; i<=n; i++) { if(cx[i]==-1) { memset(mk,0,sizeof(mk)); res+=dfs(i); } } return res; } int main() { int _,n,m,i,j,k,u,v; while(scanf("%d%d",&n,&m)) { init(); if(n==0&&m==0) break; memset(mp,0,sizeof(mp)); for(i=0; i<m; i++) { scanf("%d%d",&u,&v); addedge(u,v); mp[u][v]=1; } for(i=1;i<=n;i++) { for(k=1;k<=n;k++) { if(mp[i][k]==0||i==j) continue; for(j=1;j<=n;j++) { if(mp[k][j]==0||i==k||j==k) continue; mp[i][j]=1; addedge(i,j); } } } printf("%d\n",n-Maxmatch(n)); } return 0; }
相关文章推荐
- 如何获取触摸精灵的版本号??
- 网站开发进阶(十六)错误提示:Multiple annotations found at this line:- basePath cannot be resolved to a variable
- 你好,世界
- cookie和session
- Demo:C++操作MySql
- 51nod 1154 回文串划分 (DP)
- matlab学习笔记 repmat函数与kron函数之区别
- 以图搜图/图像检索相关资料
- Object-c学习之自定义视图
- Object-c学习之自定义视图
- Visual Studio 2015安装心得
- java自带线程池和队列详细讲解,android中适用
- 最近使用django遇到的两个问题
- 【Leetcode】Copy List with Random Pointer
- form-json-url传参
- mac下如何设置开机自动运行指定命令
- iOS9中请求出现App Transport Security has blocked a cleartext HTTP (http://)
- 网站开发进阶(十五)JS基础知识充电站
- ip
- 网站开发进阶(十五)JS基础知识充电站