当前位置:网站首页>[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
2022-07-07 11:46:00 【Twilight_ years】
Abstract problem :
Find the minimum value of the sum of the shortest distances of all cows to a certain point .
Because it's two-way , It can be regarded as the minimum value of the shortest distance from a certain point to all other points .
therefore , It can be regarded as the single source shortest path problem , Because it is a sparse graph and has no negative edges , So you can use heap optimized dij perhaps spfa
import java.io.*;
import java.util.*;
class Main{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static final int N=10000;
static final int INF=(int)1e8;
static int[]dist=new int[N];
static int[]h=new int[N];
static int[]ne=new int[N];
static int[]e=new int[N];
static int[]w=new int[N];
static int[]nums=new int[N];
static boolean[] st=new boolean[N];
static int idx;
static int n,p,c;
public static void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
public static void dijskra(int start){
PriorityQueue<int[]>pq=new PriorityQueue<>(p,(a,b)->a[1]-b[1]);
Arrays.fill(dist,INF);
Arrays.fill(st,false);
dist[start]=0;
pq.add(new int[]{start,0});
while(!pq.isEmpty()){
int[]cur=pq.poll();
int v=cur[0];
int d=cur[1];
if(st[v])continue;
st[v]=true;
for(int i=h[v];i!=-1;i=ne[i]){
int j=e[i];
if(dist[j]>d+w[i]){
dist[j]=d+w[i];
pq.add(new int[]{j,dist[j]});
}
}
}
}
public static void main(String[] args)throws IOException{
String[] s=br.readLine().split(" ");
n=Integer.parseInt(s[0]);
p=Integer.parseInt(s[1]);
c=Integer.parseInt(s[2]);
for(int i=1;i<=n;i++){
int t=Integer.parseInt(br.readLine());
nums[t]++;
}
Arrays.fill(h,-1);
for(int i=0;i<c;i++){
s=br.readLine().split(" ");
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
int c=Integer.parseInt(s[2]);
add(a,b,c);
add(b,a,c);
}
int res=INF;
for(int i=1;i<=p;i++){
dijskra(i);
int sum=0;
for(int j=1;j<=p;j++){
sum+=dist[j]*nums[j];
}
res=Math.min(res,sum);
}
System.out.println(res);
}
}
spfa:
import java.io.*;
import java.util.*;
class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static final int N=5000;
static final int INF=(int)1e8;
static int[]h=new int[N];
static int[]ne=new int[N];
static int[]e=new int[N];
static int[]w=new int[N];
static int[]dist=new int[N];
static boolean[]st=new boolean[N];
static int idx;
static int[]nums=new int[N];
static int n,p,c;
public static void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
public static void spfa(int start){
Arrays.fill(dist,INF);
dist[start]=0;
Queue<Integer>q=new LinkedList<>();
q.add(start);
st[start]=true;
while(!q.isEmpty()){
int cur=q.poll();
st[cur]=false;
for(int i=h[cur];i!=-1;i=ne[i]){
int j=e[i];
if(dist[j]>dist[cur]+w[i]){
dist[j]=dist[cur]+w[i];
if(!st[j]){
q.add(j);
st[j]=true;
}
}
}
}
}
public static void main(String[]args)throws IOException{
String[]s=br.readLine().split(" ");
n=Integer.parseInt(s[0]);
p=Integer.parseInt(s[1]);
c=Integer.parseInt(s[2]);
for(int i=1;i<=n;i++){
int t=Integer.parseInt(br.readLine());
nums[t]++;
}
Arrays.fill(h,-1);
for(int i=0;i<c;i++){
s=br.readLine().split(" ");
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
int w=Integer.parseInt(s[2]);
add(a,b,w);
add(b,a,w);
}
int res=INF;
for(int i=1;i<=p;i++){
spfa(i);
int sum=0;
for(int j=1;j<=p;j++){
sum+=dist[j]*nums[j];
}
res=Math.min(res,sum);
}
System.out.println(res);
}
}
边栏推荐
- What development models did you know during the interview? Just read this one
- In my limited software testing experience, a full-time summary of automation testing experience
- 聊聊SOC启动(十) 内核启动先导知识
- 聊聊SOC启动(九) 为uboot 添加新的board
- The post-90s resigned and started a business, saying they would kill cloud database
- Onedns helps college industry network security
- Audit migration
- Talk about SOC startup (11) kernel initialization
- Swiftui tutorial how to realize automatic scrolling function in 2 seconds
- STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
猜你喜欢
基于华为云IOT设计智能称重系统(STM32)
Distributed database master-slave configuration (MySQL)
Onedns helps college industry network security
Automated testing framework
MySQL安装常见报错处理大全
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
Flet教程之 14 ListTile 基础入门(教程含源码)
请查收.NET MAUI 的最新学习资源
一度辍学的数学差生,获得今年菲尔兹奖
對比學習之 Unsupervised Learning of Visual Features by Contrasting Cluster Assignments
随机推荐
相机标定(1): 单目相机标定及张正友标定基本原理
核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
SwiftUI 教程之如何在 2 秒内实现自动滚动功能
Tsinghua Yaoban programmers, online marriage was scolded?
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
Neural approvals to conversational AI (1)
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
Various uses of vim are very practical. I learned and summarized them in my work
‘module‘ object is not callable错误
R Language Using Image of magick package Mosaic Function and Image La fonction flatten empile plusieurs images ensemble pour former des couches empilées sur chaque autre
Apprentissage comparatif non supervisé des caractéristiques visuelles par les assignations de groupes de contrôle
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
Case study of Jinshan API translation function based on retrofit framework
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】
When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)
Qt 实现容器的DELETE的方式
MySQL安装常见报错处理大全