当前位置:网站首页>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;
}
边栏推荐
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- C文件输入操作
- 451-memcpy、memmove、memset的实现
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- [test development] takes you to know what software testing is
- Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- A4988 drive stepper motor "recommended collection"
- NPOI导出Excel2007
- 横向越权与纵向越权[通俗易懂]
猜你喜欢

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

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

Advanced performance test series "24. Execute SQL script through JDBC"

Codeworks 5 questions per day (1700 average) - day 4

Markdown basic grammar

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Imitation Jingdong magnifying glass effect (pink teacher version)

Reading notes of "the way to clean structure" (Part 2)
随机推荐
451-memcpy、memmove、memset的实现
Novice must see, click two buttons to switch to different content
MySQL
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
程序猿入门攻略(十二)——数据的存储
End to end object detection with transformers (Detr) paper reading and understanding
二进制操作
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
PHP parser badminton reservation applet development requires online system
NPOI导出Excel2007
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
IEDA refactor的用法
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
How to print mybats log plug-in using XML file
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Getting started with typescript
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
PHP asymmetric encryption method private key and public key encryption and decryption method
第七章-类基础