日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Problem Description

In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an n×nn \times nn×n map, and there are monsters in some grids. Each monster has an experience. As a master, however, Shino has a strange self-restrain. She would kill at most one monster in a column, and also at most one in a row. Now she wants to know how to get max experience, under the premise of killing as many monsters as possible.

Input

The first line contains an integer nnn. (n<=500)
Then n lines follow. In each line there are n integers, and AijAijAij represents the experience of the monster at grid (i,j)(i,j)(i,j). If Aij=0Aij=0Aij=0, there is no monster at grid (i,j)(i,j)(i,j).

The experience is the minimal experience of all the monster which are killed.

It guaranteed that the maximum of the experience of the monster is not larger than 10^9

Output

One integer, the value of max experience.

Sample Input

2 2 0 1 8

Sample Output

2

題目大意:

? ?就是一個打怪獸的游戲,你可以選擇一個位置打怪獸,你可以打很多怪獸,但是每一行或者每一列最多打一個怪獸(和N皇后問題一樣),每個位置上的怪獸都有一個權值(二維數組中的值),如果值為0代表這里沒有怪獸,你打完怪獸可以獲得的經驗值為你所有打的怪獸的權值的最小值。問你最大可以獲得多大的權值。

解題報告:

? ?二分這個最小值,然后匈牙利check一下即可。

? ?這題卡常(其實是復雜度本身就不允許),但是加了優化可以過。

? 優化1:memset改for(不過對這種單組樣例的可能用處不大)

? 優化2:每次二分之后都新建邊,這個對二分圖的優化較大。

? 優化3:可以先離散化數據然后對數據二分,這樣降低了二分次數(不過效果并不是很好,甚至500ms變成了900ms)

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 500 + 5; struct Edge {int to,ne,w; } e[MAX*MAX]; int tot,head[MAX];void add(int u,int v,int w) {e[++tot].to = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot; } int all,tmp,n,a[MAX][MAX]; int nxt[MAX]; bool use[MAX]; bool find(int x) {for(int j = head[x]; ~j; j = e[j].ne) {int v = e[j].to;if(use[v] == 1) continue; if(e[j].w < tmp) continue;use[v]=1;if(nxt[v] == 0 || find(nxt[v])) {nxt[v] = x;return 1;}}return 0 ; } int match() { // memset(nxt,0,sizeof nxt);for(int i = 1; i<=n; i++) nxt[i] = 0;int res = 0;for(int i = 1; i<=n; i++) {for(int i = 1; i<=n; i++) use[i] = 0;if(find(i)) res++;}return res; } bool ok(int x) {tmp = x;if(match() == all) return 1;else return 0 ; } int main() { tot=0;int maxx=0;cin>>n;for(int i = 1; i<=n; i++) head[i] = -1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&a[i][j]);maxx=max(maxx,a[i][j]);}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(a[i][j]) add(i,j,a[i][j]);}}all = match();if(maxx == 0) {printf("0\n");return 0 ;}int l = 1,r = maxx,mid,ans=0;while(l<=r) {mid=(l+r)>>1;tot=0;for(int i = 1; i<=n; i++) head[i] = -1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(a[i][j] >= mid) add(i,j,a[i][j]);}}if(ok(mid)) ans = mid,l = mid+1;else r = mid-1;}printf("%d\n",ans);return 0 ; }

?

總結

以上是生活随笔為你收集整理的【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。