当前位置:网站首页>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;
}
边栏推荐
- [pytorch learning notes] tensor
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- Yolov3 trains its own data set to generate train txt
- 线程应用实例
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
- Data dimensionality reduction factor analysis
- 性能测试如何创造业务价值
- Which video recording software is better for the computer
- 搭建主从模式集群redis
- golang:[]byte转string
猜你喜欢
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

In pytorch function__ call__ And forward functions

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

Use cheat engine to modify money, life and stars in Kingdom rush

Refactoring: improving the design of existing code (Part 1)
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Bubble sort array

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

Juypter notebook modify the default open folder and default browser

Watchful pioneer world outlook Architecture - (how does a good game come from)
随机推荐
MySQL advanced (Advanced) SQL statement
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
2022.7.1-----leetcode.241
Npoi export Excel2007
Refactoring: improving the design of existing code (Part 1)
NPOI导出Excel2007
Use cheat engine to modify money, life and stars in Kingdom rush
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
Date tool class (updated from time to time)
数字滚动带动画
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Getting started with typescript
数据降维——主成分分析
Qpropertyanimation use and toast case list in QT
PHP非对称加密方法私钥及公钥加密解密的方法
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
SIFT feature point extraction "suggestions collection"
MySQL高级(进阶)SQL语句
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