当前位置:网站首页>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;
}
边栏推荐
- Markdown基础语法
- Yolov3 trains its own data set to generate train txt
- Notes de lecture sur le code propre
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- Thread application instance
- 450-深信服面经1
- 电脑使用哪个录制视频软件比较好
- 线程应用实例
- 453-atoi函数的实现
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
猜你喜欢
新手必看,點擊兩個按鈕切換至不同的內容
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
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
仿京东放大镜效果(pink老师版)
ICDE 2023|TKDE Poster Session(CFP)
Codeworks 5 questions per day (1700 average) - day 4
IEDA refactor的用法
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Data dimensionality reduction factor analysis
随机推荐
MySQL advanced (Advanced) SQL statement
2022 compilation principle final examination recall Edition
C file input operation
MySQL
Refactoring: improving the design of existing code (Part 1)
PHP parser badminton reservation applet development requires online system
A4988 drive stepper motor "recommended collection"
[pytorch learning notes] tensor
《代碼整潔之道》讀書筆記
Typescript 之 快速入门
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
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
In pytorch function__ call__ And forward functions
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
【测试开发】一文带你了解什么是软件测试
横向越权与纵向越权[通俗易懂]
Talk about the design of red envelope activities in e-commerce system
性能测试如何创造业务价值
Hospital online inquiry source code hospital video inquiry source code hospital applet source code