当前位置:网站首页>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;
}
边栏推荐
- 2022 software engineering final exam recall Edition
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- Qpropertyanimation use and toast case list in QT
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- C的内存管理
- 【ERP软件】ERP体系二次开发有哪些危险?
- 第七章-类基础
- 《架构整洁之道》读书笔记(下)
- ICDE 2023|TKDE Poster Session(CFP)
猜你喜欢
zabbix5客户端安装和配置
450-深信服面经1
How performance testing creates business value
搭建哨兵模式reids、redis从节点脱离哨兵集群
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Novice must see, click two buttons to switch to different content
Processing strategy of message queue message loss and repeated message sending
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
In pytorch function__ call__ And forward functions
随机推荐
PHP asymmetric encryption method private key and public key encryption and decryption method
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Markdown basic grammar
开发固定资产管理系统,开发固定资产管理系统用什么语音
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
新手必看,點擊兩個按鈕切換至不同的內容
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
二进制操作
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
MySQL
移动机器人路径规划:人工势场法[通俗易懂]
In pytorch function__ call__ And forward functions
《重构:改善既有代码的设计》读书笔记(上)
Reading notes of "the way to clean structure" (Part 2)
Digital scroll strip animation
2022 software engineering final exam recall Edition
ORA-01455: converting column overflows integer datatype
High frequency interview questions