一、Y总视频进度
二、 AcWing 853. 有边数限制的最短路
(1)题目描述
给定一个 nn 个点 mm 条边的有向图,图中可能存在重边和自环, 边权可能为负数。
请你求出从 11 号点到 nn 号点的最多经过 kk 条边的最短距离,如果无法从 11 号点走到 nn 号点,输出 impossible
。
注意:图中可能 存在负权回路 。
输入格式
第一行包含三个整数 n,m,kn,m,k。
接下来 mm 行,每行包含三个整数 x,y,zx,y,z,表示存在一条从点 xx 到点 yy 的有向边,边长为 zz。
输出格式
输出一个整数,表示从 11 号点到 nn 号点的最多经过 kk 条边的最短距离。
如果不存在满足条件的路径,则输出 impossible
。
数据范围
1≤n,k≤5001≤n,k≤500,
1≤m≤100001≤m≤10000,
任意边长的绝对值不超过 1000010000。
输入样例:
3 3 1
1 2 1
2 3 1
1 3 3
输出样例:
3
难度:简单 |
时/空限制:1s / 64MB |
总通过数:22888 |
总尝试数:44041 |
来源:模板题,Airbnb面试题 |
算法标签 |
(2)代码实现
import java.io.*;
import java.util.*;
class Node{
int a,b,c;
public Node(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
}
class Main{
static int N = 10010;
static Node[] edge = new Node[N];
static int[] dist = new int[N];
static int n,m,k;
static int max = (int)1e8;
static int bellman_ford(){
//初始化
Arrays.fill(dist, max);
dist[1] = 0;
//循环k次
for(int i=0; i<k; i++){
//对dist数组做一次备份
int[] copy = Arrays.copyOf(dist, N);
for(int j=0; j<m; j++){
Node cur = edge[j];
int a = cur.a;
int b = cur.b;
int c = cur.c;
if(dist[b] > copy[a]+c){
dist[b] = copy[a]+c;
}
}
}
if(dist[n] >= max/2) return -1;
else return dist[n];
}
public static void main(String[]args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] cur = in.readLine().split(" ");
n = Integer.parseInt(cur[0]);
m = Integer.parseInt(cur[1]);
k = Integer.parseInt(cur[2]);
for(int i=0; i<m; i++){
String[] arr = in.readLine().split(" ");
int a = Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);
int c = Integer.parseInt(arr[2]);
edge[i] = new Node(a,b,c);
}
int t = bellman_ford();
if(t==-1) System.out.println("impossible");
else System.out.println(t);
}
}
三、AcWing 850. Dijkstra求最短路 II
(1)题目描述
给定一个 nn 个点 mm 条边的有向图,图中可能存在重边和自环,所有边权均为非负值。
请你求出 11 号点到 nn 号点的最短距离,如果无法从 11 号点走到 nn 号点,则输出 −1−1。
输入格式
第一行包含整数 nn 和 mm。
接下来 mm 行每行包含三个整数 x,y,zx,y,z,表示存在一条从点 xx 到点 yy 的有向边,边长为 zz。
输出格式
输出一个整数,表示 11 号点到 nn 号点的最短距离。
如果路径不存在,则输出 −1−1。
数据范围
1≤n,m≤1.5×1051≤n,m≤1.5×105,
图中涉及边长均不小于 00,且不超过 1000010000。
输入样例:
3 3
1 2 2
2 3 1
1 3 4
输出样例:
3
(2)代码实现
import java.util.*;
import java.io.*;
public class Main {
static int N = 150010;
static int[] h = new int[N], e = new int[N], ne = new int[N], w = new int[N];
static int idx;
static int[] dist = new int[N];
static boolean[] st = new boolean[N];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt(), m = in.nextInt();
Arrays.fill(h, -1);
while (m -- > 0) {
int x = in.nextInt(), y = in.nextInt(), z = in.nextInt();
add(x, y, z);
}
//Dijkstra
Arrays.fill(dist, 0x3f3f3f3f);
dist[1] = 0;
Queue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
pq.offer(new int[]{1, 0});
while (!pq.isEmpty()) {
int[] t = pq.poll();
int ver = t[0], distance = t[1];
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
pq.offer(new int[]{j, dist[j]});
}
}
}
if (dist[n] == 0x3f3f3f3f) out.print(-1);
else out.print(dist[n]);
out.flush();
}
private static void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx;
idx++;
}
}
四、I. 双向排序(25分)
(2)代码实现
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
int n = in.nextInt(), m = in.nextInt();
Integer[] arr = new Integer[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = i;
}
for (int i = 0; i < m; i++) {
int p = in.nextInt();
int split = in.nextInt();
if (p == 0) {
Arrays.sort(arr, 1, split + 1, (a, b) -> Integer.compare(b, a));
} else {
Arrays.sort(arr, split, n + 1);
}
}
for (int i = 1; i <= n; i++) {
if (i > 1) {
System.out.print(" ");
}
System.out.print(arr[i]);
}
in.close();
}
}