NOIP模拟题 2016.10.4 [Hash] [dp] [复杂状态spfa]

这里写图片描述
这里写图片描述


T1:
一开始写的“康托展开”,然而发现这里并不能完美适用,因为很小的时候有重复,那么双hash就过了。
还有一种就直接排序后用set判断不同的个数。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 10005;
const int maxl = 105;
const int SIGMA_SIZE = 30;
char s[maxl];
int n;
struct Hash
{
    int hash1,hash2;
    bool operator < (const Hash t) const
    {
        if(hash1^t.hash1) return hash2 < t.hash2;
        return hash1 < t.hash1;
    }
    bool operator == (const Hash t) { return hash1==t.hash1 && hash2==t.hash2; }
}_hash[maxn];
unsigned int pool[SIGMA_SIZE];
int main()
{
    freopen("word.in","r",stdin);
    freopen("word.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        int lens = strlen(s);
        memset(pool,0,sizeof(pool));
        for(int j=0;j<lens;j++) pool[s[j]-'A'+1]++;
        for(int j=1;j<=26;j++)
        {
            _hash[i].hash1=(_hash[i].hash1<<5)^(_hash[i].hash1>>27)^pool[j];
            _hash[i].hash2=(_hash[i].hash2<<7)^(_hash[i].hash2>>25)^pool[j];
        }
    }
    sort(_hash+1,_hash+n+1);
    int tot = unique(_hash+1,_hash+n+1)-_hash-1;
    printf("%d",tot);
    return 0;
}

T2:
这个虽然正解就是贪心,但是总感觉有些说不通,并且不好想,索性写DP。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn = 100005;
int a[maxn],min1,_round;
int n;
void init()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    sort(a+1,a+n+1,greater<int>());
    min1=a[n-1],_round=a[n];
    n-=2;
}
LL last[2],now[2];
LL work()
{
    memset(last,0x3f,sizeof(last));
    last[0]=0;
    for(int i=1;i<=n;i++)
    {
        swap(last,now);
        memset(now,0x3f,sizeof(now));
        if(i^n) now[1] = last[0] + a[i] + ((LL)min1<<1) + _round;
        now[0] = min(i==1?INF:last[1],last[0]+a[i]+_round);
    }
    return now[0]+min1;
}
int main()
{
    freopen("river.in","r",stdin);
    freopen("river.out","w",stdout);
    init();
    LL ans = work();
    printf(AUTO,ans);
    return 0;
}

T3:
光从算法的角度看,就是一个spfa变形就好,然而内存256M,QAQ
空间优化:手写queue
时间优化:用邻接链表存储禁止的三元组,然后查询的时候就放到一个disable里面,O(n)预处理,O(1)查询。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 3005;
const int maxm = 20005;
struct Edge
{
    int to,next;
    int val;
}edge[maxm<<1];
int head[maxn];
int maxedge;
inline void addedge(int u,int v,int c)
{
    edge[++maxedge] = (Edge) { v,head[u],c };
    head[u] = maxedge;
    edge[++maxedge] = (Edge) { u,head[v],c };
    head[v] = maxedge;
}
const int maxBAN = 100005;
struct Ban
{
    int to,next;
}banne[maxBAN];
int ban[maxn][maxn];
int maxban;
inline void addban(int a,int b,int c)
{
    banne[++maxban] = (Ban) { c,ban[a][b] };
    ban[a][b] = maxban;
}
int n,m,k;
inline void init()
{
    memset(head,-1,sizeof(head)); maxedge=-1;
    memset(ban,-1,sizeof(ban)); maxban=-1;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        addedge(x,y,1);
    }
    for(int i=1;i<=k;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        addban(x,y,z);
    }
}
struct Node
{
    int last,u;
    Node (const int _last = 0,const int _u = 0) { last = _last; u = _u; }
};
int dis[maxn][maxn],pre[maxn][maxn];
bool inque[maxn][maxn];
Node que[maxn*maxn];
bool disable[maxn];
pair <int,int> spfa(int S,int T)
{
    int lim = (maxn-3)*(maxn-3);
    int _head=0 , _tail=0;
    memset(dis,0x3f,sizeof(dis));
    que[++_tail]=Node(0,S); inque[0][S]=true; dis[0][S]=0;
    while(_tail^_head)
    {
        if(_head==lim) _head-=lim;
        Node now = que[++_head];
        inque[now.last][now.u]=false;
        memset(disable,0,sizeof(disable));
        for(int i=ban[now.last][now.u];~i;i=banne[i].next) disable[banne[i].to]=true;
        for(int i=head[now.u];~i;i=edge[i].next)
        {
            int v = edge[i].to;
            if(disable[v]) continue;
            int tmp = dis[now.last][now.u] + edge[i].val;
            if(dis[now.u][v] > tmp)
            {
                dis[now.u][v] = tmp; pre[now.u][v] = now.last;
                if(inque[now.u][v]) continue;
                if(_tail==lim) _tail=0; que[++_tail]=Node(now.u,v); inque[now.u][v]=true;
            }
        }
    }
    int ret = INF , pos;
    for(int i=1;i<=n;i++) if(ret > dis[i][T])
    {
        ret = dis[i][T];
        pos = i;
    }
    return make_pair(ret,pos);
}
int path[maxn],top;
void print(int u,int v)
{
    if(!pre[u][v])
    {
        path[++top]=u; path[++top]=v;
        return;
    }
    print(pre[u][v],u);
    path[++top]=v;
}
int main()
{
    freopen("path.in","r",stdin);
    freopen("path.out","w",stdout);
    init();
    pair <int,int> ans = spfa(1,n);
    print(ans.second,n);
    printf("%d\n",ans.first);
    for(int i=1;i<top;i++) printf("%d ",path[i]);
    printf("%d",path[top]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值