1, Y total video progress
2, 854 Floyd finds the shortest path
(1) Problem description
Given a directed graph with {nn} points and {mm} edges, there may be multiple edges and self rings, and the edge weight may be negative.
Give another {kk} queries. Each query contains two integers {xx} and} yy, indicating the shortest distance from point {xx} to point {yy}. If the path does not exist, output} impossible.
There is no negative weight loop in the data assurance diagram.
Input format
The first line contains three integers n,m,kn,m,k.
Next, there is a line of ^ mm ^ each line contains three integers ^ x,y,zx,y,z, indicating that there is a directed edge from point ^ xx ^ to point ^ yy ^ with an edge length of ^ zz.
Next kk lines, each line contains two integers x,yx,y, representing the shortest distance from query point xx to point yy.
Output format
There are {kk} lines in total, and an integer is output for each line to represent the query result. If there is no path between the two query points, then {impossible is output.
Data range
1≤n≤2001≤n≤200,
1≤k≤n21≤k≤n2
1≤m≤200001≤m≤20000,
The absolute value of the side length involved in the figure shall not exceed 10000.
Input example:
3 3 2 1 2 1 2 3 2 1 3 1 2 1 1 3
Output example:
impossible 1
Difficulty: simple |
Time / space limit: 1s / 64MB |
Total number of passes: 16849 |
Total attempts: 33722 |
Source: template question |
Algorithm label |
(2) Code implementation
import java.io.*; import java.util.*; class Main{ static int N = 250; static int[][] g = new int[N][N]; static int n,m; static int max = 0x3f3f3f3f; static void floyd(){ for(int k=1; k<=n; k++){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ g[i][j] = Math.min(g[i][k]+g[k][j], g[i][j]); } } } } public static void main(String[]args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] arr = in.readLine().split(" "); n = Integer.parseInt(arr[0]); m = Integer.parseInt(arr[1]); int k = Integer.parseInt(arr[2]); for(int i=1; i<=n ;i++){ for(int j=1; j<=n; j++){ if(i==j) g[i][j]=0; else g[i][j] = max; } } for(int i=0; i<m; i++){ String[] cur = in.readLine().split(" "); int a = Integer.parseInt(cur[0]); int b = Integer.parseInt(cur[1]); int c = Integer.parseInt(cur[2]); g[a][b] = Math.min(c, g[a][b]); } floyd(); for(int i=0; i<k; i++){ String[] cur = in.readLine().split(" "); int a = Integer.parseInt(cur[0]); int b = Integer.parseInt(cur[1]); if(g[a][b] >= max/2) System.out.println("impossible"); else System.out.println(g[a][b]); } } }
III. 858 Prim algorithm for minimum spanning tree
(1) Title Description
Given an undirected graph with {nn} points and {mm} edges, there may be multiple edges and self rings, and the edge weight may be negative.
Find the sum of the tree edge weights of the minimum spanning tree. If the minimum spanning tree does not exist, output {impossible.
A weighted undirected graph G=(V,E)G=(V,E), where | VV | represents the set of points in the graph, EE | represents the set of edges in the graph, n=|V|n=|V |, m=|E|m=|E |.
An undirected connected subgraph composed of all − nn vertices in − VV − and − 1n − 1 edges in − EE is called a spanning tree of − GG, in which the spanning tree with the smallest sum of edge weights is called the smallest spanning tree of undirected graph − GG.
Input format
The first line contains two integers , nn , and , mm.
Next, there is a line of {mm}, each line contains three integers} u,v,wu,v,w, indicating that there is an edge with a weight of {ww} between the point {uu} and the point {vv}.
Output format
There is one line in total. If there is a minimum spanning tree, an integer is output to represent the sum of tree edge weights of the minimum spanning tree. If there is no minimum spanning tree, it is output as {impossible.
Data range
1≤n≤5001≤n≤500,
1≤m≤1051≤m≤105,
The absolute value of the edge weight of the edges involved in the figure shall not exceed 10000.
Input example:
4 5 1 2 1 1 3 2 1 4 3 2 3 2 3 4 4
Output example:
6
Difficulty: simple |
Time / space limit: 1s / 64MB |
Total number of passes: 22592 |
Total attempts: 38456 |
Source: template question |
Algorithm label |
(2) Code implementation
import java.util.*; import java.io.*; class Main{ static int N=510; static int[][] g=new int[N][N]; static int[] dist=new int[N]; static boolean[] st=new boolean[N]; static int max = (int)1e9; static int n; static int prime(int n){ Arrays.fill(dist, max); //Initializes that the distance from all points to the set is positive infinity int res = 0; //Used to record the weight of the minimum spanning tree //Cycle n times for(int i=0; i<n; i++){ //Find the point closest to the collection int t = -1; for(int j=1; j<=n; j++){ if((t==-1||dist[j]<dist[t])&&!st[j]){ t = j; } } //If this is not the first point to find, but the minimum distance from all points to the set is positive infinity, it indicates that there is no minimum spanning tree if(i>0 && dist[t]==max) return -1; if(i>0) res+=dist[t]; st[t] = true; //Update the distance from other points to the set with the newly added points for(int j=1; j<=n; j++){ if(dist[j]>g[t][j]) dist[j] = g[t][j]; } } return res; } public static void main(String[]args) throws IOException{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String[]arr=in.readLine().split(" "); n=Integer.parseInt(arr[0]); int m=Integer.parseInt(arr[1]); for(int i=1;i<=n;i++) Arrays.fill(g[i],0x3f3f3f3f); while(m-->0){ String[] cur=in.readLine().split(" "); int u=Integer.parseInt(cur[0]); int v=Integer.parseInt(cur[1]); int w=Integer.parseInt(cur[2]); g[u][v]=g[v][u]=Math.min(g[u][v],w); } int t=prime(n); if(t==-1) System.out.println("impossible"); else System.out.println(t); } }