“亞信科技杯”南郵第七屆大學生程式設計競賽之網路預賽 A noj 2073 FFF [ 二分圖最大權匹配 || 最大費用最大流 ]

來源:互聯網
上載者:User

標籤:

傳送門

FFF 時間限制(普通/Java) :  1000 MS/  3000 MS           運行記憶體限制 : 65536 KByte
總提交 : 145            測試通過 : 13 

題目描述

FFF團,一個異端審判組織,團員活躍在各個角落,每當燒烤節來臨的時候,我們都能聽到他們傳播的那熟悉的旋律:

燒啊~燒啊~燒啊燒啊燒~ (請自行腦補《當》)

FFF團成員內建這樣一個屬性:憑空變出火把與汽油,兩者配合起來才能讓FFF之火duang的一下燒起來,但是不同的火把與不同的汽油配合產生的火焰是不同的,現在有n種火把與n種汽油,已知每一種火把與每一種汽油配合時產生的火焰的旺盛程度,現在求怎樣使得火把與汽油一一配對,產生最旺盛的火焰。


輸入

 

第一行為一個整數T,表示有T組資料

每組資料第一行為一個正整數n(2≤n≤30)

第二行開始一共有n行,每行為n個正整數,第i行第j個數表示第i種火把與第j種汽油配合的火焰的旺盛程度。(0<a[i][j]≤10000)

 

輸出

 

每組資料輸出一個整數,表示最大的火焰旺盛程度

 

範例輸入

2
3
5 2 6
6 7 9
7 4 1

8 5 2 8
5 8 2 1
9 6 3 7
7 5 8 1

範例輸出

20 
33

 

題目來源

kojimai

 

轉一發wdd的題解:

http://blog.csdn.net/u010535824/article/details/44746223

 

A:一對一配對,兩種解法,一種是用二分最大權匹配 km演算法可解 ,二是用最大費用最大流,兩種都是直接套演算法的模板就可以了。

 

下面給出km的做法

 

 

njczy2010 2073 Accepted 0MS   212K 2522Byte G++ 2015-04-01 17:14:34.0

 

  1 #include <cstdio>  2 #include <cstring>  3 #include <stack>  4 #include <vector>  5 #include <algorithm>  6 #include <map>  7 #include <string>  8 #include <queue>  9 #include <cmath> 10  11 #define ll long long 12 int const N = 35; 13 int const M = 100005; 14 int const INF = 0x3f3f3f3f; 15 ll const mod = 1000000007; 16  17 using namespace std; 18  19 int T; 20 int n; 21 int nx,ny;       //兩邊的點數 22 int g[N][N];    //二分圖描述 23 int linker[N],lx[N],ly[N];      //y中各點匹配狀態,x,y中的點標號 24 int slack[N]; 25 bool visx[N],visy[N]; 26  27 bool DFS(int x) 28 { 29     visx[x] = true; 30     for(int y = 0;y < ny;y++) 31     { 32         if(visy[y]) continue; 33         int tmp = lx[x] + ly[y] -g[x][y]; 34         if(tmp == 0) 35         { 36             visy[y] = true; 37             if(linker[y] == -1 || DFS(linker[y])) 38             { 39                 linker[y] = x; 40                 return true; 41             } 42         } 43         else if(slack[y] > tmp) 44             slack[y] = tmp; 45     } 46     return false; 47 } 48  49 int KM() 50 { 51     memset(linker,-1,sizeof(linker)); 52     memset(ly,0,sizeof(ly)); 53     for(int i = 0;i < nx;i++) 54     { 55         lx[i] = -INF; 56         for(int j = 0;j < ny;j++) 57             if(g[i][j] > lx[i]) 58                 lx[i] = g[i][j]; 59     } 60     for(int x =0;x < nx;x++) 61     { 62         for(int i = 0;i < ny ;i++) 63             slack[i] = INF; 64         while(true) 65         { 66             memset(visx,false,sizeof(visx)); 67             memset(visy,false,sizeof(visy)); 68             if(DFS(x)) break; 69             int d = INF; 70             for(int i = 0;i < ny;i++) 71                 if(!visy[i] && d > slack[i]) 72                     d = slack[i]; 73             for(int i = 0 ; i < nx ;i++) 74                 if(visx[i]) 75                     lx[i] -= d; 76             for(int i = 0 ; i < ny ;i++) 77             { 78                 if(visy[i]) ly[i] += d; 79                 else slack[i] -= d; 80             } 81         } 82     } 83     int res = 0; 84     for(int i = 0;i < ny ;i++) 85         if(linker[i] != -1) 86             res += g[ linker[i] ][i]; 87     return res; 88 } 89  90 void ini() 91 { 92     scanf("%d",&n); 93     int i,j; 94     for(i = 0;i < n;i++){ 95         for(j = 0;j < n;j++) 96             scanf("%d",&g[i][j]); 97     } 98     nx = ny =n; 99 }100 101 void solve()102 {103 104 }105 106 void out()107 {108     printf("%d\n",KM());109 }110 111 int main()112 {113     //freopen("data.in","r",stdin);114     //freopen("data.out","w",stdout);115     scanf("%d",&T);116     //for(int cnt=1;cnt<=T;cnt++)117     while(T--)118     //while(scanf("%d%d%d",&a,&b,&n)!=EOF)119     {120         ini();121         solve();122         out();123     }124 }

 

“亞信科技杯”南郵第七屆大學生程式設計競賽之網路預賽 A noj 2073 FFF [ 二分圖最大權匹配 || 最大費用最大流 ]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.