【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 8Sample Output
2題目大意:
? ?就是一個打怪獸的游戲,你可以選擇一個位置打怪獸,你可以打很多怪獸,但是每一行或者每一列最多打一個怪獸(和N皇后問題一樣),每個位置上的怪獸都有一個權值(二維數(shù)組中的值),如果值為0代表這里沒有怪獸,你打完怪獸可以獲得的經驗值為你所有打的怪獸的權值的最小值。問你最大可以獲得多大的權值。
解題報告:
? ?二分這個最小值,然后匈牙利check一下即可。
? ?這題卡常(其實是復雜度本身就不允許),但是加了優(yōu)化可以過。
? 優(yōu)化1:memset改for(不過對這種單組樣例的可能用處不大)
? 優(yōu)化2:每次二分之后都新建邊,這個對二分圖的優(yōu)化較大。
? 優(yōu)化3:可以先離散化數(shù)據(jù)然后對數(shù)據(jù)二分,這樣降低了二分次數(shù)(不過效果并不是很好,甚至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(二分 + 二分图匹配,匈牙利算法,卡常)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 老师用《孤勇者》和小学生对上暗号 大都会
- 下一篇: 最强游戏手机预定 联想骁龙8+旗舰即将登