1. 程式人生 > >HDU 6223 Infinite Fraction Path(BFS+剪枝)

HDU 6223 Infinite Fraction Path(BFS+剪枝)

優先 val == d+ mil 如果 inf 2.6 ring

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+剪枝)