首页 > 其他 > 详细

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

时间:2019-10-20 21:15:29      阅读:60      评论:0      收藏:0      [点我收藏+]

题目

搜索 可行性剪枝

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

但是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;
}

谢谢收看, 祝身体健康!

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

原文:https://www.cnblogs.com/yanxiujie/p/11708278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!