当前位置:网站首页>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;
}
边栏推荐
- AcWing 1128. 信使 题解(最短路—Floyd)
- Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
- pxe装机「建议收藏」
- MySQL function
- 基于SSM实现网上购物商城系统
- mysql函数
- AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
- Golang concurrent programming goroutine, channel, sync
- golang:[]byte转string
- Use cheat engine to modify money, life and stars in Kingdom rush
猜你喜欢
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Refactoring: improving the design of existing code (Part 1)
字典
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
《架构整洁之道》读书笔记(下)
Py's interpret: a detailed introduction to interpret, installation, and case application
搭建哨兵模式reids、redis从节点脱离哨兵集群
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
随机推荐
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Juypter notebook modify the default open folder and default browser
AcWing 1129. 热浪 题解(最短路—spfa)
Introduction to mongodb chapter 03 basic concepts of mongodb
编写完10万行代码,我发了篇长文吐槽Rust
Codeforces Round #802 (Div. 2) 纯补题
定了,就是它!
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
golang:[]byte转string
What is the MySQL backup suffix_ MySQL backup restore
函数高阶-柯里化实现
Py's interpret: a detailed introduction to interpret, installation, and case application
Reading notes of "the way to clean structure" (Part 2)
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
Horizontal ultra vires and vertical ultra vires [easy to understand]
Correspondence between pytoch version, CUDA version and graphics card driver version
Chapter 7 - class foundation
AcWing 1126. 最小花费 题解(最短路—dijkstra)