Expensive dowry (shortest path, drawing)

Expensive dowry

Title

There are several items, these items have a price, and the level of the owner (the highest level of the target point), and then there will be several alternatives and discounted prices. Each item can only be purchased with at most one of its substitutes. When exchanging, there is an upper limit on the level difference of all owners participating in the exchange. Ask, how much money is required to exchange to the target item?

practice

Consider constructing a map like this. The substitute for each item can be connected to a directed edge leading to the item, and the edge right is the discounted price. And set up a virtual source point, connected to all items, the border rights are the prices of these items. The problem is transformed into the shortest path from the virtual source point to the target point. Next, consider the limitation of the level difference. Because the level of the target point has been determined, you can traverse the lower limit that meets the requirements, find the shortest path for each level interval, and then find the minimum. In the process of finding the shortest path, only points within the range of the level can be updated, and points not within the range cannot be updated.

Code

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 110, inf = 0x3f3f3f3f;

int n,m,k;
int g[N][N];
int rk[N], dist[N];
bool st[N];

int dijkstra(int down, int up)
{
    
    
    memset(dist,0x3f,sizeof(dist));
    memset(st,false,sizeof(st));
    dist[0] = 0;
    for(int i=0;i<=n;i++){
    
    
        int t = -1;
        for(int j=0;j<=n;j++){
    
    
            if(!st[j]&&(t==-1||dist[t]>dist[j])){
    
    
                t = j;
            }
        }
        st[t] = true;
        for(int j=1;j<=n;j++){
    
    
            if(rk[j]>=down&&rk[j]<=up){
    
    
                dist[j] = min(dist[j], dist[t]+g[t][j]);
            }
        }
    }
    return dist[1];
}

int main()
{
    
    
    cin >> k >> n;
    memset(g,0x3f,sizeof(g));
    for(int i=1;i<=n;i++) g[i][i] = 0;
    for(int i=1;i<=n;i++){
    
    
        int price, level, cnt;
        cin >> price >> level >> cnt;
        rk[i] = level;
        g[0][i] = min(g[0][i], price);
        for(int j=0;j<cnt;j++){
    
    
            int id, c;
            cin >> id >> c;
            g[id][i] = min(g[id][i], c);
        }
    }
    int res = inf;
    for(int i=rk[1]-k;i<=rk[1];i++) res = min(res, dijkstra(i, i+k));
    cout << res << endl;
    return 0;
}

reward

This question introduces a restriction. This restriction has an attribute, each point has a value, and there is an upper limit for the attribute difference between all points on the path. This kind of problem is to enumerate all the intervals that meet this limit, and find the shortest path in each interval. This is different from the two-dimensional shortest path problem. The two-dimensional shortest path problem has two side weights, one side weight seeks the shortest path, and the other side weight has a path length that satisfies a restriction. The two-dimensional shortest path problem will be described in detail later.

Guess you like

Origin blog.csdn.net/weixin_43634220/article/details/108604225