zoukankan      html  css  js  c++  java
  • 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("
    ");
         }
        return 0;
    }


  • 相关阅读:
    内网穿透(Frp)-拯救没有公网IP的你
    用Windows远程桌面连接树莓派的方法
    TensorFlow 1.9开始支持树莓派
    树莓派制作遥控小车-新手教程
    Layui 一个页面包含多个table时不展示分页条
    MVC 通过@符号把数据赋值给jQuery对象
    jQuery 批量为表单元素赋值
    layui分页组件,一直在调用方法的解决办法
    layui 表格复选框不居中解决办法
    JQuery 解决遮罩层下内容可以滚动问题
  • 原文地址:https://www.cnblogs.com/a249189046/p/7978945.html
Copyright © 2011-2022 走看看