Title Link: Order me, please
Main idea:
nnn items, each item has its own price PPP, the level of the item is LLL, and the number of items that can be replaced is XXX. The X-line of each item gives the item number that can be replaced and the gold coins needed for replacement. At the same time, the grade difference of the items cannot exceed MMM when they are exchanged. Ask the minimum number of gold coins to be spent???
Solutions:
Obviously, how to build a map is the difficulty of this problem. Since each item has its own value, we can set another origin 000, and the edge weight from the origin to nnn items is the value of the item. The problem is that there is no hierarchy at the origin, so how to limit the grade difference required by the problem???
We can enumerate rank[0]rank[0]rank[0] rank[0] for nnn items, and assume that rank[0]rank[0]rank[0] is the lowest rank among all items. Then, according to the requirements of the topic, all exchanges should meet rank[k] - rank[0] ≥ Mrank[k]-rank[0] ≥ M
Code idea:
Run n times for dijastra, take the minimum value
Core: enumerating the minimum value to solve the hierarchy problem
#include<bits/stdc++.h> using namespace std; const int INF = 0x3F3F3F3F; const int N = 105; int m, n, mp[N][N]; int dis[N], vis[N], rank[N]; void init() { for (int i=0; i<=n; i++) { for (int j=0; j<=n; j++) { if (i == j) mp[i][j] = 0; else mp[i][j] = INF; } dis[i]=INF; } } bool rk(int i, int j){ return rank[i]<=rank[j] && (rank[j]-rank[i])<=m; } void creatgraph() { int t1, t2, t3; for (int i=1; i<=n; i++) { scanf("%d%d%d", &t1, &t2, &t3); mp[0][i] = t1; rank[i] = t2; for (int j=1; j<=t3; j++) { scanf("%d%d", &t1, &t2); mp[t1][i] = t2; } } } int dijkstra(int st) { memset(vis, 0, sizeof(vis)); for (int i=0; i<=n; i++) dis[i] = mp[st][i]; vis[st] = 1; for (int i=1; i<=n; i++) { /*Find the closest point to the starting point*/ int minn = INF, k = -1; for (int j=1; j<=n; j++) { if (!vis[j] && dis[j]<minn) { minn = dis[j]; k = j; } } if(k==-1) break; vis[k] = 1; if(!rk(0,k)) continue; for (int j=1; j<=n; j++) { //Relax the operation and find the medium to make the new shortest circuit appear if (!vis[j] && dis[k]+mp[k][j] < dis[j] && rk(0,j)) dis[j] = dis[k] + mp[k][j]; } } return dis[1]; } int main() { scanf("%d%d", &m, &n); init(); //Initialize map creatgraph(); //Map building int ans = INF; for(int i=1; i<=n; i++){ rank[0] = rank[i]; ans = min(ans, dijkstra(0)); } printf("%d\n", ans); }