# acwing 903. Expensive bride price is the shortest path with constraints

## Title:

The young Explorer came to an Indian tribe.

There he fell in love with the chief's daughter, so he asked the chief to marry him.

The chief asked him to use 10000 gold coins as a bride price before he agreed to marry his daughter to him.

The Explorer couldn't get so many gold coins, so he asked the chief to lower his requirements.

The chief said, "well, if you can get the high priest's fur coat for me, I can only buy 8000 gold coins. If you can get his crystal ball, you only need 5000 gold coins. "

The Explorer went to the high priest and asked him for a leather jacket or crystal ball. The high priest asked him to exchange gold coins or get other things for him. He could reduce the price.

The explorers then went to other places, and others made similar demands. They either exchanged gold coins directly, or found other things to reduce the price.

But explorers don't have to trade a variety of things for one thing, because they won't get a lower price.

The Explorer now needs your help to marry his sweetheart with the least gold coins.

In addition, what he wants to tell you is that in this tribe, the concept of hierarchy is very strict.

Two people whose status gap exceeds a certain limit will not have any form of direct contact, including transactions.

He is an outsider, so he can be free from these restrictions.

However, if he trades with a person of lower status, the person of higher status will not trade with him. They think that this is tantamount to indirect contact, and vice versa.

So you need to give him the best plan after considering all the situations.

For convenience, we number all items from 1. The promise of the chief is also regarded as an item, and the number is always 1.

Each item has a corresponding price P, the owner's status level L, a series of substitutes Ti and the corresponding "discount" Vi.

If the difference between two people's status levels exceeds M, they can't "trade indirectly".

Based on these data, you must calculate how many gold coins the explorer needs at least to marry the chief's daughter.

### Input format

The first line of input is two integers M and N, which represent the status level gap limit and the total number of items in turn.

Next, the descriptions of N items are given from small to large.

The description of each item begins with three non negative integers P, L and X, which in turn represent the price of the item, the status level of the owner and the total number of substitutes.

Next, line X includes two integers T and V, representing the number of the substitute and the "preferential price" respectively.

### Output format

Output the minimum number of gold coins required.

1≤N≤100,
1≤P≤10000,
1≤L,M≤N,
0≤X<N

1 4
10000 3 2
2 8000
3 5000
1000 2 1
4 200
3000 2 1
4 200
50 2 0

5250

## Solution:

I didn't understand the meaning of the question for the first time. I thought it was ok if the level of two adjacent people didn't exceed M. unexpectedly, it was not allowed to exceed m between the owners of the transaction
y the general practice is to enumerate the rank ranges of chiefs. Make an interval dijkstra.
The code is as follows:

```#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m,p[N],L[N],dist[N];
typedef pair<int,int>pii;
vector<pii>G[N];
int dijk(int l,int r)
{
memset(dist,0x3f,sizeof dist);
dist[0]=p[0];
priority_queue<pii,vector<pii>,less<pii> > q;
q.push({dist[0],0});
while(q.size())
{
int u=q.top().second;q.pop();
for(auto i:G[u])
{
int v=i.first,c=i.second;
if(L[v]<=r&&L[v]>=l&&dist[v]>dist[u]+c)
{
dist[v]=dist[u]+c;
//cout<<v<<"--"<<dist[v]<<endl;
q.push({dist[v],v});
}
}
}
return dist[1];
}
int main()
{
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++)
{
int num;
scanf("%d %d %d",&p[i],&L[i],&num);
G[0].push_back({i,p[i]});
while(num--)
{
int v,c;
scanf("%d %d",&v,&c);
G[v].push_back({i,c});
}
}
int res=p[1];
for (int i = L[1] - m; i <= L[1]; i ++ ) res = min(res, dijk(i, i + m));
cout<<res<<endl;
}
```

But in fact, we only need to maintain the maximum and minimum values of the interval and do the shortest circuit once.

```#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,m,p[N],L[N],dist[N];
typedef pair<int,int>pii;
struct node
{
int v,u,l,l1;
bool operator<(const node& t)const
{
return v<t.v;
}
};
vector<pii>G[N];
void dijk()
{
memset(dist,0x3f,sizeof dist);
dist[0]=p[0];
priority_queue<node> q;
q.push({dist[0],0,L[1],L[1]});
while(q.size())
{
int u=q.top().u,l=q.top().l,l1=q.top().l1;q.pop();
for(auto i:G[u])
{
int v=i.first,c=i.second;
if(abs(L[v]-l)<=m&&abs(L[v]-l1)<=m&&dist[v]>dist[u]+c)
{
dist[v]=dist[u]+c;
//cout<<v<<"--"<<dist[v]<<endl;
q.push({dist[v],v,min(L[v],l),max(L[v],l1)});
}
}
}
printf("%d",dist[1]);
}
int main()
{
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++)
{
int num;
scanf("%d %d %d",&p[i],&L[i],&num);
G[0].push_back({i,p[i]});
while(num--)
{
int v,c;
scanf("%d %d",&v,&c);
G[v].push_back({i,c});
}
}
dijk();
}
```

Obviously, the shortest circuit will come much faster.

Posted on Sat, 20 Nov 2021 22:15:54 -0500 by TomT