• B - Extended Traffic(边权带负,spfa并且判断负环)


    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:
    ?

    边权带负,spfa,同时需要判断负环,cnt数组记录 

    1. #include <iostream>
    2. #include <cstring>
    3. #include <utility>
    4. #include <queue>
    5. #include <cmath>
    6. #include <string>
    7. using namespace std;
    8. constexpr int N=205;
    9. int s,n,money[N],m,q,tt,dist[N],k=1,cnt[N];
    10. bool st[N];
    11. vector<pair<int,int>>adj[N];
    12. int lf(int u,int v){
    13. return (money[v]-money[u])*(money[v]-money[u])*(money[v]-money[u]);
    14. }
    15. void spfa(){
    16. memset(dist,0x3f,sizeof(dist));
    17. memset(st,0,sizeof(st));
    18. memset(cnt,0,sizeof(cnt));
    19. dist[1]=0;
    20. queue<int>que;
    21. que.push(1);
    22. st[1]=1;
    23. while(!que.empty()){
    24. int t=que.front();
    25. que.pop();
    26. st[t]=0;
    27. for(const auto &[j,w]:adj[t]){
    28. if(dist[j]>dist[t]+w) {
    29. dist[j] = dist[t] + w;
    30. if (st[j] == 0&&cnt[j]<n+1) {
    31. que.push(j);
    32. st[j] = 1;
    33. cnt[j]=cnt[t]+1;
    34. }
    35. }
    36. }
    37. }
    38. }
    39. int main() {
    40. scanf("%d", &s);
    41. while (s--) {
    42. scanf("%d", &n);
    43. for (int i = 1; i <= n; i++) {
    44. adj[i].clear();
    45. scanf("%d", &money[i]);
    46. }
    47. scanf("%d", &m);
    48. for (int i = 1; i <= m; i++) {
    49. int u, v;
    50. scanf("%d%d", &u, &v);
    51. adj[u].push_back(make_pair(v, lf(u, v)));
    52. }
    53. spfa();
    54. printf("Case %d:\n", k);
    55. k++;
    56. scanf("%d", &q);
    57. while (q--) {
    58. scanf("%d", &tt);
    59. if (dist[tt] < 3 || dist[tt] == 0x3f3f3f3f ) {
    60. printf("?\n");
    61. }
    62. else
    63. printf("%d\n", dist[tt]);
    64. }
    65. }
    66. }

     

  • 相关阅读:
    MySQL 自建数据库慢日志分析
    【Flutter】如何优美地实现一个悬浮NavigationBar
    【状语从句练习题】although vs but
    Mybatis-Plus之模块集成和分层改造
    如何优化并提供 Tomcat的启动速度
    解决nodejs报错:TypeError: require(...).sayHi is not a function
    【金融量化】电话口试-智力题
    【一】1D测量 Measuring——gen_measure_rectangle2()算子
    【译】使用 ML.NET 进行机器学习 - 集群完整指南
    过敏婴儿的4大症状 家长要及时发现正确处理很重要
  • 原文地址:https://blog.csdn.net/q619718/article/details/128003546