HDOJ 2236 – 無題II 暴力+二分圖最大匹配判斷

來源:互聯網
上載者:User

                 題意

                          這是一個簡單的遊戲,在一個n*n的矩陣中,找n個數使得這n個數都在不同的行和列裡並且要求這n個數中的最大值和最小值的差值最小。

                 題解

                          枚舉最大最小數的差值(也可以二分)...再枚舉最小值為多少..推出最大值為多少..構圖..用二分圖最大匹配來判斷是否有解...

                          總結...一類關於每行每列只能取一個數的問題..可以考慮二分圖最大匹配...

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<string.h>#include<queue>#define ll long long#define esp 1e-5#define MAXN 105#define MAXM 50000000#define oo 100000007using namespace std; int n,match[MAXN],Min,Max,A[MAXN][MAXN];bool used[MAXN],g[MAXN][MAXN];bool dfs(int x){      for (int i=1;i<=n;i++)          if (!used[i] && g[x][i])          {                 used[i]=true;                 if (!match[i] || dfs(match[i]))                 {                        match[i]=x;                        return true;                 }           }      return false;}bool getmax(){      int sum=0;      memset(match,0,sizeof(match));      for (int i=1;i<=n;i++)      {             memset(used,false,sizeof(used));             if (!dfs(i)) return false;      }      return true;}int getans(){      int Min,Max,ans,x,y;      for (ans=0;ans<=100;ans++)          for (Min=0;Min<=100-ans;Min++)          {                  Max=Min+ans;                  memset(g,false,sizeof(g));                  for (y=1;y<=n;y++)                     for (x=1;x<=n;x++)                        if (A[y][x]>=Min && A[y][x]<=Max)                            g[y][x]=true;                  if (getmax()) return ans;          }}int main()  {                int cases,i,j,anss;       scanf("%d",&cases);       while (cases--)         {                  scanf("%d",&n);               for (i=1;i<=n;i++)                   for (j=1;j<=n;j++)                       scanf("%d",&A[i][j]);               printf("%d\n",getans());       }         return 0;  }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.