QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#582281#6621. Luggage Lockgates_orzTL 4ms18660kbC++173.1kb2024-09-22 15:54:542024-09-22 15:54:54

Judging History

你现在查看的是最新测评结果

  • [2024-09-22 15:54:54]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:18660kb
  • [2024-09-22 15:54:54]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

typedef long long LL;
#define int LL
#define inl inline
const int N = 3e5 + 10;
const int M = N * 2;
//const int mod=998244353;
const int mod = 1000000007;
const double eps = 1e-8;
//const int mod=1e9+7;
typedef pair<int,int> PII;
//const int INF=0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m;

void become_faster() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int h[N],e[M],ne[M],idx,w[M];
PII dist[10010];
int vis[10010];
map<PII,int>mm;
void add(int a,int b,int c) {
    //if(mm[{a,b}]||mm[{b,a}])return;
    //mm[{a,b}]=mm[{b,a}]=1;
    e[idx]=b;
    ne[idx]=h[a];
    w[idx]=c;
    h[a]=idx++;
}
void dijk(int s,int id,int t)
{
    //memset(dist, 0x3f, sizeof(dist));
    dist[s] = {id,0};
    priority_queue<PII, vector<PII>, greater<PII> >heap;
    heap.push({ 0,s });
    while (!heap.empty())
    {
        //cout<<"s"<<endl;
        PII tmp = heap.top();
        heap.pop();
        int ver = tmp.second, dis = tmp.first;
        if (vis[ver]==id)continue;
        vis[ver] = id;
        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j].first!=id||dist[j].second > dis + w[i])
            {
                dist[j].second = dis + w[i];
                dist[j].first=id;
                if(j==t)return;
                heap.push({ dist[j].second,j });
            }
        }
    }
}

int to_dig(string s) {
    int res=0;
    for(auto c:s) {
        res=res*10+(c-'0');
    }
    return res;
}
void init() {
    memset(h,-1,sizeof(h));
    for(int i=0;i<=10000;i++)dist[i].second=INF;
    for(int i=0;i<=9999;i++) {
        string s=to_string(i);
        while(s.size()!=4)s="0"+s;


        for(int st=0;st<4;st++) {
            for (int ed = st; ed < 4; ed++) {
                string t = s;
                for (int k = st; k <= ed; k++) {
                    int x = s[k] - '0';
                    x++;
                    if (x >= 10)x = 0;
                    t[k] = x + '0';
                }

                int tmp=to_dig(t);
                //if(i==1234)cout<<"t="<<t<<endl;
                add(i,tmp,1);
                add(tmp,i,1);
            }
        }

        for(int st=0;st<4;st++) {
            for (int ed = st; ed < 4; ed++) {
                string t = s;
                for (int k = st; k <= ed; k++) {
                    int x = s[k] - '0';
                    x--;
                    if (x <0 )x = 9;
                    t[k] = x + '0';
                }

                int tmp=to_dig(t);
                //if(i==1234)cout<<"t="<<t<<endl;
                add(i,tmp,1);
                add(tmp,i,1);
            }
        }
    }
}
int Case;
void solve() {
    int a,b;
    cin>>a>>b;

    ++Case;
    dijk(a,Case,b);
    cout<<dist[b].second<<endl;
}

signed main() {
    become_faster();
    int T = 1;
    //T=read();
    init();
    cin>>T;
    //for(int i=1;i<=100;i++)solve(i);
    while (T--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 18660kb

input:

6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468

output:

1
1
4
5
1
4

result:

ok 6 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

100000
9138 9251
0887 4577
2745 6294
6883 1197
8654 2502
7867 7941
7505 2717
1497 7726
3337 8042
2767 8782
5311 4368
9711 6229
7105 8101
3553 6673
6544 7940
8848 8920
1458 6315
4605 2931
5505 7310
5963 4765
7625 0418
4049 0136
2078 6899
3339 1080
2276 6814
4249 4182
1861 0824
8975 8926
6447 6965
226...

output:

3
6
6
8
9
7
6
7
10
9
6
8
5
4
8
5
8
7
9
4
8
8
8
6
8
5
7
5
7
5
7
7
6
8
4
10
6
5
6
7
7
7
4
7
8
6
5
9
7
6
7
11
10
10
7
5
6
5
7
6
7
9
5
5
6
6
8
5
7
9
10
6
10
5
6
7
6
6
8
6
8
7
7
8
7
8
6
7
6
5
5
6
6
5
7
5
9
6
7
7
7
9
6
9
5
5
8
10
7
3
5
6
7
9
8
6
6
7
10
5
5
6
9
8
6
8
6
5
7
6
11
5
9
6
7
5
10
7
5
8
9
8
7
7
7...

result: