当前位置:网站首页>[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);
}
}
边栏推荐
- Flet教程之 14 ListTile 基础入门(教程含源码)
- Swiftui swift internal skill: five skills of using opaque type in swift
- There are ways to improve self-discipline and self-control
- audit 移植
- Blog moved to Zhihu
- Common SQL statement collation: MySQL
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- The database synchronization tool dbsync adds support for mongodb and es
- Table replication in PostgreSQL
- .NET MAUI 性能提升
猜你喜欢
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于SPI)
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
Some opinions and code implementation of Siou loss: more powerful learning for bounding box regression zhora gevorgyan
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
聊聊SOC启动(九) 为uboot 添加新的board
超标量处理器设计 姚永斌 第10章 指令提交 摘录
Design intelligent weighing system based on Huawei cloud IOT (STM32)
About the application of writing shell script JSON in JMeter
随机推荐
聊聊SOC启动(十) 内核启动先导知识
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
LeetCode - 面试题17.24 最大子矩阵
Talk about SOC startup (VII) uboot startup process III
Activity lifecycle
清华姚班程序员,网上征婚被骂?
【愚公系列】2022年7月 Go教学课程 005-变量
Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
Various uses of vim are very practical. I learned and summarized them in my work
Flet教程之 14 ListTile 基础入门(教程含源码)
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)
Use metersphere to keep your testing work efficient
The running kubernetes cluster wants to adjust the network segment address of pod
Vuthink proper installation process
超标量处理器设计 姚永斌 第8章 指令发射 摘录
electron 添加 SQLite 数据库
Creative information was surveyed by 2 institutions: greatdb database has been deployed in 9 places
分布式数据库主从配置(MySQL)