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;
    }


  • 相关阅读:
    过滤器
    自定义指令
    Window setTimeout() 方法
    Window setInterval()方法
    ThingJS官方案例(五):物联网室内3D定位导航,上下楼怎么办?
    基于WebGL的虚拟太阳系漫游技术实现 ThingJS 科幻片
    ThingJS官方案例(四):快速应用3D场景下的模拟导航能力
    ThingJS官方示例(三):3D标记“Marker”跳跃、闪烁和发光动画效果
    ThingJS官方示例(二):利用电子标注判断物联网设备的位置
    ThingJS 官方示例(一):禁区告警的3D电子围栏可视化
  • 原文地址:https://www.cnblogs.com/a249189046/p/7978945.html
Copyright © 2011-2022 走看看