当前位置:网站首页>AcWing 1127. 香甜的黄油 题解(最短路—spfa)
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
2022-07-02 18:27:00 【乔大先生】
AcWing 1127. 香甜的黄油
还是用spfa算法求最短路,思路就是找到每个牧场为中心点放置糖,找到所有奶牛移动距离和最小的牧场作为结果,输出移动和的最小值
#include<bits/stdc++.h>
using namespace std;
const int N = 810, M = 4000, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
int d[N], q[N];
bool st[N];
int n, p, m;
int id[N]; //记录每个牧场的编号
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
int spfa(int start){
memset(d, 0x3f, sizeof d);
d[start] = 0;
int hh = 0, tt = 1;
q[0] = start;
st[start] = true;
while(hh != tt){
int t = q[hh ++ ];
if(hh == N) hh = 0;
st[t] = false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if(d[j] > d[t] + w[i]){
d[j] = d[t] + w[i];
if(!st[j]){
q[tt ++ ] = j;
if(tt == N) tt = 0;
st[j] = true;
}
}
}
}
int res = 0;
for(int i = 0; i < n; i ++ ){
int j = id[i];
if(d[j] == INF) return INF;
res += d[j];
}
return res;
}
int main()
{
cin>>n>>p>>m;
for(int i = 0; i < n; i ++ ){
cin>>id[i];
}
memset(h, -1, sizeof h);
for(int i = 0; i < m; i ++ ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
}
int minv = INF;
for(int i = 1; i <= p; i ++ ){
minv = min(minv, spfa(i));
}
cout<<minv<<endl;
return 0;
}
边栏推荐
- 思考变量引起的巨大变化
- Yunna | why use the fixed asset management system and how to enable it
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- PHP asymmetric encryption method private key and public key encryption and decryption method
- 以太网PHY层芯片LAN8720A简介
- Use cheat engine to modify money, life and stars in Kingdom rush
- C文件输入操作
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- Progress progress bar
- Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
猜你喜欢

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

搭建哨兵模式reids、redis从节点脱离哨兵集群

Markdown basic grammar

PyTorch函数中的__call__和forward函数

注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Quanzhi A33 uses mainline u-boot

《架构整洁之道》读书笔记(下)

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

数据降维——因子分析

程序猿入门攻略(十二)——数据的存储
随机推荐
Learn the knowledge points of eight part essay ~ ~ 1
以太网PHY层芯片LAN8720A简介
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Imitation Jingdong magnifying glass effect (pink teacher version)
横向越权与纵向越权[通俗易懂]
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
PHP-Parser羽毛球预约小程序开发require线上系统
Reading notes of code neatness
虚拟机初始化脚本, 虚拟机相互免秘钥
2022.7.1-----leetcode. two hundred and forty-one
4274. 后缀表达式-二叉表达式树
metric_ Logger urination
452-strcpy、strcat、strcmp、strstr、strchr的实现
Introduction of Ethernet PHY layer chip lan8720a
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
《代碼整潔之道》讀書筆記
Develop fixed asset management system, what voice is used to develop fixed asset management system
股票证券公司排名,有安全保障吗
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机