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

    题目链接:http://acm.nankai.edu.cn/p2121.html

    解题思路:二分图匹配(匈牙利算法DFS实现)

      1 /**************************************************************************
      2 user_id: SCNU20102200088
      3 problem_id: nkoj 2121
      4 problem_name: 飞行员配对方案问题
      5 **************************************************************************/
      6 
      7 #include <algorithm>
      8 #include <iostream>
      9 #include <iterator>
     10 #include <iomanip>
     11 #include <sstream>
     12 #include <fstream>
     13 #include <cstring>
     14 #include <cstdlib>
     15 #include <climits>
     16 #include <bitset>
     17 #include <string>
     18 #include <vector>
     19 #include <cstdio>
     20 #include <cctype>
     21 #include <ctime>
     22 #include <cmath>
     23 #include <queue>
     24 #include <stack>
     25 #include <list>
     26 #include <set>
     27 #include <map>
     28 using namespace std;
     29 
     30 //线段树
     31 #define lson l,m,rt<<1
     32 #define rson m+1,r,rt<<1|1
     33 
     34 //手工扩展栈
     35 #pragma comment(linker,"/STACK:102400000,102400000")
     36 
     37 const double EPS=1e-9;
     38 const double PI=acos(-1.0);
     39 const double E=2.7182818284590452353602874713526;  //自然对数底数
     40 const double R=0.5772156649015328606065120900824;  //欧拉常数:(1+1/2+...+1/n)-ln(n)
     41 
     42 const int x4[]={-1,0,1,0};
     43 const int y4[]={0,1,0,-1};
     44 const int x8[]={-1,-1,0,1,1,1,0,-1};
     45 const int y8[]={0,1,1,1,0,-1,-1,-1};
     46 
     47 typedef long long LL;
     48 
     49 typedef int T;
     50 T max(T a,T b){ return a>b? a:b; }
     51 T min(T a,T b){ return a<b? a:b; }
     52 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     53 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     54 
     55 ///////////////////////////////////////////////////////////////////////////
     56 //Add Code:
     57 const int maxm=105;
     58 const int maxn=105;
     59 int m,n,x[maxm],y[maxn];
     60 bool check[maxn],flag[maxm][maxn];
     61 
     62 bool SearchPath(int k){
     63     for(int i=m+1;i<=n;i++){
     64         if(flag[k][i] && !check[i]){
     65             check[i]=1;
     66             if(y[i]==-1 || SearchPath(y[i])){
     67                 x[k]=i;
     68                 y[i]=k;
     69                 return 1;
     70             }
     71         }
     72     }
     73     return 0;
     74 }
     75 
     76 int MaxMatch(){
     77     int ret=0;
     78     memset(x,-1,sizeof(x));
     79     memset(y,-1,sizeof(y));
     80     for(int i=1;i<=m;i++){
     81         if(x[i]==-1){
     82             memset(check,0,sizeof(check));
     83             if(SearchPath(i)) ret++;
     84         }
     85     }
     86     return ret;
     87 }
     88 ///////////////////////////////////////////////////////////////////////////
     89 
     90 int main(){
     91     std::ios::sync_with_stdio(false);
     92     //freopen("in.txt","r",stdin);
     93     //freopen("out.txt","w",stdout);
     94     ///////////////////////////////////////////////////////////////////////
     95     //Add Code:
     96     int a,b,i;
     97     scanf("%d%d",&m,&n);
     98     memset(flag,0,sizeof(flag));
     99     while(scanf("%d%d",&a,&b)!=EOF){
    100         if(a==-1 && b==-1) break;
    101         flag[a][b]=1;
    102     }
    103     int ans=MaxMatch();
    104     if(ans==0) printf("No Solution!");
    105     else{
    106         printf("%d
    ",ans);
    107         for(i=1;i<=m;i++){
    108             if(x[i]!=-1) printf("%d %d
    ",i,x[i]);
    109         }
    110     }
    111     ///////////////////////////////////////////////////////////////////////
    112     return 0;
    113 }
    114 
    115 /**************************************************************************
    116 Testcase:
    117 Input:
    118 5 10
    119 1 7
    120 1 8
    121 2 6
    122 2 9
    123 2 10
    124 3 7
    125 3 8
    126 4 7
    127 4 8
    128 5 10
    129 -1 -1
    130 Output:
    131 4
    132 1 7
    133 2 9
    134 3 8
    135 5 10
    136 **************************************************************************/
  • 相关阅读:
    「SOL」工厂选址(BZOJ)
    「NOTE」数论小札
    Flask实现简单的群聊和单聊
    python基础总结
    基于Flask和百度AI实现与机器人对话
    django创建路径导航
    django中权限控制到按钮级别
    django中非菜单权限的归属
    MongoDB的增删改查
    jQuery于js的区别和联系
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3360585.html
Copyright © 2011-2022 走看看