Expensive dowry POJ - 1062 (shortest)

topic:

Young explorers came to an Indian tribe. Where he and the chief's daughter fell in love, so go Qiuqin Bianxiang chiefs. Emirates to him as dowry was promised his daughter to him with 10,000 gold coins. Explorer not so much gold, it requested chiefs reduce the requirements. Emirates said:. "Ah, if you can get the coats of the high priest for me, I can as long as 8000 coins if you can got his crystal ball, so long as the 5000 gold on the line." Explorers went to the high priest there, she asked him for a fur coat or a crystal ball, the high priest asked him to use gold coins to change, or got other things for him, he can lower the price. Explorer so he went to other places, other people have made similar demands, or change in gold directly, or anything else you can find lower prices. But explorers did not need to replace it with something diverse things, because it would not get a lower price. Explorers now really need your help, let him marry his sweetheart with a minimum of gold. In addition, he will tell you that in this tribe, hierarchy is very strict. Will not carry out any form of direct contact between the position of the gap exceeds a certain limit of two people, including transaction. He is an outsider, it can be exempted from these restrictions. But if he had a deal and lower-status individuals, higher status people will not deal with him, they think it is tantamount to indirect contact, and vice versa. So you need to provide the best option for him after considering all the circumstances. 
For convenience, we put all the items are numbered starting from 1, the chief's promise also be seen as an object, and the number is always 1. Each item has a corresponding price P, the owner's position level L, and a range of alternatives and the alternatives Ti corresponding to the "preferential" Vi. If more than two people standing gap between the level of M, it can not be "indirect trade." You must be calculated based on these data explorer requires a minimum number of coins in order to marry the chief's daughter. 

Input

The first line input two integers M, N (1 <= N <= 100), successively position gap between the level represents the total number of items and limitations. Next, according to the numbers given in ascending order of the N described in the article. At the beginning of each item is described in three non-negative integer P, L, X (X <N), the price of the item represented in turn, level position and the total number of the owner of alternatives. The next X lines each include two integers T and V, respectively, and a number of alternatives "preferential price."

Output

The output of the minimum number of coins needed.

Sample Input

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

Sample Output

5250 

analysis:
Since the final article want to change to 1, the first reaction is a reverse request to change article 1 takes other items, converted to change to take the article 1, but since the limit level to exchange goods, this process will be very troublesome 
so think, assume that a number 0 items, seeking to change the number 0 items to take No. 1 item
on the level of restrictions, unable to determine whether the Emirates is the highest level, so you can enumerate the lowest level (each shortest must have a minimum grade) , n times the shortest operation, all exchange of information can be obtained. Note: every point of dis select the smallest, is determined to be level is not within range, while the relaxation time is also determined level range.
Code:
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 using namespace std;
 5 const int inf = 0x3f3f3f3f;
 6 struct obj
 7 {
 8     int v;
 9     int d;
10 }a[110];
11 int m, n;
12 int g[110][110];
13 int dis[110];
14 bool vis[110];
15 
16 void dj(int low)
17 {
18     for (int i = 1; i <= n; i++)
19         dis[i] = g[0][i];
20     memset(vis, 0, sizeof(vis));
21 
22     for (int i = 1; i <= n; i++)
23     {
24         int u = -1, minn = inf;
25         for (int j = 1; j <= n; j++)
26             if (!vis[j] && dis[j] < minn)
27                 minn = dis[j], u = j;
28         if (u == -1) continue;
29         vis[u] = 1;
30         if (a[u].d < low || a[u].d - low > m) continue;
31         for (int j = 1; j <= n; j++)
32             if (!vis[j] && dis[j] > dis[u] + g[u][j] && a[j].d - low >= 0 && a[j].d - low <= m)
33                 dis[j] = dis[u] + g[u][j];
34     }
35 }
36 
37 int main()
38 {
39     cin >> m >> n;
40     int num;
41 
42     memset(g, inf, sizeof(g));
43     for (int i = 1; i <= n; i++)
44     {
45         cin >> a[i].v >> a[i].d >> num;
46         g[0][i] = a[i].v;
47         int t, v;
48         for (int j = 1; j <= num; j++)
49         {
50             cin >> t >> v;
51             g[t][i] = v;
52         }
53     }
54     int ans = a[1].v;
55 
56     for (int i = 1; i <= n; i++)
57     {
58         dj(a[i].d);
59         ans = min(ans, dis[1]);
60     }
61     cout << ans << endl;
62 }

 



Guess you like

Origin www.cnblogs.com/liuwenhan/p/11421244.html