题目描述
给定一个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
注意
在最短路径下,如果没有特别要求,可以采用spfa来求解最短路径问题,如果被卡,可以采用堆优化的dijkstra
思路
bellman_ford 详细请看 https://www.acwing.com/file_system/file/content/whole/index/content/1761584/
初始化 dist[1] = 0;
for i in 0, … , n - 1 //n个顶点,如果题目限制了寻找最后节点的次数k,则遍历次数改为k
for 所有边 记作a, b, w
backup(dist) //备份上一次的结果
//松弛操作,找最短路径
dist[b] = min(dist[b], dist[a] + w)
优化思想
在bellman_ford,有大量无用的计算,有价值的节点是与1节点相连接的边。因此可以采用队列或者其他数据结构来记录与1相邻的节点。
queue <- 1;
while(queue不为空){
1.a <- q.front(); 取出队头节点
q.pop();
2.更新a的所有出边b a->b
queue <- b;
}
注意,在遍历的过程中,在对于将b添加到队列中,b可能已经存在队列,所以需要标记位st[i], 来表示节点i是否存在队列中
java
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N = 100010, V = 0x3f3f3f3f;
static int n, m;
static int[] h = new int[N], w = new int[N], e = new int[N], ne = new int[N];
static int idx;
static int[] dist = new int[N];
static boolean[] st = new boolean[N];
private static void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
private static int spfa() {
Arrays.fill(dist, V);
dist[1] = 0;
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
st[1] = true;
while (!queue.isEmpty()) {
int t = queue.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];
if (!st[j]) {
queue.offer(j);
st[j] = true;
}
}
}
}
if (dist[n] == V) return V;
return dist[n];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
Arrays.fill(h, -1);
while (m -- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
add(a, b, c);
}
int t = spfa();
if (t == V) System.out.println("impossible");
else System.out.println(t);
}
}
c++
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int spfa(){
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
st[1] = true; //st标记当前点是否在队列中,同时队列中保存了上一次计算保留下来的最短路径
//st[1] = true; 从1的点计算
while(q.size()){
int t = q.front();
q.pop();
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]; //更新距离
if(!st[j]){ //如果不在队列中
q.push(j);
st[j] = true;
}
}
}
}
if(dist[n] == 0x3f3f3f3f) return 0x3f3f3f3f;
return dist[n];
}
int main(){
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while(m --){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int t = spfa();
if(t == 0x3f3f3f3f) puts("impossible");
else printf("%d\n", t);
}