zoukankan      html  css  js  c++  java
  • 飞行员配对方案问题

    题目大意:给出一个二分图,求它的最大匹配。

    匈牙利算法板子题。。

    代码:(写的比较烂)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,m,hed[105],cnt;
    struct EG
    {
        int to,nxt;
    }e[10005];
    void ae(int f,int t)
    {
        e[++cnt].to = t;
        e[cnt].nxt = hed[f];
        hed[f] = cnt;
    }
    int fa[105],vis[105];
    bool dfs(int u)
    {
        vis[u]=1;
        for(int j=hed[u];j;j=e[j].nxt)
        {
            int to = e[j].to;
            if(vis[to])continue;
            vis[to]=1;
            if(!fa[to]||dfs(fa[to]))
            {
                fa[to]=u;
                return 1;
            }
        }
        return 0;
    }
    struct ass
    {
        int x,y;
    }as[102];
    int tot;
    bool cmp(ass a,ass b)
    {
        return a.x<b.x;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        int f,t;
        while(1)
        {
            scanf("%d%d",&f,&t);
            if(f==-1&&t==-1)break;
            ae(f,t);
        }
        int ans = 0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;
        }
        printf("%d
    ",ans);
        for(int i=n+1;i<=m;i++)
        {
            if(fa[i])
            {
                as[++tot].x=fa[i];
                as[tot].y=i;
            }
        }
        sort(as+1,as+1+tot,cmp);
        for(int i=1;i<=tot;i++)
            printf("%d %d
    ",as[i].x,as[i].y);
        return 0;
    }
  • 相关阅读:
    无废话XML--XML约束(DTD)
    无废话XML--XML细节
    XML,HTML,XHTML
    javabean内省
    反射应用--IOC和AOP
    JDK动态代理
    反射
    类加载机制
    linkin大话数据结构--泛型
    java异常处理
  • 原文地址:https://www.cnblogs.com/LiGuanlin1124/p/9747193.html
Copyright © 2011-2022 走看看