CodeForces505B【floyd】/C【记忆化搜索】

25 篇文章 0 订阅

CodeForces505B

题意:

有M种不同颜色的线,给出这样的图,然后q个询问,问两点可以由多少种线连接。

思路:

其实这题很水,也是不必写题解的。
但是呢!wa了两遍woc…
写这题主要也是看到了大哥曾经写wa了。。然后他还没补!(真是怠惰
然后我直接就是100次floyd,然后wa了!!!不讲道理啊!这都wa!
然后实在想不出,也不想去打什么BFS/DFS/并查集,然后就去题解看看呗,然后果然都是啥BFS/DFS/并查集这种做法,然后还是自己看看吧。。然后惊了!!拉了个floyd模板过了!
然后发现前面的floyd全部打错了,哈哈哈哈。。。

代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int Maxn = 1e2+10;

int edge[Maxn][Maxn][Maxn];
int n, m, q;

void floyd(int col){
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if((!edge[col][i][j]) && edge[col][i][k] && edge[col][k][j])
        edge[col][i][j] = edge[col][j][i] = 1;
}

int main(){
    int ans, u, v, c;
    memset(edge, 0, sizeof(edge));
    scanf("%d%d", &n, &m);
    for(int i=0;i<m;i++){
        scanf("%d%d%d", &u, &v, &c);
        edge[c][u][v] = edge[c][v][u] = 1;
    }
    for(int i=1;i<=m;i++) floyd(i);

    scanf("%d", &q);
    for(int i=0;i<q;i++){
        scanf("%d%d", &u, &v);
        ans = 0;
        for(int j=1;j<=m;j++){
            ans += edge[j][u][v];
        }
        printf("%d\n", ans);
    }
    return 0;
}

CodeForces505C

题意:

思路:

这个呀~
真是有点意思啊~
如果直接dp[cur_pos][pre_step],直接硬赛给pre_step,空间复杂度炸了。
因为最差的d就是1,然后最多会跳几次呢?(1+x)*x<=30000;
然后就很小呀~
那么第二维就搞个相对值就好了,然后本地跑好像机子开了…三天没关过机好慢…

代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int INF = 0x3f3f3f3f;
const int Maxn = 3e4+10;

int p[Maxn];
int dp[Maxn][550];
int n, d;

int Mmax(int x,int y){
    return x > y ? x : y;
}
int change(int pos){
    return pos-d+250;
}

int DFS(int pos, int pre_step){
    int tmp;
    tmp = change(pre_step);
    if(dp[pos][tmp] != -1) return dp[pos][tmp];
    int new_step, temp = 0;

    new_step = pre_step;
    if(new_step+pos<=30000&&new_step>=1) temp = Mmax(DFS(pos+new_step, new_step), temp);
    new_step = pre_step-1;
    if(new_step+pos<=30000&&new_step>=1) temp = Mmax(DFS(pos+new_step, new_step), temp);
    new_step = pre_step+1;
    if(new_step+pos<=30000&&new_step>=1) temp = Mmax(DFS(pos+new_step, new_step), temp);

    dp[pos][tmp] = p[pos] + temp;
    return dp[pos][tmp];
}

int main(){
    int pos;
    scanf("%d%d", &n, &d);
    for(int i=0;i<n;i++){
        scanf("%d", &pos);
        p[pos]++;
    }

    memset(dp, -1, sizeof(dp));

    printf("%d", DFS(d, d));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值