jQuery火箭图标返回顶部代码 - 站长素材
jQuery火箭图标返回顶部代码 - 站长素材

洛谷p1559运动员最佳匹配问题

题目

搜索 可行性剪枝

虽然这题目是我搜二分图的标签搜到的

但是n比较小

明显可以暴力

然而只有80分

再加上可行性剪纸就行啦

就是记所有运动员他所能匹配到的最大值、

在我们搜索到第i层的时候

如果他后边的运动员的最大值加起来还比当前已经搜到的最优解还小的话

就把他减掉

Code:

//bao li
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 30;
int n, a[N][N], b[N][N], maxn[N], ans;
bool vis[N];
int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
    while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
    return s * w;
}
void dfs(int dep, int w) {
    if(dep > n) {ans = max(ans, w); return;} 
    int sum = 0;
    for(int i = dep; i <= n; i++) sum += maxn[i];
    if(w + sum < ans) return;
    for(int i = 1; i <= n; i++) 
        if(!vis[i]) {
            vis[i] = 1;
            dfs(dep + 1, w + a[dep][i] * b[i][dep]);
            vis[i] = 0;
        }
}
int main() {
    n = read();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            a[i][j] = read();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            b[i][j] = read();
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            maxn[i] = max(maxn[i], a[i][j] * b[j][i]);
    dfs(1, 0);
    cout << ans << endl;
    return 0;
}

谢谢收看, 祝身体健康!

posted @ 2019-10-20 17:38  lzpclxf  阅读(161)  评论(0编辑  收藏  举报