HDU 6223 Infinite Fraction Path(BFS+剪枝)

Posted hinata_hajime

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6223 Infinite Fraction Path(BFS+剪枝)相关的知识,希望对你有一定的参考价值。

The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly’s talent and hoped that this talent can help him find the best infinite fraction path before the anniversary.
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled (i2 + 1)%N.
A path beginning from the i-th city would pass through the cities u1,u2,u3, and so on consecutively. The path constructs a real number A[i], called the relevant fraction such that the integer part of it is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[u1], D[u2

], and so on.
The best infinite fraction path is the one with the largest relevant fraction
InputThe input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof test cases.
For each test case, the first line contains the integer N (1 ≤ N ≤ 150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
OutputFor each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
Sample Input
4
3
149
5
12345
7
3214567
9
261025520
Sample Output
Case #1: 999
Case #2: 53123
Case #3: 7166666
Case #4: 615015015

跟某位大佬学的,大概不是标解,但也很有道理的样子
分析:
题目中让我们找最大的,那么每一层都需要保持最大。
所以我们可以将第一层最大的先加入队列,然后将每一层都进行BFS找最大
需要进行的剪枝是 1.如果这一层的值小于最大值就不加入队列,已经加入的直接出队
2.如果已经在该层走到过该点,那么直接不入队。
通过优先队列可以将价值大和步骤多的先进行操作,就能有效的剪枝


代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long ll;
const int MAXN=151000;
int maxx[MAXN];
int max1;
char str[MAXN];
int a[MAXN];
int t,n;
int vis[MAXN];
int num[MAXN];
ll last;
struct node
{
   int val;
   ll id;
   int step;
};
bool operator <(node A,node B)
{
    if(A.step==B.step)return A.val>B.val;
    return A.step>B.step;
}
void bfs()
{

    max1=0;
    priority_queue<node>Q;
    for(int i=0;i<n;i++)
    max1=max(max1,a[i]);
    maxx[1]=max1;
    node a1,next1;
    for(int i=0;i<n;i++)
    {
        if(a[i]==max1)
          {
              a1.id=i;
              a1.val=a[i];
              a1.step=1;
              Q.push(a1);
          }
    }
    last=0;
    int top=0;
    while(!Q.empty())
    {
       a1=Q.top();
       Q.pop();
       if(last<a1.step)
       {
          last=a1.step;
          while(top)vis[num[--top]]=0;
       }
       if(a1.val<maxx[a1.step]||a1.step>=n||vis[a1.id]==1)
       continue;
       num[top]=a1.id;
       vis[num[top++]]=1;
       next1.id=(a1.id*a1.id+1)%n;
       next1.val=a[next1.id];
       next1.step=a1.step+1;
       if(next1.val>=maxx[next1.step])
       {
          maxx[next1.step]=next1.val;
          Q.push(next1);
       }
    }
}
int main()
{
    int Case=0;
     scanf("%d",&t);
     while(t--)
     {
         Case++;
        memset(maxx,0,sizeof(maxx));
        scanf("%d",&n);
        scanf("%s",str);
        for(int i=0;i<n;i++)
        a[i]=str[i]-0;
        bfs();
        printf("Case #%d: ",Case);
        for(int i=1;i<=n;i++)
        printf("%d",maxx[i]);
        printf("\n");
     }
    return 0;
}

 



以上是关于HDU 6223 Infinite Fraction Path(BFS+剪枝)的主要内容,如果未能解决你的问题,请参考以下文章

Infinite Fraction Path(HDU6223 + bfs + 剪枝)

题解 HDU6223 Infinite Fraction Path

HDU6223——2017ICPC徐州G Infinite Fraction Path

ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223

hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

HDOJ6223Infinite Fraction Path(后缀数组)