poj 1235 Machine Schedule 最小点覆盖

来源:互联网 发布:vb登录界面模板下载 编辑:程序博客网 时间:2024/06/05 20:26

题意:有两个机器a和b,它们各有n,m种工作模式。序号为0~n-1 ,0~m-1.起始时,a,b都处于工作模式0。现在有k个作业需要借助机器a和b。第I个作业需要a的第u个模式,或者b的第v个模式,可以表示成(I,u,v),因为装换工作模式只有重启才能调换。所以,在把所有作业完成的前提下,尽量少的重启机器,求出最小的重启次数。

思路:对于某个工作牵涉a,b的两个工作模式,则在工作模式之间连一条边,边代表这个工作,所以求最小重启量,就是求用最少的顶点去覆盖所有的边,也即求最小点覆盖,另外,一开始处于0模式,所以对于a,b的0模式要忽略,一开始就处于0模式,不算重启。

最小点覆盖=最大(基数)匹配

#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn = 1505;struct edge{int from,to;};vector<edge> edges;vector<int> g[maxn];int check[maxn],match[maxn];int n,m,k;void addedge(int from,int to){edges.push_back(edge{from,to});edges.push_back(edge{to,from});int m=edges.size();g[from].push_back(m-2);g[to].push_back(m-1);}bool dfs(int u){for(int i=0;i<g[u].size();i++){int v=edges[g[u][i]].to;if(!check[v]){check[v]=1;if(match[v]==-1||dfs(match[v])){match[v]=u;match[u]=v;return true;}}}return false;}int Hungarian(){int ans=0;memset(match,-1,sizeof(match));for(int i=1;i<n;i++){if(match[i]==-1){memset(check,0,sizeof(check));if(dfs(i))++ans;}}return ans;}int main(){while(scanf("%d",&n)&&n){scanf("%d%d",&m,&k);for(int i=0;i<=n+m;i++)g[i].clear();edges.clear();int nul,u,v;for(int i=0;i<k;i++){scanf("%d%d%d",&nul,&u,&v);if(u==0||v==0)continue;addedge(u,v+n);}int ans=Hungarian();printf("%d\n",ans);}}

0 0