当前位置: 首页 > news >正文

洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

P3003 [USACO10DEC]苹果交货Apple Delivery

题目描述

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000)

cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000.

What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course.

Consider this map of bracketed pasture numbers and cowpaths with distances:

               3        2       2
           [1]-----[2]------[3]-----[4]
             \     / \              /
             7\   /4  \3           /2
               \ /     \          /
               [5]-----[6]------[7]
                    1       2

If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is:

5 -> 6-> 7 -> 4 -> 3 -> 2 -> 1

with a total distance of 12.

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了P(1<=P<=100000)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。

现在,贝西要从牧场PB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。

输入输出格式

输入格式:

 

  • Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2

  • Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i

 

输出格式:

 

  • Line 1: The shortest distance Bessie must travel to deliver both apples

 

输入输出样例

输入样例#1:
9 7 5 1 4 
5 1 7 
6 7 2 
4 7 2 
5 6 1 
5 2 4 
4 3 2 
1 2 3 
3 2 2 
2 6 3 
输出样例#1:
12 
/*
    基本上是个最短路模板题吧。。
    就是求出t1和t2间的最短路a1
    再求min(dis[s][t1],dis[s][t2])
    两者相加即可
    本来以为这题没这么简单,随便一写结果A了
    我用的堆优化Dij
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 100010
int n,m,s,t1,t2,dis[maxn],num,head[maxn];
bool vis[maxn];
struct node{
    int id,d;
    bool operator < (const node b)const{
        return d>b.d;
    }
};
struct Node{
    int to,pre,v;
}e[200010*2];
void Insert(int from,int to,int v){
    e[++num].to=to;
    e[num].v=v;
    e[num].pre=head[from];
    head[from]=num;
}
void Dij(int st,int tt1,int tt2){
    int flag=0;
    priority_queue<node>q;
    memset(dis,127/3,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[st]=0;node now;
    now.id=st;now.d=0;
    q.push(now);
    while(!q.empty()){
        now=q.top();q.pop();
        int u=now.id;
        if(vis[u])continue;
        vis[u]=1;
        if(u==tt1)flag++;
        if(u==tt2)flag++;
        if(flag==2)return;
        for(int i=head[u];i;i=e[i].pre){
            int to=e[i].to;
            if(dis[u]+e[i].v<dis[to]){
                dis[to]=dis[u]+e[i].v;
                node nxt;nxt.d=dis[to];nxt.id=to;
                q.push(nxt);
            }
        }
    }
}
int main(){
    //freopen("Cola.txt","r",stdin);
    scanf("%d%d%d%d%d",&m,&n,&s,&t1,&t2);
    int x,y,z;
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        Insert(x,y,z);
        Insert(y,x,z);
    }
    int a1,a2=0x7fffffff;
    Dij(s,t1,t2);
    a1=min(dis[t1],dis[t2]);
    Dij(t1,t2,t2);
    a2=min(dis[t2],a2);
    Dij(t2,t1,t1);
    a2=min(dis[t1],a2);
    cout<<a1+a2;
}

 

转载于:https://www.cnblogs.com/thmyl/p/7498891.html

相关文章:

  • linux添加ip白名单_为什么IP代理需要授权?
  • 事件对象练习
  • yii beforeaction 如何赋值全局变量_讲讲 js 的内存泄漏、如何监控和分析
  • Java基础总结--变量、运算符总结
  • boolean mybatis_mybatis的环境搭建以及问题
  • 软件工程实践2017第二次作业
  • python django步骤_python - django (创建到运行流程)
  • CODEVS——T 1004 四子连棋
  • linux查看显卡信息_如何查看linux系统的相关信息
  • 华宇笔试题总结
  • system.objectdisposedexception: 已释放该集合_集合啦!动物森友会夏季更新第 2 弹!烟火大会、梦境参观、复原储存资料即将来袭...
  • JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?jQuery中的text()、html()和val() ?...
  • python 逗号作用 语句间_python逗号作用
  • SDUT_2116 数据结构实验之链表一:顺序建立链表
  • 华三模拟器hcl实验手册_Caffeinated 6.828:实验 1:PC 的引导过程
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • JavaScript DOM 10 - 滚动
  • JavaScript 一些 DOM 的知识点
  • Java应用性能调优
  • Js基础——数据类型之Null和Undefined
  • laravel 用artisan创建自己的模板
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • SpiderData 2019年2月23日 DApp数据排行榜
  • spring boot 整合mybatis 无法输出sql的问题
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 深度解析利用ES6进行Promise封装总结
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 学习笔记:对象,原型和继承(1)
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • (3)nginx 配置(nginx.conf)
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (二)fiber的基本认识
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (算法设计与分析)第一章算法概述-习题
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • ******之网络***——物理***
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .net 设置默认首页
  • .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试
  • .NET开发者必备的11款免费工具
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken
  • @property @synthesize @dynamic 及相关属性作用探究
  • @拔赤:Web前端开发十日谈
  • [100天算法】-二叉树剪枝(day 48)
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [BZOJ1089][SCOI2003]严格n元树(递推+高精度)