Time: 0.25 s
Space: 4 m
Question:
In fact, it is to find the K short circuit without a ring.
Input:
N, m, and K are given, which represent N points, m edges, and K long paths respectively. In the next m row, the three integers x, y, and z represent two-way routes with charges of X and Y respectively. Ensure that there is no duplicate edge.
Output:
The first line has two numbers a, B, the K small cost a, and the number B of the passed points. Next, the number B indicates the K-short path.
Sample Input
5 10 3 1 2 6 1 3 13 1 4 18 1 5 35 2 3 14 2 4 34 2 5 17 3 4 22 3 5 15 4 5 34 1 5
Sample output
35 2 1 5
Solution:
The algorithm used to calculate the K short circuit is basically a *. The data volume here is relatively small. You can use a binary answer.
The length of the binary path, DFS to find how many paths length less than it, if it is (k-1) then directly output.
PS: sgu seems to have a problem with the data on this issue. Many people cannot use the AC. I tried to submit the code that someone else has AC or PE.
Therefore, the Code is not AC, but it is sure of the correctness of the program.
Reference code:
#include <cstdio>const int INF = 111;int g[INF][INF], vis[INF], path[INF];int n, m, k, x, y, z, l, r, mid, leSum;int S, T, pd, len, tol;void dfs (int x, int dis) {vis[x] = 1;if (x == T) {if (dis < mid) leSum++;if (!pd && dis == mid) leSum++, pd = 1;}elsefor (int i = 1; i <= n; i++)if (!vis[i] && g[x][i] && dis + g[x][i] <= mid)dfs (i, dis + g[x][i]);vis[x] = 0;}int check (int x) {pd = leSum = 0;dfs (S, 0);return leSum;}int getPath (int x, int dis) {vis[x] = 1;if (x == T && dis == len) {path[++tol] = x;return pd = 1;}elsefor (int i = 1; i <= n; i++) {if (!vis[i] && g[x][i] && g[x][i] + dis <= len) {if (getPath (i, dis + g[x][i]) ) path[++tol] = x;if (pd) return 1;}}vis[x] = 0;}int Search () {while (l <= r) {mid = l + (r - l >> 1);int tem = check (mid);if (tem == k)return mid;else if (tem > k)r = mid - 1;else l = mid + 1;}return -1;}int main() {scanf ("%d %d %d", &n, &m, &k);for (int i = 1; i <= m; i++) {scanf ("%d %d %d", &x, &y, &z);g[x][y] = g[y][x] = z, r += z;}scanf ("%d %d", &S, &T);len = Search();pd = tol = 0;getPath (S, 0);printf ("%d %d\n", len, tol);for (int i = tol; i > 1; i--)printf ("%d ", path[i]);printf("%d\n",path[1]);return 0;}