10.26考试爆炸记

emm…
绝地反击场
(1)copycat
判断两个字符串是否存在小写字母的唯一对应关系

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
using namespace std;
inline int read(){
    int i=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch);ch=getchar())
        if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar())
        i=(i<<3)+(i<<1)+(ch^48);
    return i*f;
}
int buf[1024];
inline void write(int x){
    if(!x){putchar('0');return ;}
    if(x<0){putchar('-');x=-x;}
    while(x){buf[++buf[0]]=x%10,x/=10;}
    while(buf[0]) putchar(buf[buf[0]--]+48);
    return ;
}
#define stan 1111
#define sten 33
char s1[stan],s2[stan];
int T,len1,len2,rev[sten],tag;
signed main(){
    T=read();
    for(register int i=1;i<=T;++i){
        gets(s1);
        gets(s2);
        memset(rev,-1,sizeof(rev));
        tag=true;
        len1=strlen(s1);
        len2=strlen(s2);
        if(len1!=len2){
            puts("0");
            continue;
        }
        for(int i=0;i<len1&&tag;++i)
            if(s1[i]<='z'&&s1[i]>='a'){
                if(s2[i]>'z'||s2[i]<'a'){
                    puts("0");
                    tag=false;
                    break;
                }
                if(rev[s1[i]-'a']!=-1&&s2[i]-'a'!=rev[s1[i]-'a']){
                    puts("0");
                    tag=false;
                    break;
                }
                else rev[s1[i]-'a']=s2[i]-'a';
            }else
                if(s1[i]!=s2[i]){
                    puts("0");
                    tag=false;
                    break;
                }
        if(tag) puts("1");
    }
    return 0;
}

(2)running

双权值最短路
先kruskal一波

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
using namespace std;
inline int read(){
    int i=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch);ch=getchar())
        if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar())
        i=(i<<3)+(i<<1)+(ch^48);
    return i*f;
}
int buf[1024];
inline void write(long long x){
    if(!x){putchar('0');return ;}
    if(x<0){putchar('-');x=-x;}
    while(x){buf[++buf[0]]=x%10,x/=10;}
    while(buf[0]) putchar(buf[buf[0]--]+48);
    return ;
}
#define stan 555555
#define sten 2222222
int n,m,lim,x,y,S,T;
int tot,first[stan],nxt[sten],goal[sten],fa[stan];
long long to[stan],dis[sten];
bool exi[stan],tag;
struct edg{
    int a,b,c,t;
}edge[sten];
priority_queue<pair<long long,int> >que;
void addedge(int a,int b,long long c){
    nxt[++tot]=first[a];first[a]=tot;goal[tot]=b;dis[tot]=c;
    nxt[++tot]=first[b];first[b]=tot;goal[tot]=a;dis[tot]=c;
    return; 
}
bool cmp(const edg &x,const edg &y){
    return x.t<y.t;
}
inline int getfa(int x){
    if(fa[x]!=x) return fa[x]=getfa(fa[x]);
    else return x;
}
void dij(){
    for(int i=1;i<=n;++i)
        to[i]=-1;
    to[S]=0;
    que.push(make_pair(-to[S],S));
    while(!que.empty()){
        int u=que.top().second;que.pop();
        if(exi[u]) continue;
        exi[u]=true;
        for(int p=first[u];p;p=nxt[p])
            if(to[goal[p]]>to[u]+dis[p]||to[goal[p]]==-1){
                to[goal[p]]=to[u]+dis[p];
                que.push(make_pair(-to[goal[p]],goal[p]));
            }
    }
    return ;
}
signed main(){
    n=read();m=read();
    for(register int i=1;i<=n;++i)
        fa[i]=i;
    for(register int i=1;i<=m;++i){
        edge[i].a=read();
        edge[i].b=read();
        edge[i].t=read();
        edge[i].c=read();
    }
    S=read();T=read();
    sort(edge+1,edge+m+1,cmp);
    for(register int i=1;i<=m;++i){
        if(tag&&edge[lim].t<edge[i].t) break;
        x=getfa(edge[i].a);
        y=getfa(edge[i].b);
        if(x!=y) fa[x]=y;
        lim=i;
        x=getfa(S);
        y=getfa(T);
        if(x==y) tag=true;
    }
    for(register int i=1;i<=lim;++i)
        addedge(edge[i].a,edge[i].b,(long long)edge[i].t*edge[i].c);
    dij();
    write(edge[lim].t);putchar(' ');write(to[T]);
    return 0;
}

(3)toyuq

树形DP,
f[i][j][0] 为从 i 出发,回到i
f[i][j][1] 为从 i 出发,只出不回
f[i][j][2]为只经过 i <script type="math/tex" id="MathJax-Element-250">i</script>

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<queue>
using namespace std;
inline int read(){
    int i=0,f=1;
    char ch;
    for(ch=getchar();!isdigit(ch);ch=getchar())
        if(ch=='-') f=-1;
    for(;isdigit(ch);ch=getchar())
        i=(i<<3)+(i<<1)+(ch^48);
    return i*f;
}
int buf[1024];
inline void write(long long x){
    if(!x){putchar('0');return ;}
    if(x<0){putchar('-');x=-x;}
    while(x){buf[++buf[0]]=x%10,x/=10;}
    while(buf[0]) putchar(buf[buf[0]--]+48);
    return ;
}
#define stan 333
#define sten 666
int tot,first[stan],nxt[sten],goal[sten],dis[sten];
int f[stan][stan][3],rev[stan][stan],tmp[stan],ans,a,b,c,n,T,w[stan],t[stan];
void addedge(int a,int b,int c){
    nxt[++tot]=first[a];first[a]=tot;goal[tot]=b;dis[tot]=c;
    nxt[++tot]=first[b];first[b]=tot;goal[tot]=a;dis[tot]=c;
    return; 
}
void dfs(int u,int fa){
    for(int i=t[u];i<=T;++i)
        f[u][i][0]=f[u][i][1]=f[u][i][2]=w[u];
    for(int p=first[u];p;p=nxt[p])
        if(goal[p]!=fa){
            dfs(goal[p],u);
            for(int i=0;i<=T;++i)
                tmp[i]=f[u][i][2];
            for(int i=0;i<=T;++i){
                for(int j=0;j<=i-dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-dis[p]-j][1]+f[goal[p]][j][1]);
                for(int j=0;j<=i-2*dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-2*dis[p]-j][0]+f[goal[p]][j][2]);
                for(int j=0;j<=i-2*dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-2*dis[p]-j][2]+f[goal[p]][j][0]);
            }
            for(int i=0;i<=T;++i)
                f[u][i][2]=tmp[i];
            for(int i=0;i<=T;++i)
                tmp[i]=f[u][i][1];
            for(int i=0;i<=T;++i){
                for(int j=0;j<=i-dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-dis[p]-j][0]+f[goal[p]][j][1]);    
                for(int j=0;j<=i-2*dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-2*dis[p]-j][1]+f[goal[p]][j][0]);
            }
            for(int i=0;i<=T;++i)
                f[u][i][1]=tmp[i];
            for(int i=0;i<=T;++i)
                tmp[i]=f[u][i][0];
            for(int i=0;i<=T;++i)
                for(int j=0;j<=i-2*dis[p]-t[u];++j)
                    tmp[i]=max(tmp[i],f[u][i-2*dis[p]-j][0]+f[goal[p]][j][0]);
            for(int i=0;i<=T;++i)
                f[u][i][0]=tmp[i];
        }
}
signed main(){
    n=read();T=read();
    for(int i=1;i<=n;++i)
        w[i]=read();
    for(int i=1;i<=n;++i)
        t[i]=read();
    for(int i=1;i<n;++i){
        a=read();b=read();c=read();
        addedge(a,b,c);
    }
    dfs(1,0);
    for(int i=1;i<=n;++i)
        for(int j=0;j<=T;++j)
            for(int k=0;k<=2;++k)
                ans=max(ans,f[i][j][k]);
    write(ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值