POJ 2594 Treasure Exploration

来源:互联网 发布:利用淘宝漏洞赚钱 编辑:程序博客网 时间:2024/05/17 08:33

POJ 2594 Treasure Exploration

题目链接:POJ 2594

题意:

使最少的机器人参观完n个点,在通过的道路中,机器人只可以一端到另一端,不能往后走,机器人之间通过的点可能相同。


分析:

最小路径覆盖数=最大匹配数


#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,s,v;// BFSconst int maxn=512;int g[maxn][maxn];int L,R;int x[maxn],y[maxn];int Q[maxn]; //模拟队列int pre[maxn];int MaxMatch(){    int res=0,temp;memset(x,-1,sizeof(x));memset(y,-1,sizeof(y));for(int i=1;i<=L;i++){if(x[i]==-1){int cur=0,tail=0;for(int j=1;j<=R;j++){if(g[i][j])pre[j]=-1,Q[tail++]=j;else pre[j]=-2;}//BFSwhile(cur<tail){temp=Q[cur];if(y[temp]==-1)break;cur++;for(int j=1;j<=R;j++){if(pre[j]==-2&&g[y[temp]][j]){pre[j]=temp;Q[tail++]=j;}}}//end of BFSif(cur==tail)  //没有找到交错轨continue;while(pre[temp]>-1)  //更改交错轨上匹配状态{x[ y[pre[temp]] ]=temp;y[temp]=y[pre[temp]];temp=pre[temp];}y[temp]=i;x[i]=temp;res++;}}return res;}int main(){// freopen("in.txt","r",stdin);int u,v;    while(scanf("%d %d",&n,&m)!=EOF&&(n||m)){memset(g,0,sizeof(g));for(int i=0;i<m;i++){scanf("%d %d",&u,&v);g[u][v]=1;}for(int k=1;k<=n;k++)    //floyd构图(中间许多过程点){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(k==i||k==j)  continue;if(g[i][k]==1&&g[k][j]==1)g[i][j]=1;}}}L=R=n;printf("%d\n",n-MaxMatch());}return 0;}



0 0
原创粉丝点击