洛谷 - 挖地雷( dp , dfs搜索 )

题目传送
题意:
给你n个地窖,每个地窖有一定的地雷,再给出地窖的连通情况,(单向边) 问你最多能挖走多少地雷。
在这里插入图片描述
在这里插入图片描述

思路:
这个题由于,数据不大(n <= 20)所以可以dfs爆搜直接a了。

但是他也可以用dp的思想来做,
f[i]表示以 i 地窖结束的所挖的最大地雷数
arr[i] 表示该地窖的地雷数
dp递推方程:当 j 到 i 有路可走时候
f[i] = max(f[j]) + arr[i]。

dfs代码:

#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
const int N = 1e5 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int n;
int di[30],vis[30][30],a[30],ans[30],step[30],Max,Maxstep;
bool check(int x)//检查这个点后面还是否有路可走
{
    for(int i = 1;i <= n;i++)
        if(vis[x][i] && !a[i])
            return false;
    return true;
}
void dfs(int x,int ste,int sum)
{
    if(check(x))
    {
        if(sum > Max)//更新路径
        {
            Max = sum,Maxstep = ste;
            for(int i = 1;i <= ste;i++)
                ans[i] = step[i];
        }
    }
    else
    {
        for(int i = 1;i <= n;i++)
        {
            if(vis[x][i] && !a[i])
            {
                a[i] = 1;
                step[ste+1] = i;
                dfs(i,ste+1,sum+di[i]);
                a[i] = 0;
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> di[i];//每个地窖的地雷数
    for(int i = 1;i <= n-1;i++)
        for(int j = i+1;j <= n;j++)
            cin >> vis[i][j];//表示i到j的连通性
    for(int i = 1;i <= n;i++)//枚举每个起点
    {
        a[i] = 1;//表示这个地窖已经走过了
        step[1] = i;//记录路径
        dfs(i,1,di[i]);
        a[i] = 0;
    }
    for(int i = 1;i <= Maxstep;i++) cout << ans[i] << " ";
    cout << endl << Max << endl;
}

dp代码

#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
const int N = 1e5 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int n,vis[30][30],f[30],arr[30],step[30],Max,x;
void Put(int x)//递归输出路径
{
    if(step[x] == 0) {cout << x << " ";return ;}
    Put(step[x]);//递归
    cout << x << " ";
}
void solve()
{
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            if(vis[j][i] && f[j] > f[i])//dp思想寻找最大的f[j]
            {
                f[i] = f[j];
                step[i] = j;//记录路径
            }
        }
        f[i] += arr[i];
        if(Max < f[i]) Max = f[i],x = i;//记录最大值和结束的地窖
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> arr[i];
    for(int i = 1;i < n;i++)
        for(int j = i+1;j <= n;j++)
            cin >> vis[i][j];//表示i到j有路可走
    solve();
    Put(x);//递归输出路径
    cout << endl << Max << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值