poj2531

题目感觉是图的题。。但是做的是POJ简单搜索的专题,嗯,没有思路。。看了大神的代码才学会的,回溯+dfs。。

等以后图论学好了,在回头用图论做,我在网上看到有大神用最大割来做,加星星了,等回头在用别的方法来做,先贴上DFS回溯的代码,后面学会最大割再用最大割的方法来A。最近心态爆炸,不知道为啥,总感觉难道有1说1,有2说2的性格不好嘛?不喜欢空口说大话表面功夫。。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=22;
int data[maxn][maxn];
bool book[maxn];
int n;
int ans=0;
void init()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
            cin>>data[i][j];
    memset(book,0,sizeof(book));
}
void dfs(int local,int sum)
{
    book[local]=1;
    int temp=sum;
    for(int i=1;i<=n;i++)
        if(book[i])
            temp=temp-data[i][local];
        else
            temp=temp+data[i][local];
    ans=temp>ans?temp:ans;
    if(temp>sum)
        for(int i=local+1;i<=n;++i)
            dfs(i,temp);
    book[local]=0;
}
int main()
{
    init();
    dfs(1,0);
    cout<<ans<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值