当前位置:网站首页>【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
2022-07-07 09:46:00 【暮色_年华】
抽象问题:
求所有奶牛到某个点的最短距离和的最小值。
由于是双向的,可以看成从某个点到所有其他点的最短距离的最小值。
所以,可以看成是单源最短路问题,由于是稀疏图且没有负边,所以可以使用堆优化的dij或者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);
}
}
边栏推荐
- 测试优惠券要怎么写测试用例?
- Common SQL statement collation: MySQL
- STM32入门开发 NEC红外线协议解码(超低成本无线传输方案)
- STM32 entry development uses IIC hardware timing to read and write AT24C08 (EEPROM)
- 科普达人丨一文弄懂什么是云计算?
- 面试被问到了解哪些开发模型?看这一篇就够了
- Android interview knowledge points
- Talk about SOC startup (x) kernel startup pilot knowledge
- 互联网协议
- R language uses image of magick package_ Mosaic functions and images_ The flatten function stacks multiple pictures together to form a stack layers on top of each other
猜你喜欢
测试开发基础,教你做一个完整功能的Web平台之环境准备
Learning notes | data Xiaobai uses dataease to make a large data screen
Electron adding SQLite database
. Net Maui performance improvement
聊聊SOC启动(九) 为uboot 添加新的board
About the application of writing shell script JSON in JMeter
一度辍学的数学差生,获得今年菲尔兹奖
Talk about SOC startup (VII) uboot startup process III
There are so many factors that imprison you
聊聊SOC启动(七) uboot启动流程三
随机推荐
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
Electron adding SQLite database
Android 面试知识点
如何在博客中添加Aplayer音乐播放器
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
sql里,我想设置外键,为什么出现这个问题
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
Eth trunk link switching delay is too high
vim 的各种用法,很实用哦,都是本人是在工作中学习和总结的
Antd select selector drop-down box follows the scroll bar to scroll through the solution
MIF file format record
Input type= "password" how to solve the problem of password automatically brought in
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
Activity生命周期
對比學習之 Unsupervised Learning of Visual Features by Contrasting Cluster Assignments
简单介绍一下闭包及它的一些应用场景
Table replication in PostgreSQL
About the application of writing shell script JSON in JMeter
相机标定(1): 单目相机标定及张正友标定基本原理
Vuthink正确安装过程