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

牛客 NC24755 [USACO 2010 Dec S]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 P1iP1_iP1i​ (1 <= P1iP1_iP1i​ <= P) and P2iP2_iP2i​ (1 <= P2iP2_iP2i​ <= P) with a distance between them of DiD_iDi​. The sum of all the distances DiD_iDi​ 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.

输入描述:

* 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: P1i,P2i,DiP1_i, P2_i, D_iP1i​,P2i​,Di​

输出描述:

* 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 

输出

复制

12

思路:因为是双向边,因此p1到p2与p2到p1的距离是相等的,于是我们只需要比较p到p1和p到p2那个短再加上p1到p2就是答案;因为目的地数量较少所以可以直接通过两次求最短路找到答案,分别以p1和p2为起点

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

#define PII pair<int ,int>
const int N=100010,M=400010;

int n,m,p1,p2,p;
int h[N],e[M],ne[M],w[M],idx;
int dist[3][N];
bool st[N];
queue<int>q ;

void add(int a,int b,int c)
{
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}

void dijkstra(int dist[],int s)
{
	memset(dist,0x3f,4*N);
	memset(st,0,sizeof st);
	dist[s]=0;
	
	priority_queue<PII,vector<PII>,greater<PII>> q;
	q.push({0,s});
	
	while(!q.empty())
	{
		PII t=q.top();
		q.pop();
		
		int ver=t.second,dis=t.first;
		
		if(st[ver])continue;
		st[ver]=true;
		
		for(int i=h[ver];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dis+w[i])
			{
				dist[j]=dis+w[i];
				q.push({dist[j],j});
			}
		}
	}
}

int main()
{
	memset(h,-1,sizeof h);
	scanf("%d%d%d%d%d",&m,&n,&p,&p1,&p2);
	while(m--)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c),add(b,a,c);
	}
	dijkstra(dist[0],p1);
	dijkstra(dist[1],p2);
	
	cout<<min(dist[0][p],dist[1][p])+dist[0][p2];
}

相关文章:

  • Git做版本管理及CHANGELOG
  • python经典编程100例(1)
  • GO语言 | go work 神一般的管理 多个module没烦恼
  • 【C语言】指针数组
  • 基于51单片机数字电压表仿真设计_数码管显示
  • 种草模式崛起!小红书KOL达人种草成推广热门方向!
  • Git Commit规范指北
  • 易观之星 | “2022年度用户推荐数字应用”投票通道开启
  • Flutter实战之go_router路由组件入门指南
  • Java--MybatisPlus Wrapper构造器;分页;MP代码生成器(四)
  • JS高级(数据类型,数据_变量_内存)
  • 分类模型评估的实际编码
  • 攻防世界WEB练习-mfw
  • 设计模式:设计模式概述
  • 你真的了解并查集?
  • [译]前端离线指南(上)
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 11111111
  • avalon2.2的VM生成过程
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • Java方法详解
  • java概述
  • spring学习第二天
  • 使用 @font-face
  • 原生Ajax
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​MySQL主从复制一致性检测
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​如何防止网络攻击?
  • # .NET Framework中使用命名管道进行进程间通信
  • # 计算机视觉入门
  • #Linux(make工具和makefile文件以及makefile语法)
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (11)MATLAB PCA+SVM 人脸识别
  • (定时器/计数器)中断系统(详解与使用)
  • (二十三)Flask之高频面试点
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (汇总)os模块以及shutil模块对文件的操作
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (转)ORM
  • (转载)hibernate缓存
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • **PHP分步表单提交思路(分页表单提交)
  • .NET Core 版本不支持的问题
  • .NET中使用Protobuffer 实现序列化和反序列化
  • @angular/cli项目构建--Dynamic.Form
  • @ModelAttribute注解使用
  • @PreAuthorize注解
  • [ vulhub漏洞复现篇 ] Hadoop-yarn-RPC 未授权访问漏洞复现
  • [android] 切换界面的通用处理