当前位置:网站首页>AcWing 1127. Sweet butter solution (shortest path SPFA)
AcWing 1127. Sweet butter solution (shortest path SPFA)
2022-07-02 19:39:00 【Mr. Qiao Da】
AcWing 1127. Sweet butter
Or use it spfa Algorithm for the shortest path , The idea is to find each pasture as the central point to place sugar , Find all cows moving distance and the smallest pasture as a result , Minimum value of output moving sum
#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]; // Record the number of each farm
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;
}
边栏推荐
- Think about the huge changes caused by variables
- Istio1.12:安装和快速入门
- Infix expression is converted to suffix expression (C language code + detailed explanation)
- Py's interpret: a detailed introduction to interpret, installation, and case application
- KT148A语音芯片ic的硬件设计注意事项
- 4274. Suffix expression - binary expression tree
- Gmapping code analysis [easy to understand]
- Qpropertyanimation use and toast case list in QT
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
猜你喜欢
Dictionaries
PHP parser badminton reservation applet development requires online system
Refactoring: improving the design of existing code (Part 2)
Build a master-slave mode cluster redis
Introduction to program ape (XII) -- data storage
程序猿入门攻略(十二)——数据的存储
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
冒泡排序数组
随机推荐
Infix expression is converted to suffix expression (C language code + detailed explanation)
How to print mybats log plug-in using XML file
checklistbox控件用法总结
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
4274. 后缀表达式-二叉表达式树
AcWing 1137. Select the best line solution (the shortest circuit)
SQLite 3.39.0 发布,支持右外连接和全外连接
高并发下如何避免产生重复数据?
AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
《MongoDB入门教程》第03篇 MongoDB基本概念
What is the MySQL backup suffix_ MySQL backup restore
Think about the huge changes caused by variables
Chapter 7 - class foundation
程序猿入门攻略(十二)——数据的存储
AcWing 343. 排序 题解(floyd性质实现传递闭包)
zabbix5客户端安装和配置
IEDA refactor的用法
KS004 基于SSH通讯录系统设计与实现
Thread application instance