• 牛客 NC24858 [USACO 2009 Nov S]Job Hunt


    题目描述

    Bessie is running out of money and is searching for jobs. Farmer John knows this and wants the cows to travel around so he has imposed a rule that his cows can only make D (1 <= D <= 1,000) dollars in a city before they must work in another city. Bessie can, however, return to a city after working elsewhere for a while and again earn the D dollars maximum in that city. There is no limit on the number of times Bessie can do this.
    Bessie's world comprises P (1 <= P <= 150) one-way paths connecting C (2 <= C <= 220) cities conveniently numbered 1..C. Bessie is currently in city S (1 <= S <= C). Path i runs one-way from city Ai to city Bi (1 <= Ai <= C; 1 <= Bi <= C) and costs nothing to traverse.
    To help Bessie, Farmer John will give her access to his private jet service. This service features F (1 <= F <= 350) routes, each of which is a one way flight from one city Ji to a another Ki (1 <= Ji <= C; 1 <= Ki <= C) and which costs Ti (1 <= Ti <= 50,000) dollars. Bessie can pay for the tickets from future earnings if she doesn't have the cash on hand.
    Bessie can opt to retire whenever and wherever she wants. Given an unlimited amount of time, what is the most money that Bessie can make presuming she can make the full D dollars in each city she can travel to? Print -1 if there is no limit to this amount.

    输入描述:

    * Line 1: Five space-separated integers: D, P, C, F, and S
    * Lines 2..P+1: Line i+1 contains two space-separated integers that name a one-way path from one city to another: Ai and Bi
    * Lines P+2..P+F+1: Line P+i+1 contains three space-separated integers that name a one-way jet flight from one city to another and the price for that flight: Ji, Ki, and Ti

    输出描述:

    * Line 1: A single integer representing the most money she can make while following the law.

    示例1

    输入

    复制

    100 3 5 2 1
    1 5
    2 3
    1 4
    5 2 150
    2 5 120

    输出

    复制

    250

    说明

    This world has five cities, three paths and two jet routes. Bessie starts out in city 1, and she can only make 100 dollars in each city before moving on.
    Bessie can travel from city 1 to city 5 to city 2 to city 3, and make a total of 4*100 - 150 = 250 dollars.

    思路:这是一道比较裸的最长路问题,只要将起点的距离初始化为d,建图时两点之间的路径距离为d,航线距离为d-c;

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N=230,M=100010;
    7. int h[N],ne[M],e[M],w[M],idx;
    8. int d,m,n,f,s;
    9. int dist[N];
    10. bool st[N];
    11. void add(int a,int b,int c)
    12. {
    13. e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
    14. }
    15. void spfa()
    16. {
    17. memset(dist,-0x3f,sizeof dist);
    18. queue<int> q;
    19. dist[s]=d;
    20. q.push(s);
    21. while(q.size())
    22. {
    23. int t=q.front();
    24. q.pop();
    25. st[t]=0;
    26. for(int i=h[t];~i;i=ne[i])
    27. {
    28. int j=e[i];
    29. if(dist[j]
    30. {
    31. dist[j]=dist[t]+w[i];
    32. if(!st[j])
    33. {
    34. q.push(j);
    35. st[j]=1;
    36. }
    37. }
    38. }
    39. }
    40. }
    41. int main()
    42. {
    43. memset(h,-1,sizeof h);
    44. scanf("%d%d%d%d%d",&d,&m,&n,&f,&s);
    45. while(m--)
    46. {
    47. int a,b;
    48. cin>>a>>b;
    49. add(a,b,d);
    50. }
    51. while(f--)
    52. {
    53. int a,b,c;
    54. cin>>a>>b>>c;
    55. add(a,b,d-c);
    56. }
    57. spfa();
    58. int res=-0x3f3f3f3f;
    59. for(int i=1;i<=n;i++)
    60. res=max(res,dist[i]);
    61. cout<
    62. }

  • 相关阅读:
    史上最全架构师知识图谱(纯干货)
    1798_GNU pdf阅读器evince_支持的格式
    C语言函数指针
    测试人员如何打造自己的核心影响力
    飞项聊职场:剖析产品经理和项目经理关键技能
    php资源列表|开发者应知晓
    C语言之常用的排序算法
    网络安全复习笔记
    后续遍历非递归算法
    HTTP网络知识
  • 原文地址:https://blog.csdn.net/qq_62242287/article/details/126671527