M-Expensive dowry-poj1062

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. Finally met a Chinese topic ah ... decided to use a dfs+ memory search to try, I hope not tle cannot do memory search, because the permission system is not the same, only violent search, complexity is very high, but time unexpectedly is 0, also can enumerate permission system to calculate (premise right limit is not high)

#include <stdio.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <string.h>
#include <math.h>
usingnamespaceStd

ConstintMAXN =10005;
ConstintMAXM = the;
ConstintOO =0xFFFFFFF;

structNode
{
intU, V, price, next;
}E[MAXN];
intHEAD[MAXM], LV[MAXM], P[MAXM];
BOOLUSE[MAXM];

voidADD (intUintVintCintK
{
e[k].u = u;
E[K].V = v;
E[k].price = C;
E[k].next = Head[u];
Head[u] = k;
}
intDfs (intMAXLV,intMINLV,intKintM
{
intMINP = oo;

for(intI=HEAD[K]; i!=0; I=e[i].next)
{
intv = e[i].v;
intc = E[i].price;

if(Lv[v] >= minlv && lv[v] <= maxlv && use[v] = =false)
{
USE[V] =true;

intL = min (maxlv, lv[v]+m);
intR = Max (MINLV, lv[v]-m);

MINP = min (Minp, Dfs (L, R, V, M) +c);

USE[V] =false;
}
}

returnMin (P[k], MINP);
}

intMain ()
{
intN, M;

while(SCANF ("%d%d", &m, &n)! = EOF)
{
intI, k=1, V, Price, T;

Memset (Head,0,sizeof(head));

for(i=1; i<=n; i++)
{
scanf"%d%d%d", &p[i], &lv[i], &t);

while(t--)
{
scanf"%d%d", &v, &price);
ADD (i, V, Price, k++);
}
}

use[1] =true;
intAns = Dfs (lv[1]+m, lv[1]-m,1, M);

printf"%d\n", ans);
}

return0;

}

M-Expensive dowry-poj1062

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.