码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • A - Til the Cows Come Home


    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

    Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

    Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

    Input

    * Line 1: Two integers: T and N

    * Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

    Output

    * Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

    Sample

    InputcopyOutputcopy
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    90

    Hint

    INPUT DETAILS:

    There are five landmarks.

    OUTPUT DETAILS:

    Bessie can get home by following trails 4, 3, 2, and 1.

    Sponsor

    提交poj疯狂给我ce报错,试了网上多个题解版本也一样,我直接默认我做对了吧 主要是怕之后找这个码找不到了 当成发个blog保存一下

    1. /*Where there is light, in my heart.*/
    2. /*SUMMER_TRAINING DAY 25*/
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. //
    10. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    11. //#define INF 0x3f3f3f
    12. #define ll long long
    13. #define INF 0x3f3f3f3f
    14. #define mem(a,b) memset(a,b,sizeof(a))
    15. #define unmap(a,b) unordered_map
    16. #define unset(a) unordered_set
    17. #define F first
    18. #define S second
    19. #define pb push_back
    20. #define rep(i, a, b) for (int i = (a); i <= (b); ++i)
    21. #define _rep(i, a, b) for (int i = (a); i >= (b); --i)
    22. #define mode 1e4+7
    23. #define pi acos(-1)
    24. #define U_queue priority_queue,greater>
    25. typedef double db;
    26. typedef pair<int,int> PII;
    27. typedef pair PLL;
    28. typedef vector<int> vi;
    29. const int N = 2005;
    30. //
    31. int n,m;
    32. int q[N],e[N],ne[N],dist[N],idx,h[N],w[N];
    33. bool st[N];
    34. //
    35. void add(int a,int b,int c){
    36. e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
    37. }
    38. //
    39. int dijkstra(){
    40. mem(dist,0x3f);
    41. U_queue q;
    42. q.push({0,1});
    43. dist[1]=0;
    44. dist[1]=0;
    45. while(q.size()){
    46. auto t=q.top();
    47. q.pop();
    48. int ver=t.S,d=t.F;
    49. if(st[ver]) continue;
    50. st[ver]=true;
    51. for(int i=h[ver];i!=-1;i=ne[i]){
    52. int j=e[i];
    53. if(dist[j]>dist[ver]+w[i])
    54. dist[j]=dist[ver]+w[i];
    55. q.push({dist[j],j});
    56. }
    57. }
    58. return dist[n];
    59. }
    60. signed main(){
    61. cin>>m>>n;
    62. mem(h,-1);
    63. while(m--){
    64. int a,b,c;
    65. cin>>a>>b>>c;
    66. add(a,b,c);
    67. }
    68. cout<<dijkstra()<
    69. }
    70. //made by shun 20220728

  • 相关阅读:
    Springboot JSON 转换:Jackson篇
    AUTOSAR汽车电子嵌入式编程精讲300篇-基于 AUTOSAR 的混合动力汽车BMS 应用层软件开发(中)
    【python】time库知识整理
    Java中的this关键字
    C++ 继承原理。
    Embedding 模型部署及效果评测
    如何构建前端自动化测试套件?
    Redis事务操作与原理剖析
    Matlab:确定数值类
    k8s(3)
  • 原文地址:https://blog.csdn.net/LanceLSf/article/details/126026840
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号