图论--单源最短路 spfa求最短路
spfa的限制:不存在负环,否则死循环(不需要太关心这个限制),或者限制了走的边数
一般的最短路问题里面都不存在负环(99.9%)。。。
很多的正权图都可以用spfa:(被卡了就换成堆优化版的dijkstra算法)
给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环, 边权可能为负数。
请你求出 1 号点到 n 号点的最短距离,如果无法从 1 号点走到 n 号点,则输出 impossible。
数据保证不存在负权回路。
输入格式
第一行包含整数 n 和 m。
接下来 m 行每行包含三个整数 x,y,z,表示存在一条从点 x 到点 y 的有向边,边长为 z。
输出格式
输出一个整数,表示 1 号点到 n 号点的最短距离。
如果路径不存在,则输出 impossible。
数据范围
1≤n,m≤105,
图中涉及边长绝对值均不超过 10000。
输入样例:
3 3
1 2 5
2 3 -3
1 3 4
输出样例:
2
https://www.acwing.com/problem/content/description/853/
import java.util.*;
public class Main
{
static int n,m,N=100005,M=N,idx,INF=0x3f3f3f3f;
static int h[]=new int [N],e[]=new int [M],ne[]=new int [M],w[]=new int[M];
static int dist[]=new int [N];
static boolean st[]=new boolean[N];
static void add(int a,int b,int c)
{
e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
static Queue q=new LinkedList();
static int spfa()
{
Arrays.setAll(dist, x->INF);
q.offer(1);
st[1]=true;
dist[1]=0;
while(!q.isEmpty())
{
int t=(int)q.poll();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];//注意这里即使st[j]=true,你也要更新,只是不加队列而已
if(st[j]==false)
{
q.offer(j);
st[j]=true;
}
}
}
}
return dist[n];
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
Arrays.setAll(h,x->-1);
for(int i=0;i<m;++i)
{
int a,b,w;
a=sc.nextInt();
b=sc.nextInt();
w=sc.nextInt();
add(a,b,w);
}
int ans=spfa();
if(ans==INF)System.out.println("impossible");
else System.out.println(ans);
}
}

浙公网安备 33010602011771号