The expensive dowry

Source: Internet
Author: User

Description

The young explorers came to an Indian tribe. There he fell in love with the chief's daughter, so he went to Qiuqin to the chief. The chief asked him to use 10,000 gold coins as a dowry to promise to marry his daughter. The explorers could not get so many gold coins that they asked the chief to lower their demands. The sheikh said, "Well, if you can get me the piao of the high priest, I can just have 8000 gold." If you can get his crystal ball, then just 5000 gold. The explorer ran to the high priest and demanded Piao or a crystal ball, and the high priest asked him to change it with gold coins, or to get other things for him, he could lower the price. The explorers then ran to other places, and others made similar demands, either by exchanging coins directly or by finding something else to lower the price. But there's no need for explorers to change things with a variety of things, because they won't get a lower price. Explorers now need your help so that he can marry his sweetheart with the least amount of gold. And what he's going to tell you is that in this tribe, hierarchy is very strong. No direct contact will be made between two persons with a status gap exceeding a certain limit, including transactions. He is a foreigner, so he can not be subject to these restrictions. But if he trades with a lower-level person, the higher-status person will not trade with him, they think it is indirect contact and vice versa. So you need to give him the best plan after considering all the circumstances.
For the sake of convenience, we numbered all items starting from 1, and the chief's promise was also regarded as an item, and the number was always 1. Each item has a corresponding price p, the host's status level L, as well as a series of alternatives ti and the replacement of the corresponding "preferential" Vi. If the difference in status between the two people exceeds m, it cannot be "indirectly traded". You have to figure out how much gold the adventurer needs at least to marry the chief's daughter.

Input

The input first line is two integers m,n (1 <= N <= 100), which in turn represents the status level gap limit and the total number of items. Next, according to the number from small to large in order to give a description of n items. The description of each item begins with three nonnegative integers p, L, X (x < N), which in turn indicate the price of the item, the rank of the host, and the total number of substitutes. The next x row contains two integers t and V, each representing the replacement number and the "preferential price".

Output

Output the minimum number of coins required.

Sample Input

1 410000 3 22 80003 50001000 2 14 2003000 2 14 20050 2 0

Sample Output

5250

Source

Zhejiang main idea: With the least money to buy a daughter-0-... With a d[i] array to save the minimum intermediary fees, with I buy the minimum intermediary fees, p[i] for the value, because the title rank of the requirements first to be filtered so to establish a vis array, so d[1]=0, that is, with 1 for 1 words without intermediary fees, Dijkstra algorithm: Open a large for loop, Open two smaller, the first to filter out the least known intermediary fees, the second to update the minimum cost of the intermediary to continue to exchange the least path, with an n cycle to ensure that each vertex is transferred to.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f, MAX = $;intMap[max][max],p[max],d[max],rank[max];//map is the cost of the exchange, p is the value of the item, and d is the minimum cost to get the itemBOOLVis[max];//the VIS is used to mark whether it is feasible and D cannot be executed after it has been executedintm, n, X;intT, v;voidDijkstraintLintR) {memset (Vis,0,sizeof(VIS));  for(inti =1; I <= N; i++){            if(Rank[i] < l| | Rank[i] >r) vis[i]=1; D[i]=inf; } d[1] =0;  for(inti =1; I <= N; i++){            intMinc =inf,k;  for(intj =1; J <= N; J + +){                if(!vis[j]&& D[j] <minc) {Minc=D[j]; K=J; }           }//if D[J] can be exchanged, go to the smallestVIS[K] =1;  for(intj =1; J <= N; J + +){                if(!vis[j] && d[j] > D[k] +Map[k][j]) d[j]= D[k] +Map[k][j]; }    }}intMain () {intans;  while(~SCANF ("%d%d",&m,&N)) {             for(inti =1; I <= N; i++)                for(intj =1; J <= N; j + +) Map[i][j]=inf;  for(inti =1; I <= N; i++) {scanf ("%d%d%d",&p[i],&rank[i],&x);  while(x--) {scanf ("%d%d",&t,&v); Map[i][t]=v; }} ans=inf;  for(inti = rank[1]-M; i<= rank[1];i++) {Dijkstra (i,i+m);  for(intj =1; J <= N; j + +) ans= Min (ans,p[j]+D[j]); } printf ("%d\n", ans); }    return 0;}
View Code

The expensive dowry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.