Poj1062 expensive dowry [DFS]

Source: Internet
Author: User

Expensive dowry
Time limit:1000 ms   Memory limit:10000 K
Total submissions:37475   Accepted:10816

Description

The young explorer came to an Indian tribe. There he fell in love with the daughter of the chief, so he went to ask for advice from the Chief. The chief promised to marry his daughter only when he used 10000 gold coins as a dowry. The explorer asked the chief to lower his requirements if he could not get so many gold coins. The chief said, "Well, if you can get me the high priest's leather jacket, I only need 8000 gold coins. If you can get his crystal ball, you only need 5000 gold coins. "The explorer ran to the high priest and asked him for a leather jacket or a crystal ball. The high priest asked him to exchange his gold coins or get something else for him. He could lower the price. As a result, the explorer ran to other places, and other people made similar requests, or directly exchanged gold coins, or found other things to lower the price. However, it is not necessary for the explorer to change things with a variety of things, because they will not get a lower price. The explorer now needs your help so that he can marry his sweetheart with the least gold coins. What he wants to tell you is that in this tribe, the hierarchy concept is very strict. Two people whose status gap exceeds a certain limit will not conduct any form of direct contact, including transactions. He is an outsider, so he is not subject to these restrictions. However, if he makes a transaction with a person with a lower position, the person with a higher position will not trade with him. They think that this is like indirect contact, and the opposite is true. Therefore, you need to provide him with the best solution after considering all the situations.
For convenience, we numbered all items from 1, and the promise of the chief is also considered as an item, and the number is always 1. Each item has a corresponding price P, owner's Position Level L, and a series of alternatives Ti and the corresponding "discount" vi. If the gap between the two positions exceeds m, it cannot be "indirect transaction ". Based on the data, you must calculate the minimum amount of gold coins required by the explorer to marry the daughter of the chief.

Input

The first line of the input is two integers, m, n (1 <= n <= 100), indicating the limit of the status level gap and the total number of items in turn. Next, the descriptions of N items are given in ascending order of numbers. The description of each item starts with three non-negative integers p, L, and x (x <n), indicating the price of the item, the owner's Position level, and the total number of substitutes. The next line in Line X contains two integers T and V, indicating the replacement number and "preferential price" respectively ".

Output

Outputs the minimum required gold coins.

Sample Input

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

Sample output

5250

Source

Zhejiang classic question.

#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 110#define inf 0x3f3f3f3fstruct Node {    int price, level, down;} G[maxn];int head[maxn], m, n, id;struct Node2 {    int v, val, next;} E[maxn * maxn];bool vis[maxn];int sta[maxn], id2; // sta防止间接等级越界void addEdge(int u, int v, int val) {    E[id].v = v; E[id].next = head[u];    E[id].val = val; head[u] = id++;}int min(int a, int b) {    return a < b ? a : b;}int DFS(int u) {    for(int i = 0; i < id2; ++i)        if(abs(sta[i] - G[u].level) > m)             return inf;    int v, ans = G[u].price;    for(int i = head[u]; i != -1; i = E[i].next) {        if(vis[v = E[i].v]) return inf;        vis[v] = 1; sta[id2++] = G[u].level;        ans = min(ans, E[i].val + DFS(v));        vis[v] = 0; --id2;    }    return ans;}int solve() {    memset(vis, 0, sizeof(bool) * (n + 1));    int i, j, v, ans = G[1].price; vis[1] = 1;    id2 = 0; sta[id2++] = G[1].level;    for(i = head[1]; i != -1; i = E[i].next) {        v = E[i].v;         ans = min(ans, E[i].val + DFS(v));    }    return ans;}int main() {    int i, j, v, val;    while(scanf("%d%d", &m, &n) == 2) {        memset(head, -1, (n + 1) * sizeof(int));        for(i = 1, id = 0; i <= n; ++i) {            scanf("%d%d%d", &G[i].price, &G[i].level, &G[i].down);            for(j = 0; j < G[i].down; ++j) {                scanf("%d%d", &v, &val);                addEdge(i, v, val);            }        }        printf("%d\n", solve());    }    return 0;}


Poj1062 expensive dowry [DFS]

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.