Portal: Click to open the link
The main idea of the title: When buying things, there are substitutes for each thing. After having substitutes, you can get preferential prices. The owner of each item has its own level. Those whose level exceeds m cannot be directly or indirectly traded. Ask the minimum purchase price for item No. 1 what is the price.
Idea: I thought of dfs at first, but it is more troublesome to have a level of no more than m. After reading other people's practices, I found that it is too clever to convert this problem into the shortest path (I am too weak). The starting point is 0, which means There is nothing , the price of each item is the weight from 0 to i, and then the preferential price is the weight of u and i, which is converted into the shortest path, but the starting point is 0, and the end point is 1. For the level problem , enumerate the levels of each node in turn , assuming the lowest level, then traverse it, run Dij Tesla, and calculate the minimum value. (Enumerate with this method, dfs should also work).
See the code comments for details.
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<string> #include<math.h> #include<cmath> #include<time.h> #include<map> #include<set> #include<vector> #include<queue> #include<algorithm> #include<numeric> using namespace std; int m,n; const int maxn=110; int dis[maxn],v[maxn],g[maxn][maxn],vis[maxn],vv[maxn]; int djks(){ for(int i=1;i<=n;i++){ dis[i]=g[0][i];//Indicates starting from 0 (when there is nothing) } dis[0]=0; vis[0]=1; for(int i=1;i<=n;i++){ int p,minn=0x3f3f3f3f; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[j]<minn){ from = dis [j]; p=j; } } force[p]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[j]>dis[p]+g[p][j]){ dis[j]=dis[p]+g[p][j]; } } } return dis[1];//(return to 1 point) } int main(){ scanf("%d%d",&m,&n); memset(g,0x3f3f3f3f,sizeof(g));//图 for(int i=1;i<=n;i++){ int k; scanf("%d%d%d",&g[0][i],&v[i],&k);//The cost of u item to i item (the weight of reaching u node to i node) while(k--){ int u, w; scanf("%d%d",&u,&w); g[u][i]=w; } } int minn=0x3f3f3f3f;//The minimum answer for(int i=1;i<=n;i++){//Enumerate the level of each node and set the current node to the lowest level int va = v [i]; if(vv[va])continue; vv [va] = 1; memset(vis,0,sizeof(vis)); for(int j=1;j<=n;j++){ if(v[j]-va>m)vis[j]=1;//Exclude m above yourself if(v[j]<va)vis[j]=1;//Exclude those lower than yourself (because the enumeration is the lowest level) } minn = min (minn, djks ()); } printf("%d\n",minn); }