Codeforces Round #374 (Div. 2)【A,B,C】

= =C题这种DP打的少吧,记得以前最短路分层图打过这样子的,然后比赛前半个小时才恍然大雾。。。然后瞎几把还打错了,还好A,B手速快。。上分了;

A题:
计算B的连续个数的组数,每组的连续个数;
水题;

#include <iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;

char s[110];
int a[110];

int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",s);
    int num=0;
    int flag=0;
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++)
    {
        if(s[i]=='B')
        {
            if(flag)
            {
                a[num]++;
            }
            else
            {
                a[++num]++;
                flag=1;
            }
        }
        else
        {
            flag=0;
        }
    }
    printf("%d
",num);
    for(int i=1;i<=num;i++)
        printf("%d ",a[i]);
    return 0;
}

B题:
求最少开锁,最晚开锁;= =最后一个是密码;
这题纯暴力模拟,水;

#include <iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
/*

*/

char s[110][110];

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n+1;i++)
    {
        scanf("%s",s[i]);
    }
    int temp=strlen(s[n+1]);
    int mini=0;
    int mimi=0;
    for(int i=1;i<=n;i++)
    {
        int x=strlen(s[i]);
        if(x<temp)
            mini++;
        if(x<=temp&&strcmp(s[i],s[n+1])!=0)
            mimi++;
    }
    int ans1=0;
    if(mini)
    {
        for(int i=1;i<=mini;i++)
        {
            if(i%k==0)
                ans1+=5;
            ans1++;
        }
    }
    printf("%d ",ans1+1);
    int ans2=0;
    if(mimi)
    {
        for(int i=1;i<=mimi;i++)
        {
            if(i%k==0)
                ans2+=5;
            ans2++;
        }
    }
    printf("%d
",ans2+1);
    return 0;
}

C题:
求1-n的路径长度<=T范围内,最多能经过几个点;
思路:
dp[i][j]代表到达 i 经 过 j 个点的最小花费;
然后BFS每个可到达,里面写个小DP;
//注意vis数组开bool,不知道不开会不会MLE,还有图的话最好用链表模拟;

#include <bits/stdc++.h>
using namespace std;
//dp[i][j]代表到达第i个结点,经过j个结点的最短时间;

typedef pair<int,int> PP;
const int INF=0x3f3f3f3f;

int dp[5001][5001];
bool vis[5001][5001];
int pre[5001][5001];
int n,m,T;
int i,j,k;
int u,v,w;
vector<PP>ma[5001];
queue<PP>q;

void bfs()
{
    vis[1][1]=true;
    dp[1][1]=0;
    q.push({1,1});
    while(!q.empty())
    {
        PP u=q.front();q.pop();
        int x=u.first;
        int y=u.second;
        vis[x][y]=false;
        v=ma[x].size();
        for(i=0;i<v;i++)
        {
            int xx=ma[x][i].first;
            int co=ma[x][i].second;
            if(dp[x][y]+co>T)
                continue;
            if(dp[xx][y+1]>dp[x][y]+co)
            {
                dp[xx][y+1]=dp[x][y]+co;
                pre[xx][y+1]=x;
                if(vis[xx][y+1]) continue;
                vis[xx][y+1]=true;
                q.push({xx,y+1});
            }
        }
    }
}
void print()
{
    vector<int>res;
    res.push_back(n);
    while(pre[n][j])
    {
        res.push_back(pre[n][j]);
        n=pre[n][j];
        j--;
    }
    int ans=res.size();
    printf("%d
",ans);
    for(i=ans-1;i>=0;i--)
    {
        printf("%d ",res[i]);
    }
}
int main()
{
    memset(dp,INF,sizeof(dp));
    scanf("%d%d%d",&n,&m,&T);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        ma[u].push_back({v,w});
    }
    bfs();

    for(i=n;i>=1;i--)
    {
        if(dp[n][i]<=T)
        {
            j=i;
            break;
        }
    }
    print();
    return 0;
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934749.html