• 2022.11.25Extended Traffic LightOJ - 1074


    Dhaka city is getting crowded and noisy everyday. Certain roads always remain blocked for congestion. In order to convince people avoid shortest routes as it's the number one reason for roads crowded; the city authority has come up with a new plan.

    Each junction of the city is marked with a positive integer (≤ 20) denoting the busy-ness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets an amount of money (busy-ness of destination - busy-ness of source)3 (that means the cube of the difference) from the traveler.

    Now, the authority has appointed you to find the minimum total amount that can be earned when someone goes from a certain junction (the zero point) to several others.

    Input

    Input starts with an integer T (≤ 50), denoting the number of test cases.

    Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively.

    The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.

    Output

    For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a ?.

    Sample

    InputcopyOutputcopy
    2
    
    5
    6 7 8 9 10
    6
    1 2
    2 3
    3 4
    1 5
    5 4
    4 5
    2
    4
    5
    
    2
    10 10
    1
    1 2
    1
    2
    
    Case 1:
    3
    4
    Case 2:
    ?

    原题链接:传送门 

    题意:给出n个点的权值(不超过20),点i到点j之间的距离(如果可达)为(a[j]-a[i])^3,求到点的最短路径

    思路:有负环,所以选择spfa

    ps:反而stl要比数组模拟的循环队列快一点

    1. #include
    2. typedef long long ll;
    3. const int M = 1006;
    4. const int N = 1e6 + 9;
    5. bool st[N];
    6. int n, m;
    7. int dist[N], c[N], q[N], ft[N];
    8. int h[M], w[N], e[N], ne[N], idx;
    9. void add(int a, int b, int c){
    10. e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
    11. }
    12. void spfa(){
    13. int hh = 0, tt = 0;
    14. memset(dist, 0x3f, sizeof dist);
    15. memset(ft, 0, sizeof ft);
    16. dist[1] = 0;
    17. q[tt++] = 1;
    18. // std::queueq;
    19. // q.push(1);
    20. st[1] = true;
    21. ft[1] = 1;
    22. while (hh != tt /*q.size()*/){
    23. int t = q[hh++];
    24. if (hh == N) hh = 0;
    25. // int t = q.front();
    26. // q.pop();
    27. st[t] = false;
    28. for (int i = h[t]; i != -1; i = ne[i]){
    29. int j = e[i];
    30. if (dist[j] > dist[t] + w[i]){
    31. dist[j] = dist[t] + w[i];
    32. if (!st[j] && ft[j] <= n){
    33. ft[j]++;
    34. q[tt++] = j;
    35. // q.push(j);
    36. if (tt == N) tt = 0;
    37. st[j] = true;
    38. }
    39. }
    40. }
    41. }
    42. }
    43. int main()
    44. {
    45. int t;
    46. scanf("%d", &t);
    47. int cnt = 1;
    48. while (t--){
    49. scanf("%d", &n);
    50. memset(h, -1, sizeof h);
    51. memset(st, 0, sizeof st);
    52. for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    53. scanf("%d", &m);
    54. while (m--){
    55. int a, b;
    56. scanf("%d%d", &a, &b);
    57. int vule = int(pow(c[b] - c[a], 3));
    58. add(a, b, vule);
    59. }
    60. spfa();
    61. // for(int i=1;i<=n;i++) printf("%d ",dist[i]);
    62. // puts("");
    63. int h;
    64. scanf("%d", &h);
    65. printf("Case %d:\n", cnt++);
    66. while (h--){
    67. int x;
    68. scanf("%d", &x);
    69. if (dist[x] == 0x3f3f3f3f || dist[x] < 3 || ft[x] > n) printf("?\n");
    70. else printf("%d\n", dist[x]);
    71. }
    72. }
    73. return 0;
    74. }

    1. #include
    2. typedef long long ll;
    3. const int M = 1006;
    4. const int N = 1e6 + 9;
    5. bool st[N];
    6. int n, m;
    7. int dist[N], c[N] /*, q[N]*/, ft[N];
    8. int h[M], w[N], e[N], ne[N], idx;
    9. void add(int a, int b, int c){
    10. e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
    11. }
    12. void spfa()
    13. {
    14. int hh = 0, tt = 0;
    15. memset(dist, 0x3f, sizeof dist);
    16. memset(ft, 0, sizeof ft);
    17. dist[1] = 0;
    18. // q[tt ++ ] = 1;
    19. std::queue<int> q;
    20. q.push(1);
    21. st[1] = true;
    22. ft[1] = 1;
    23. while (q.size()){
    24. // int t = q[hh ++ ];
    25. // if (hh == N) hh = 0;
    26. int t = q.front();
    27. q.pop();
    28. st[t] = false;
    29. for (int i = h[t]; i != -1; i = ne[i]){
    30. int j = e[i];
    31. if (dist[j] > dist[t] + w[i]){
    32. dist[j] = dist[t] + w[i];
    33. if (!st[j] && ft[j] <= n){
    34. ft[j]++;
    35. // q[tt ++ ] = j;
    36. q.push(j);
    37. // if (tt == N) tt = 0;
    38. st[j] = true;
    39. }
    40. }
    41. }
    42. }
    43. }
    44. int main(){
    45. int t;
    46. scanf("%d", &t);
    47. int cnt = 1;
    48. while (t--){
    49. scanf("%d", &n);
    50. memset(h, -1, sizeof h);
    51. memset(st, 0, sizeof st);
    52. for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    53. scanf("%d", &m);
    54. while (m--){
    55. int a, b;
    56. scanf("%d%d", &a, &b);
    57. int vule = int(pow(c[b] - c[a], 3));
    58. add(a, b, vule);
    59. }
    60. spfa();
    61. // for(int i=1;i<=n;i++) printf("%d ",dist[i]);
    62. // puts("");
    63. int h;
    64. scanf("%d", &h);
    65. printf("Case %d:\n", cnt++);
    66. while (h--){
    67. int x;
    68. scanf("%d", &x);
    69. if (dist[x] == 0x3f3f3f3f || dist[x] < 3 || ft[x] > n) printf("?\n");
    70. else printf("%d\n", dist[x]);
    71. }
    72. }
    73. return 0;
    74. }

     

  • 相关阅读:
    flutter实现底部弹出框
    HIVE数据导入ES并避免字段空值占用空间
    浅谈以驱动为中心的运维架构
    [二进制漏洞]PWN学习之格式化字符串漏洞 Linux篇
    【软件测试】使用边界值分析法和等价类划分法计算佣金
    【算法训练营】 - ⑨ 贪心算法
    【无标题】
    HDU - 2859 Phalanx(DP)
    RTP GB28181 文件测试工具
    2.6 PE结构:导出表详细解析
  • 原文地址:https://blog.csdn.net/weixin_62802134/article/details/128059459