当前位置:网站首页>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;
}
边栏推荐
- MySQL
- 线程应用实例
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- Memory management of C
- Hospital online inquiry source code hospital video inquiry source code hospital applet source code
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- PHP非对称加密方法私钥及公钥加密解密的方法
- Digital scroll strip animation
- Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
猜你喜欢

Imitation Jingdong magnifying glass effect (pink teacher version)

数据降维——因子分析

论文导读 | 机器学习在数据库基数估计中的应用

性能测试如何创造业务价值

High frequency interview questions
冒泡排序数组

新手必看,点击两个按钮切换至不同的内容

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

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

End-to-End Object Detection with Transformers(DETR)论文阅读与理解
随机推荐
冒泡排序数组
Data dimensionality reduction principal component analysis
Use cheat engine to modify money, life and stars in Kingdom rush
zabbix5客户端安装和配置
Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
SIFT feature point extraction "suggestions collection"
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
2022 software engineering final exam recall Edition
开发固定资产管理系统,开发固定资产管理系统用什么语音
Markdown基础语法
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
PyTorch函数中的__call__和forward函数
Microservice technology - distributed global ID in high concurrency
Usage of ieda refactor
Date tool class (updated from time to time)
Digital scroll strip animation
以太网PHY层芯片LAN8720A简介
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
【测试开发】一文带你了解什么是软件测试