POJ 1062 the expensive dowry

Source: Internet
Author: User

C-Expensive dowryTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%i64d &%i6 4u Submit Status Practice POJ 1062

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
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x7fffffff;//infinitely LargeintM,n;//m is a grade difference and N is the number of itemsintprice[101][101];//item I in the case of substitute T number of the preferential price pricr[t][i], when t=0 I have no substitute, at this time is the original priceintlv[101];//Item NO. I owner's rank lv[i]intx[101];//number of substitutes for article I x[i]intdist[101];//Initial distance from source point 0 to any point I (weight), equivalent to the original price of each itemBOOLvist[101];//whether the record point I has been accessed/*Initial and Input*/voidData_init () {memset (Price,0,sizeof(price)); memset (LV,0,sizeof(LV)); memset (Dist,inf,sizeof(Dist)); memset (Vist,false,sizeof(vist)); CIN>>M>>N;  for(intI=1; i<=n;i++) {cin>>price[0][i]>>lv[i]>>x[i];//Price[0][i] Item I no substitute for the original price         for(intj=1; j<=x[i];j++)        {            intT,u;//T substitute number, U preferential price (temporary variable)Cin>>t>>u; Price[t][i]=u;//Item I preferential price in the case of substitute T, that is, the weight of point T to I        }    }}/*Dijkstra Algorithm*/intDijkstra () {intNode//record the point that is the shortest distance from the current source point    intSd//Shortest distance    inti,j;  for(i=1; i<=n;i++) Dist[i]=price[0][i];//assuming that the original source point is 0 points, initialize the original source point to the weight of each point dist[i]     for(i=1; i<=n;i++)//since the 1 point is the target point, the worst thing to do is to make N-times the shortest path to the other points and merge the two points (no longer accessing the equivalent of merging){node=0; SD=inf;  for(j=1; j<=n;j++)        {            if(!vist[j] && sd>dist[j])//in the inaccessible point, look for the shortest one{SD=Dist[j]; Node=j;//Record this point            }        }        if(node==0)//if node does not change, it means all points are accessed and the shortest path is searched.             Break; Vist[node]=true;//Record node point has been accessed         for(j=1; j<=n;j++)        {            if(!vist[j] && price[node][j] >0&& Dist[j] > Dist[node] + price[node][j])//relaxation of points that are not visited but are connected to node (Xinyuan point)dist[j]=dist[node]+Price[node][j]; }    }    returndist[1];//returns the shortest distance from the target point 1 under level lv[i] constraints after the current trade}intMain () {data_init (); //initializing and entering data    intTemp_price;//minimum price for target point 1 under level lv[i] constraint after current trade    intMAXLV;//The maximum level (unitary length is not necessarily the largest)    intMinprice=inf;//Lowest price (initialized to infinity)     for(intI=1; i<=n;i++)    {        /*under level limits, look for points that are allowed to be accessed at the current point*/MAXLV=lv[i];//temporarily treat the current item as the highest level         for(intj=1; j<=n;j++)//traverse the other points  {            if(LV[J]&GT;MAXLV | | maxlv-lv[j]>m)//when the other item J is higher than the current item (guaranteed unidirectional), or if the difference between the two levels exceeds the limit mvist[j]=true;//Item J is forced to be defined as "visited" and is not involved in subsequent operations            ElseVist[j]=false;//Otherwise, item J is defined as "not visited" and participates in subsequent operations} temp_price=dijkstra ();//Record the minimum distance (minimum price) of the target point 1 under level lv[i] constraints after the current trade        if(Minprice>temp_price)//find the minimum price after each transaction, and finally confirm the minimum priceMinprice=Temp_price; } cout<<minprice<<Endl; return 0;}

POJ 1062 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.