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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223

参考题解:https://blog.csdn.net/qq_40482495/article/details/78492841

注意优先队列自定义比较级的用法!!

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define mst(a,b) memset((a),(b),sizeof(a))
 6 #define mp(a,b) make_pair(a,b)
 7 #define pi acos(-1)
 8 #define pii pair<int,int>
 9 #define pb push_back
10 const int INF = 0x3f3f3f3f;
11 const double eps = 1e-6;
12 const int MAXN = 15e4 + 10;
13 const int MAXM = 2e6 + 10;
14 
15 char s[MAXN];
16 int a[MAXN], ans[MAXN], vis[MAXN], nex[MAXN];
17 
18 struct node {
19     int pos,val,dep;
20 };
21 
22 struct cmp {
23     bool operator()(const node &x,const node &y) {
24         if(x.dep != y.dep) return x.dep > y.dep;
25         else if(x.val != y.val) return x.val < y.val;
26         return x.pos < y.pos;
27     }
28 };
29 
30 int main() {
31 #ifdef local
32     freopen("data.txt", "r", stdin);
33 #endif
34     int cas = 1;
35     int t;
36     scanf("%d",&t);
37     while(t--) {
38         int n;
39         scanf("%d",&n);
40         scanf("%s",s);
41         int mx = 0;
42         for(int i = 0; i < n; i++) {
43             ans[i] = vis[i] = -1;
44             a[i] = s[i] - '0';
45             mx = max(mx, a[i]);
46             nex[i] = (1ll * i * i + 1) % n;
47         }
48         priority_queue<node, vector<node>, cmp >q;
49         for(int i = 0; i < n; i++)
50             if(a[i] == mx) q.push({i,a[i],0});
51         while(!q.empty()) {
52             node now = q.top();
53             q.pop();
54             int pos = now.pos, val = now.val, dep = now.dep;
55             if(dep >= n) break;
56             if(ans[dep] > val) continue;
57             else if(ans[dep] == val && vis[pos] == dep) continue;
58             ans[dep] = val, vis[pos] = dep;
59             q.push({nex[pos],a[nex[pos]],dep + 1});
60         }
61         printf("Case #%d: ",cas++);
62         for(int i = 0; i < n; i++) printf("%d",ans[i]);
63         printf("
");
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/scaulok/p/9744004.html