Gym - 101502I最短路变形(2017 JUST Programming Contest 3.0 I. Move Between Numbers)

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

题目:
I. Move Between Numbers

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
inputCopy
1
5 1 5
11111191111191111911
11181111111111818111
11811171817171181111
11111116161111611181
11751717818314111118
output
3
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 <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

const int maxn =250+5;
const int inf =0x3f3f3f3f;
int T,n,s,e,mp[maxn][maxn],common[maxn][10],dis[maxn],vis[maxn];
string a[maxn];

void init()
{
    memset(mp,inf,sizeof(mp));
    memset(common,0,sizeof(common));
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
}

void have_edge(int x,int y)
{
    int ans =0;
    for(int i=0; i<10; i++)
    {
        ans+=min(common[x][i],common[y][i]);
    }
    if(ans==17)
    {
        mp[x][y]=1;
        mp[y][x]=1;
    }
}

void dijkstra()
{
    dis[s]=0;
    vis[s]=1;
    for(int i=1; i<=n; i++)
    {
        if(i!=s&&mp[s][i]<inf)
           dis[i]=mp[s][i];
    }
    int min_dis,u;
    while(true)
    {
        min_dis=inf;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&dis[i]<min_dis)
            {
                min_dis=dis[i];
                u=i;
            }
        }
        if(min_dis==inf)
            return ;
        vis[u]=1;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i])
            {
                if(mp[u][i]==1&&min_dis+mp[u][i]<dis[i])
                dis[i]=min_dis+mp[u][i];
            }
        }
    }
}

int main()
{
  scanf("%d",&T);
  while(T--)
  {
       init();
       int c;
      scanf("%d %d %d",&n,&s,&e);
      for(int i=1; i<=n; i++)
      {
          cin>>a[i];
          for(int j=0; j<20; j++)
          {
              c=a[i][j]-'0';
              common[i][c]++;
          }
      }

      for(int i=1; i<=n; i++)
        for(int j=1;j<=n&&j!=i;j++)
      {
          have_edge(i,j);
      }
      if(mp[s][e]==1)
      {
          printf("1\n");
          continue;
      }
      dijkstra();
      if(dis[e]==inf)
        printf("-1\n");
      else
        printf("%d\n",dis[e]);
  }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值