POJ - 1860: Currency Exchange & Shortest Path Deformation - Min Max

Title description:

N points, M edges, each edge has a value. To find a path from point 1 to point N, it is required to maximize the minimum edge weight in the path.                           

Input:

Multiple sets of input, the first line gives a T.
The first line of each group gives two numbers n and m. (1 <= n <= 1000)
Next m lines, each line has three numbers u, v, w representing the two endpoints and edge weights of the path 

(1 <= u, v <= n, 0< w <= 1e6)
There is only one edge between two points, and the graph is undirected. Output:

The i-th group of data first outputs "Scenario #i:"
and then outputs the minimum edge weight on the path, and outputs a blank line at the end.
Guaranteed solution

Example:

sample input: sample output
Scenario #1:
3  3 1
1 2 3
1 3 4
2 3 5

Ideas: 

At first glance, this problem is very simple. It is to find the shortest path and use dijkstra to solve it. But after a closer look, the "minimum value is the largest" confuses me. The first thing that pops up in my mind is "Mathematical value problem", but after the class, I feel that this is not the case. The minimum value and maximum value of this question are linked to dijkstra and can even be said to be very similar, but the principle is difficult to understand. Let us study it carefully.

Ordinary dijkstra & min max

Ordinary dijkstra, whether it is a code that does not optimize and only uses relaxation operations or a code that uses priority queues for optimization, its most important operation does not matter how the problem is solved, how it is nested with other algorithms, this "if(dis[v ]>dis[u]+w) dis[v]=dis[u]+w;” are always the same.

Regarding the minimum and maximum, the principle is difficult to understand, but the code only changes a small part of dijkstra's core code. Change the above code to "if(dis[v]>max(dis[u],w) dis[v]=dis[u]+w;" For those who have difficulty in understanding, just memorize it                                                                                                                                           

The principle of minimum value maximum:
"
When we are solving the single-source shortest path problem in a weighted directed graph, we need to find the shortest path from the starting point s to all other nodes. In this problem, the length of the path refers to the weight of all edges on the path and. And "the minimum value of the edge weight in the path is the largest" means that, for all paths from the starting point s to other nodes, select the path with the largest minimum value of the edge weight as the shortest path. That is to say, the length of the shortest path is The minimum edge weight of the path with the largest minimum edge weight among all paths. 
For example, suppose we have a weighted directed graph, where the starting point is s and the end point is t. There are three paths s->t in the graph. The edge weights are 2, 3, and 4 respectively. According to the definition of "the minimum value of the edge weight in the path is the largest", we need to select the path with the largest minimum value of the edge weight as the shortest path. In this example, the path with the largest minimum edge weight The path is the third path because it has a minimum edge weight of 4, while the other two paths have a minimum edge weight of 2 and 3. Therefore, the length of the shortest path is 4. It should be noted that if there are many If the minimum edge weights of two paths are the same, then we can choose one of them as the shortest path. At the same time, if there is no path from the starting point s to the ending point t, then the length of the shortest path is infinite.
"

This passage explains in detail the most fundamental difference between dijkstra and its deformation. "The minimum value is the largest" refers to the multiple paths I go out from one point. I want to ensure that the weight of each point I go is as high as possible. Small (at least the current path cannot find out that the sum of the weights of the second path is smaller than it), among the multiple paths that have been walked, I will select the path with the largest weight to output, and " if(dis[v]>max(dis[u],w) dis[v]=dis[u]+w;"This sentence can ensure that the weight of each point we pop out from the queue is in the current In the method, it is the smallest one, so that each path can reach the "minimum value" feature. After finishing the queue, we use min to screen each "minimum value" path in the main function. Filter out the only path that satisfies the condition.


Ideas for this question: 

Because the title says to ensure that the title has a unique solution, this question can simplify the difficulty of the special judgment, so we deformed dijkstra according to the "minimum value and maximum" feature we introduced above, paying attention to the details, "Careful and Cautious" Just finish the code.

Attach the code:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#include <vector>

const int N = 1005;

struct EDGE {
    int v, w, next;
} e[N * N * 2];

int head[N], tot = 1;
int father[N];

void add(int u, int v, int w) {//普通dijkstra建边无需多说
    e[tot].v = v;
    e[tot].w = w;
    e[tot].next = head[u];
    head[u] = tot ++;
}

void init(int n) {//对father数组进行初始化,一开始都没有父节点,将其值赋为-1
    tot = 1;
    for (int i = 0; i <= n; i++) {
        head[i] = 0;//初始化
        father[i] = -1;
    }
}

struct Node {
    int pos, dis;

    Node() {};

    Node(int pos, int dis): pos(pos), dis(dis) {};

    bool operator < (const Node &r) const {
        return dis > r.dis;
    }
};

int dis[N], vis[N];

void dijkstra(int s) {
    std::priority_queue<Node> q;
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    dis[s] = 0;
    q.push(Node(s, 0));
    while (!q.empty()) {
        Node f = q.top();
        q.pop();
        int u = f.pos;
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; i; i = e[i].next) {
            int v = e[i].v;
            int w = e[i].w;
            if (!vis[v] && dis[v] > std::max(dis[u], w)) {
                dis[v] = std::max(dis[u], w);
                q.push(Node(v, dis[v]));
                father[v] = u;
            }
        }
    }
}

void solve() {
    int n, m;
    std::cin >> n >> m;
    init(n);//进行初始化
    for (int i = 0; i < m; i++) {
        int u, v, w;
        std::cin >> u >> v >> w;
        add(u, v, w);
    }
    dijkstra(1);
    int p = n;
    int ans = 0x3f3f3f3f;
    while (p != 1) {//对应init函数father数组赋-1的行为,说明p=-1到了终点需退出
        ans = std::min(ans, dis[p]);
        p = father[p];
    }
    std::cout << ans << '\n';
}

int main() {
    // freopen("/Users/chant/in.txt", "r", stdin);
    int T;
    std::cin >> T;
    for (int i = 1; i <= T; i++) {
        printf("Scenario #%d:\n", i);
        solve();
    }
    return 0;
}

 Summary: This question is a template question for the minimum value maximum and dijkstra's deformation. The key point is to understand the meaning of the minimum value maximum, and the rest is very simple.

Creation is not easy, please do not prostitute! Please give a three-link + attention! Thanks! 

Guess you like

Origin blog.csdn.net/2301_76331300/article/details/131652062