zoukankan      html  css  js  c++  java
  • I. Move Between Numbers

    题目:http://codeforces.com/gym/101502/problem/I

    I. Move Between Numbers
    time limit per test
    2.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    You are given n magical numbers a1, a2, ..., an, such that the length of each of these numbers is 20 digits.

    You can move from the ith number to the jth number, if the number of common digits between ai and aj is exactly 17 digits.

    The number of common digits between two numbers x and y is computed is follow:

    .

    Where countXi is the frequency of the ith digit in the number x, and countYi is the frequency of the ith digit in the number y.

    You are given two integers s and e, your task is to find the minimum numbers of moves you need to do, in order to finish at number ae starting from number as.

    Input

    The first line contains an integer T (1 ≤ T ≤ 250), where T is the number of test cases.

    The first line of each test case contains three integers n, s, and e (1 ≤ n ≤ 250) (1 ≤ s, e ≤ n), where n is the number of magical numbers, s is the index of the number to start from it, and e is the index of the number to finish at it.

    Then n lines follow, giving the magical numbers. All numbers consisting of digits, and with length of 20 digits. Leading zeros are allowed.

    Output

    For each test case, print a single line containing the minimum numbers of moves you need to do, in order to finish at number ae starting from number as. If there is no answer, print -1.

    Example
    Input
    1
    5 1 5
    11111191111191111911
    11181111111111818111
    11811171817171181111
    11111116161111611181
    11751717818314111118
    Output
    3
    Note

    In the first test case, you can move from a1 to a2, from a2 to a3, and from a3 to a5. So, the minimum number of moves is 3 moves.

    题意:1个样咧

    5串字符     从第1串字符开始    到第5串字符结束

    。。。。(5串字符)

    两串字符中有17个数相同就说明两串字符间有一条边联系

    求1------》5的最短经过几个字符串

    思路:    建立图,,,先暴力把两串间能连起来的边设为1,,其它设为inf

              dijkstra 直接求两点最短路   复杂度是o(n*n),,如果数据打就用优先队列优化

     

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<algorithm>
    #include<map>
    #define maxn 1005
    #define inf 1e9+7
    using namespace std;
    int mp[maxn][maxn];
    int dis[maxn];
    int pre[maxn];
    void chushihua(){
        int i, j;
        for (i = 1; i < maxn; ++i) {
            for (j = 1; j < maxn; ++j) {
                if (i == j) {
                    mp[i][j] = 0;
                }
                else {
                    mp[i][j] = inf;
                }
            }
        }
    }
    void dijkstra(int n,int v)
    {
        bool vis[maxn];
        for(int i=1;i<=n;i++)
        {
            vis[i]=false;
            dis[i]=mp[v][i];
            if(dis[i]==inf)
            {
                pre[i]=0;
            }
            else pre[i]=v;
        }
        dis[v]=0;
        vis[v]=true;
        for(int i=2;i<=n;i++)
        {
                int u=v;
                int mazz=inf;
                for(int j=1;j<=n;j++){
            if(!vis[j]&&dis[j]<mazz)
            {
                    u=j;
                    mazz=dis[j];
            }
            }
            vis[u]=true;
            for(int k=1;k<=n;k++)
            {
                if(!vis[k]&&mp[u][k]<inf)
                {
                    if(dis[k]>mp[u][k]+dis[u]){
                        dis[k]=mp[u][k]+dis[u];
                        pre[k]=u;
                    }
                }
            }
    
        }
    }
    int main()
    {
        int T,x,y,k,n;
        int num[maxn][12];
        char s[25];
        scanf("%d",&T);
        while(T--)
        {
            chushihua();
            memset(num,0,sizeof(num));
            scanf("%d%d%d",&n,&x,&y);
            for(int i=1;i<=n;i++)
            {
                scanf("%s",&s);
                for(int k=0;k<20;k++)
            num[i][s[k]-'0']++;
            }
            for(int i=1;i<n;i++)
                for(int j=i+1;j<=n;j++)
            {
                int com=0;
                for(int k=0;k<=9;k++)
                {
                    com+=min(num[i][k],num[j][k]);
                }
                if(com==17)
                {
                    mp[i][j] = 1;
                    mp[j][i] = 1;
                }
            }
            dijkstra(n,x);
            if(dis[y]==inf)
               cout<<"-1"<<endl;
            else
                cout<<dis[y]<<endl;
        }
    }

    简单题。但算是图论上的练练手吧

  • 相关阅读:
    JS 随机整数
    微信小程序 功能函数 支付接口
    JS 正则表达式
    JS 日期 自动补齐 “2017-11-22 14:43”
    schema get_ddl
    StringBuffer 清空
    java中split任意数量的空白字符
    美国法官工资
    纪检委,检察院的工资
    国家司法机构
  • 原文地址:https://www.cnblogs.com/huangzzz/p/8342513.html
Copyright © 2011-2022 走看看