The expensive dowry
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 40715 |
|
Accepted: 11839 |
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
I think out of the idea and the problem realized when the special excitement.
Note this question two points:
1. Take the part of the offer as a path and then Dijkstra, and compare the value of each point + the shortest path length of that point.
2. Record the front node of each point when it enters set, in order to compare level, this node and the node's front node are compared to the level after the line.
Code:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace Std;int max;int edge[1005][1005];int vist[ 1005],level[1005],minidis[1005],value[1005],pre[1005];int level_diff,good_num;void init () {memset (edge,-1,sizeof ( Edge));} void Init2 () {int i;for (i=1;i<=good_num;i++) {minidis[i]=max;pre[i]=1;vist[i]=0;}} void Dijkstra (int i) {int j,k;int position=i;vist[position]=1;minidis[position]=0;for (j=1;j<=good_num-1;j++)// Altogether to be added into Num-1 points {for (k=1;k<=good_num;k++) {if (vist[k]==0 && abs (level[i]-level[k)) <=level_diff & & Edge[position][k]!=-1 && Minidis[position]+edge[position][k] < minidis[k])//New points updated Minidis{int flag =0,temp=position;while (temp!=1) {if (ABS (Level[temp]-level[k]) >level_diff) {flag=1;break;} Else{temp=pre[temp];}} if (flag==0) {minidis[k]=minidis[position]+edge[position][k];p re[k]=position;}}} int Min_value=max,min_pos=0;for (K=1;K≪=good_num;k++) {if (vist[k]==0 && minidis[k]<min_value)//Compare the smallest one as the newly added store {min_value = Minidis[k];min_ pos = k;}} Vist[min_pos]=1;position=min_pos;}} int main () {int i,j,replace;scanf ("%d%d", &level_diff,&good_num); init (); for (i=1;i<=good_num;i++) {scanf ( "%d%d%d", &value[i],&level[i],&replace), int temp;for (j=1;j<=replace;j++) {scanf ("%d", &temp); scanf ("%d", &edge[i][temp]);}} Max=value[1];init2 ();d Ijkstra (1), int ans=max;for (i=1;i<=good_num;i++) {ans=min (ans,minidis[i]+value[i]);} Cout<<ans<<endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1062: The expensive dowry