noip模拟赛

T1 decode

解哈夫曼编码

sol:

因为哈夫曼编码的性质,我们直接暴力就可以了

#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
int n;
string s;
char ch[10010],ans[10010];
int len[60];
map<string,char> hsh;
string yy,nu;
int main()
{
    freopen("decode.in","r",stdin);
    freopen("decode.out","w",stdout);
    n = read();
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        getchar();char cc = getchar();
        hsh[s] = cc;
        //cout<<hsh["0"];
    }
    scanf("%s",ch + 1);
    int m = strlen(ch + 1);
    for(int i=1;i<=m;i++)
    {
        yy = yy + ch[i];
        if(hsh[yy])
        {
            cout<<hsh[yy];
            yy = nu;
        }
    }
}
View Code

 

T2 build

有 $n$ 个点 $m$ 条边,每个点补魔花费的时间为 $t_i$

现在你给第一个点补魔,剩下的点开始补魔当且仅当有一个跟它相邻的点补魔完毕

求每个点补魔完毕的时间

sol:

裸最短路,意会一下

或者,每个点拆成两个点,补魔前向补魔后连长度为 $t_i$ 的边

然后连边就是 $u$ 后向 $v$ 前连长度为 $0$ 的边,$u$ 前向 $v$ 后连长度为 $0$ 的边

每个点的答案就是这个补魔后的这个点的 $dis$

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 100010;
int n,m,t[maxn];
int first[maxn],to[maxn << 2],nx[maxn << 2],val[maxn << 2],cnt;
inline void add(int u,int v)
{
    to[++cnt] = v;
    nx[cnt] = first[u];
    first[u] = cnt;
    //val[cnt] = w;
}
int dis[maxn],vis[maxn];
#define pa pair<int,int>
#define mp make_pair
priority_queue<pa,vector<pa>,greater<pa> > q;
void spfa()
{
    memset(dis,127,sizeof(dis));
    dis[1] = 0;q.push(mp(0,1));
    while(!q.empty())
    {
        int now = q.top().second;q.pop();
        if(vis[now])continue;
        vis[now] = 1;
        for(int i=first[now];i;i=nx[i])
        {
            if(dis[to[i]] > dis[now] + t[now])
            {
                dis[to[i]] = dis[now] + t[now];
                q.push(mp(dis[to[i]],to[i]));
            }
        }
    }
}
signed main()
{
    freopen("build.in","r",stdin);
    freopen("build.out","w",stdout);
    n = read(),m = read();
    for(int i=1;i<=n;i++)t[i] = read();
    for(int i=1;i<=m;i++)
    {
        int u = read(),v = read();
        add(u,v);add(v,u);
    }
    spfa();
    for(int i=1;i<=n;i++)
        printf("%lld\n",dis[i] + t[i]);
}
View Code

 

 

T3 不方便说

放在下一篇博客里

100 + 100 + 70 = 270

%%% Echo_Hapurubokka

转载于:https://www.cnblogs.com/Kong-Ruo/p/9924546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值