BZOJ 3045 电话线路 暴力

2 篇文章 0 订阅

思路:题干太长,而且很简单,这就不说了。。


思路:本来想着T了就写后缀数组,或者加堆优化什么的,结果直接就A了。。


CODE:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 50010
#define MAXE 5000010
#define BASE 2333
#define INF 0x3f3f3f3f
using namespace std;
 
unsigned long long power[20];
 
struct Complex{
    char s[12];
    int _id,num[10];
    unsigned long long hash,_hash,sorting;
     
    bool operator <(const Complex &a)const {
        return sorting < a.sorting;
    }
    void Read(int p) {
        _id = p;
        scanf("%s",s + 1);
        hash = _hash = 0;
        for(int i = 1; i <= 10; ++i) {
            hash = hash * BASE + s[i];
            ++num[s[i] - '0'];
        }
        for(int i = 0; i <= 9; ++i)
            _hash = _hash * BASE + num[i];
    }
    int LCP(const Complex &a)const {
        for(int i = 1;; ++i)
            if(s[i] != a.s[i])
                return i - 1;
        return 0;
    }   
}src[MAX];
 
int cnt;
int cost[10];
 
void Pretreatment()
{
    power[0] = 1;
    for(int i = 1; i <= 15; ++i)
        power[i] = power[i - 1] * BASE;
}
 
int head[MAX],total;
int next[MAXE],aim[MAXE],length[MAXE];
 
inline void Add(int x,int y,int len)
{
    next[++total] = head[x];
    aim[total] = y;
    length[total] = len;
    head[x] = total;
}
 
inline void AddEdge(int l,int r)
{
    for(int i = l; i <= r; ++i)
        for(int j = i + 1; j <= r; ++j) {
            int lcp = src[i].LCP(src[j]);
            Add(src[i]._id,src[j]._id,cost[lcp]);
            Add(src[j]._id,src[i]._id,cost[lcp]);
        }
}
 
inline void AddEdge(int l,int r,bool flag)
{
    for(int i = l; i <= r; ++i)
        for(int j = i + 1; j <= r; ++j)
            if(src[i]._hash == src[j]._hash) {
                int lcp = src[i].LCP(src[j]);
                Add(src[i]._id,src[j]._id,cost[lcp]);
                Add(src[j]._id,src[i]._id,cost[lcp]);
            }
}
 
int SPFA()
{
    static int f[MAX];
    static bool v[MAX];
    static queue<int> q;
    memset(f,0x3f,sizeof(f));
    f[1] = 0;
    q.push(1);
    while(!q.empty()) {
        int x = q.front(); q.pop();
        v[x] = false;
        for(int i = head[x]; i; i = next[i])
            if(f[aim[i]] > f[x] + length[i]) {
                f[aim[i]] = f[x] + length[i];
                if(!v[aim[i]]) {
                    v[aim[i]] = true;
                    q.push(aim[i]);
                }
            }
    }
    return f[cnt] == INF ? -1:f[cnt];
}
 
int main()
{
    Pretreatment();
    cin >> cnt;   
    for(int i = 0; i <= 9; ++i)
        scanf("%d",&cost[i]);
    for(int i = 1; i <= cnt; ++i)
        src[i].Read(i);
    for(int i = 1; i <= 10; ++i) {
        for(int j = 1; j <= cnt; ++j)
            src[j].sorting = src[j].hash - power[10 - i] * src[j].s[i];
        sort(src + 1,src + cnt + 1);
        unsigned long long now = src[1].sorting;
        int last = 1;
        for(int j = 1; j <= cnt; ++j)
            if(src[j].sorting != now) {
                AddEdge(last,j - 1);
                last = j;
                now = src[j].sorting;           
            }
        AddEdge(last,cnt);
    }
    for(int i = 1; i <= 10; ++i)
        for(int j = i + 1; j <= 10; ++j) {
            for(int k = 1; k <= cnt; ++k)
                src[k].sorting = src[k].hash - power[10 - i] * src[k].s[i] - power[10 - j] * src[k].s[j];
            sort(src + 1,src + cnt + 1);
            unsigned long long now = src[1].sorting;
            int last = 1;
            for(int k = 1; k <= cnt; ++k)
                if(src[k].sorting != now) {
                    AddEdge(last,k - 1,false);
                    last = k;
                    now = src[k].sorting;
                }
            AddEdge(last,cnt,false);
        }
    cout << SPFA() << endl;
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值