Expensive dowry POJ-1062 Dijkstra + Enumeration

Source: Internet
Author: User
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 inputs The 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". The minimum number of coins required for the output outputs. Sample Input
1 4
10000 3 2 2 8000 3 2 1 4 2 1
4
50 2 0
Sample Output

5250


Test instructions: The question of an explorer marrying a wife is to ask the adventurer to take at least as much as the dowry money.


Exercises

This problem pits me for a long time, tie heart. Start with the Floyd,wrong, with Dijkstra less write the enumeration for loop, wrong an afternoon. Scorching The last reference is made by Daniel's code.

From each point must be to dig out the gold coins that the person needs, so you can view the starting point as 0,0 to each point is the point that the people need the gold. Consider the offer as a distance, which translates into the shortest path problem. It should be noted that only from low to high level, that is, one-way, and any two transactions of the level gap can not exceed m, apply enumeration +dijstra for each point to the chiefs of the required number of coins, the chief ordinal is 1.

The problem is the basic usage of Dijkstra, but the conversion of the time is more ingenious, there are level trading restrictions. Scorching

Below are a few sets of test data in the POJ discussion area.

1 4
10000 3 2 2 8000 3 2 1 4 2 1
4
50 2 0
Answer: 5250
1 5
10000 3 4 2 3 4 5
9000
8000 2 3 3
0
  2000 4 1
5 1900
1 0

Answer: 4000
3 8
10000 3 6 2 3 4 5 7
9000
8 +
2 5008
8000 3
3
4
5 7000
1 1 6
4 1 5 1900 1 0 1 1
7 4007
2000 4 1
5 1900
3 0

Answer: 2950
1
1324 0 0
1234 0 0
255 0 0
0 0 0
0
2134 0 0
456 0 0
2345 0 0
67 0 0
6436 0 0

Answer: 1324
1 4
10000 3 2
2 1
3 3 +
2 2
4 1
3 1 +
3 1 4 2 4 0

Answer: 105
3 5
10000 3 4 2 in 3, 4,
5 9000
8000 2 3
3, 4, 5 7000 1 0
4 1
5 1900
1 0

Answer: 3950
0 5
10000 3 4 2 in 3, 4,
5 9000
8000 2 3
3, 4, 5 7000 4 0
3 1
5 1900
50 2 0

Answer: 4000
2 5
2 2 2 1 3 2 2 1 1 3 2
1
1 4
2 0
2 0

Answer: 101

The code is as follows:

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std;
#define INF 0x3f3f3f3f int mp[1100][1100];
int gg[11000];
int vis[11000];
int dis[11000];

int m,n;
    int Dijkstra () {int i,j;
    memset (dis,0,sizeof (dis));
    for (I=1; i<=n; i++)//The required gold coins from each point will be considered as the number of coins required for 0 to that point Dis[i]=mp[0][i];
        for (I=1; i<n; i++) {int mina=inf;
        int u,k; for (j=1; j<=n; J + +) {if (!vis[j] && Dis[j]<mina) {mina=dis[
                J];
            U=j;
        }} vis[u]=1;
                for (k=1; k<=n; k++) {if (!vis[k] && mp[u][k]<inf && dis[k]>dis[u]+mp[u][k])
        DIS[K]=DIS[U]+MP[U][K]; }} return dis[1];//returns the amount of money spent on each point to the Emirates Advantage} int main () {while (~scanf ("%d%d", &m,&n)) {int i,j
        , A,b,c,e,r;
     for (i=0; i<=n; i++)//initial hu is not connected for (j=0; j<=n; j + +)           Mp[i][j]=inf;
            for (I=1; i<=n; i++) {scanf ("%d%d%d", &a,&b,&c);
            mp[0][i]=a;//will start from each point of the required gold coins as 0 to the point of the number of coins required gg[i]=b;
                for (j=1; j<=c; J + +) {scanf ("%d%d", &e,&r);
        mp[e][i]=r;//one-way path, and level E is lower than I, low grade to High class}} int minn=inf;
            for (I=1; i<=n; i++)//Enumerate each person as the lowest level {int mnn=gg[i]; memset (vis,0,sizeof (VIS));//note initialization for (j=1; j<=n; j + +)//Mark level gap is too large, and rank below their own person {if ( Gg[j]<mnn | |
                GG[J]-MNN&GT;M) Vis[j]=1;
            else vis[j]=0;
            } int Ss=dijkstra ();
        Minn=min (MINN,SS);
    } printf ("%d\n", Minn);
} return 0;
 }


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.