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 aestarting
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 ns,
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.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
const int maxn = 255;
int n;
int e[maxn][maxn];
const int inf = 99999999; void initial() {
int i, j;
for (i = 1; i < maxn; ++i) {
for (j = 1; j < maxn; ++j) {
if (i == j) {
e[i][j] = 0;
}
else {
e[i][j] = inf;
}
}
}
}
/**
*floyd算法
*/
void floyd() {
int i, j, k;
for (k = 1; k <= n; ++k) {//遍历所有的中间点
for (i = 1; i <= n; ++i) {//遍历所有的起点
for (j = 1; j <= n; ++j) {//遍历所有的终点
if (e[i][j] > e[i][k] + e[k][j]) {//如果当前i-->j的距离大于i-->k--->j的距离之和
e[i][j] = e[i][k] + e[k][j];//更新从i--->j的最短路径
}
}
}
}
} int num[maxn][12];
char s[25]; void init(int i)
{
for(int k=0;k<20;k++)
num[i][s[k]-'0']++;
} int main()
{
// freopen("in.txt","r",stdin);
int T,x,y,i,j,k;
scanf("%d",&T);
while(T--)
{
initial();
memset(num,0,sizeof(num));
scanf("%d%d%d",&n,&x,&y);
for(i=1;i<=n;i++)
{
scanf("%s",&s);
init(i);
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
int com=0;
for(k=0;k<=9;k++)
{
com+=min(num[i][k],num[j][k]);
}
if(com==17)
{//加边
e[i][j] = 1;
e[j][i] = 1;
// cout<<i<<" "<<j<<endl;
}
}
floyd();
if(e[x][y]==inf)
printf("-1\n");
else
printf("%d\n",e[x][y]);
//通过x,y来计算最短路径 }
}

2017 JUST Programming Contest 3.0 I. Move Between Numbers的更多相关文章

  1. 2017 JUST Programming Contest 3.0 D. Dice Game

    D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...

  2. 2017 JUST Programming Contest 2.0 题解

    [题目链接] A - On The Way to Lucky Plaza 首先,$n>m$或$k>m$或$k>n$就无解. 设$p = \frac{A}{B}$,$ans = C_{ ...

  3. gym101532 2017 JUST Programming Contest 4.0

    台州学院ICPC赛前训练5 人生第一次ak,而且ak得还蛮快的,感谢队友带我飞 A 直接用claris的模板啊,他模板确实比较强大,其实就是因为更新的很快 #include<bits/stdc+ ...

  4. 2017 JUST Programming Contest 3.0 B. Linear Algebra Test

    B. Linear Algebra Test time limit per test 3.0 s memory limit per test 256 MB input standard input o ...

  5. 2017 JUST Programming Contest 3.0 H. Eyad and Math

    H. Eyad and Math time limit per test 2.0 s memory limit per test 256 MB input standard input output ...

  6. 2017 JUST Programming Contest 3.0 K. Malek and Summer Semester

    K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  7. 2017 JUST Programming Contest 3.0 E. The Architect Omar

    E. The Architect Omar time limit per test 1.0 s memory limit per test 256 MB input standard input ou ...

  8. gym101343 2017 JUST Programming Contest 2.0

    A.On The Way to Lucky Plaza  (数论)题意:m个店 每个店可以买一个小球的概率为p       求恰好在第m个店买到k个小球的概率 题解:求在前m-1个店买k-1个球再*p ...

  9. 2017 JUST Programming Contest 2.0

    B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...

随机推荐

  1. Jmeter参数Parameters和Body Data区别

    1.如图: 2.有文章说,Parameters是get的参数:Body Data是post的参数:get的参数存在于url中,post的参数存在于body中:   但是我使用jmeter3.3版本测试 ...

  2. 核函数以及SVM相关知识(重点)

    http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988406.html http://blog.pluskid.org/?p=685 考虑我们 ...

  3. nginx access.log 忽略favicon.ico訪问记录的方法

    favicon.ico 文件是浏览器收藏网址时显示的图标,当第一次訪问页面时.浏览器会自己主动发起请求获取页面的favicon.ico文件.当/favicon.ico文件不存在时,服务器会记录404日 ...

  4. ART虚拟机之Trace原理(转)

    一.概述 Android 6.0系统采用的art虚拟机,所有的Java进程都运行在art之上,当应用发生ANR(Application Not Response,其中最终的一个环节便是向目标进程发送信 ...

  5. instancetype VS id

    英文好的直接读下面链接的文章就好了: http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using- ...

  6. string 是值类型,还是引用类型(.net)[转]

    转自http://hi.baidu.com/newfzks/item/b805f0f4edb0810dd89e7290 string 是值类型,还是引用类型(.net) 一. string 类型的用法 ...

  7. sparse-PCA(稀疏主成分分析)是什么?

    不多说,直接上干货! 复杂降维技术有spare-PCA和sparse coding. 最近在科研需要,感谢下面的博主. Sparse PCA 稀疏主成分分析

  8. 更改Mysql登录密码

    版本号49之前的跨域设置 在Windows命令行下修改mysql数据库密码步骤如下: 1.通过dos命令进入mysql的bin目录: 2.输入“mysql -uroot -p”,回车进入mysql命令 ...

  9. putty字体大小颜色、全屏/退出全屏快捷键 保存session设置[转]

    字体大小设置 Window->Appearance->Font settings->Change按钮设置(我的设置为16)字体为(Consolas) 字体颜色设置 Window-&g ...

  10. linux kernel编译配置相关

    1 配置界面的搜索功能 “/”可以进行模块搜索,搜索结果里面还有依赖信息.非常方便. 2 在使用纯内核,不实用module的时候,很多情况下出错是因为相应的特性为编译进内核 案例一: 块设备已经发现了 ...