青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

QuXiao

每天進步一點點!

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  50 隨筆 :: 0 文章 :: 27 評論 :: 0 Trackbacks

解題報告

 

題目來源:

PKU 3513 Let's Go to the Movies

 

分類:

樹形DP

 

原文:

Let's Go to the Movies

 

Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 228


Accepted: 56

Description

http://acm.pku.edu.cn/JudgeOnline/images/3513_1.pngA favorite pastime for big families in Acmestan is going to the movies. It is quite common to see a number of these multi-generation families going together to watch a movie. Movie theaters in Acmestan have two types of tickets: A single ticket is for exactly one person while a family ticket allows a parent and their children to enter the theater. Needless to say, a family ticket is always priced higher than a single ticket, sometimes as high as five times the price of a single ticket.

It is quite challenging for families to decide which ticket arrangement is most economical to buy. For example, the family depicted in the figure on the right has four ticket arrangements to choose from: Seven single tickets; Two family tickets; One family ticket (for adam, bob, cindy) plus four single tickets for the rest; Or, one family ticket (for bob and his four children) plus single tickets for the remaining two.

Write a program to determine which ticket arrangement has the least price. If there are more than one such arrangement, print the arrangement that has the least number of tickets.

Input

Your program will be tested on one or more test cases. The first line of each test case includes two positive integers (S and F) where S is the price of a single ticket and F is the price of a family ticket. The remaining lines of the test case are either the name of a person going by him/herself, or of the form:

N1 N2 N3Nk

where N1 is the name of a parent, with N2Nk being his/her children. Names are all lower-case letters, and no longer than 1000 characters. No parent will be taking more than 1000 of their children to the movies :-). Names are unique, the name of a particular person will appear at most twice: Once as a parent, and once as a child. There will be at least one person and at most 100,000 people in any test case.

The end of a test case is identified by the beginning of the following test case (a line made of two integers.) The end of the last test case is identified by two zeros.

Output

For each test case, write the result using the following format:

k. NS NF T

Where k is the test case number (starting at 1), NS is the number of single tickets, NF is the number of family tickets, and T is the total cost of tickets.

Sample Input

1 3

adam bob cindy

bob dima edie fairuz gary

1 2

john

paul

george

ringo

1 3

a b c

0 0

Sample Output

1. 2 1 5

2. 4 0 4

3. 0 1 3

Source

Arab and North Africa 2007

 

 

中文描述:

一個大家庭一起去電影院看電影,電影院有兩種票提供:個人票和家庭票。個人票只允許一個人進電影院看電影,而家庭票則可允許一個人帶上他的兒子或女兒一起去看電影。給出整個大家庭的家族樹,讓你算出整個大家庭一起去看電影的總費用以及買個人票和家庭票的數目。當存在有總費用相同的多種方案時,選取總票數最少的那個。

 

題目分析與算法模型

很顯然,題目給出的簡化版家譜是一棵樹(每個家庭只有一個父親或母親),每個節點有三種情況:買個人票、買家庭票以及因為父母買了家庭票而不用買票。接著,我們對每一種情況分別討論:

當某個節點買了個人票時,以這個節點為子樹的最優情況(也就是最少費用)是:他的每個孩子的最優情況加上自己買的個人票。

當某個節點買了家庭票是,他的孩子可以選擇買票,也可以選擇不買票。對于他的每個孩子,這個節點會選擇這個孩子買票時的最優情況和不買票的最優情況中的那個費用較低的那個(費用一樣,就選總票數最少的那個),最后再加上自己的家庭票。

當某個節點因為父母買了家庭票而自己不需要買票時,他的最優情況是:他的每個孩子買票時的最優情況的總和。

無論當前節點選取的是什么情況,也無論這個節點會選取他的孩子的哪種情況,但肯定的是,只要當前節點在某種情況下(個人票、家庭票、不買票)達到最優,他的孩子也必然是某種情況下的最優。也就是,當原問題最優時,子問題也最優,因此問題具有最優子結構,而且在求解過程中,會多次計算某個節點在某種情況下的最優值,因此問題求解過程中具有重疊子問題。所以說,該問題可以用DP來解決。

 

代碼:

#include <iostream>

#include <string>

#include <map>

#include <vector>

using namespace std;

 

const int MAX = 100005;

 

int single, family;

int a, b;               //暫時記錄個人票和家庭票的價格

map<string, int> dic;   //將名字與一數字進行映射

map<string, int>::iterator it;          //迭代器

vector<int> sons[MAX];  //鄰接表,記錄某個節點的孩子

int people;             //家庭中的人數

int notRoot[MAX];

char s[1005];           //暫時存儲輸入的姓名

 

struct Node

{                 

   int singleNum, familyNum;

   int minPrice;

};

//某個節點買票時的最優情況和不買票時的最優情況

Node minBuy[MAX], minNotBuy[MAX];

 

int isNumber (string s)

{

   int i, ans;

   ans = 0;

   for (i=0; i<s.length(); i++)

   {

        if ( s[i] >= '0' && s[i] <= '9' )

              ans = ans*10 + (s[i]-'0');

        else

              return -1;

   }

   return ans;

}

 

void Initial ()

{

   memset(notRoot, 0, sizeof(notRoot));

   people = 0;

   memset(minBuy, -1, sizeof(minBuy));

   memset(minNotBuy, -1, sizeof(minNotBuy));

   dic.clear();

}

 

bool Input ()

{

   //如果上個testcase已經將這次輸入的第一個變量讀入到a,就無需再讀入

   if ( a == -1 )

        scanf("%d", &a);

   scanf("%d", &b);

   single = a;

   family = b;

   a = b = -1;

   if ( single == 0 && family == 0 )

        return false;

 

   char ch;

   int pIndex, sIndex;

   string parent, son;

  

   Initial ();

 

   while (1)

   {

        //先將名字讀入字符串數組,在將其置于string, 這樣做是為了節省時間

        scanf("%s", &s);          

        parent = "";

        parent.append(s);

        a = isNumber(parent);

        if ( a != -1 )       //讀入了下個testcase的數據

              break;

 

        it = dic.find(parent);     //查找有無對應映射

        if ( it ==  dic.end() )

        {

              dic[parent] = people;

              sons[people].clear();

              people++;

        }

 

        pIndex = dic[parent];

 

        //讀取當前節點的孩子

        ch = getchar();

        while ( ch != '\n' )

        {

              scanf("%s", &s);

              son = "";

              son.append(s);       //把字符數組的內容傳給string

              it = dic.find(son);

              if ( it == dic.end() )

              {

                   dic[son] = people;

                   sons[people].clear();

                   people++;

              }

              sIndex = dic[son];

              sons[pIndex].push_back(sIndex);

              notRoot[sIndex] = 1;

 

              ch = getchar();

        }

   }

 

   return true;

}

 

int getMinBuy (int);          //計算買票時的最少費用

int getMinNotBuy (int);       //計算不買票時的最少費用

 

int getMinBuy (int index)

{

   if ( minBuy[index].minPrice != -1 )

        return minBuy[index].minPrice;

 

   int i;

   //當前節點買家庭票

   int singleNum1, familyNum1, price1;

   singleNum1 = 0;

   familyNum1 = 1;

   price1 = family;

   for (i=0; i<sons[index].size(); i++)

   {

        //買票時的費用比不買票時的費用低,或者費用相同時,買票時所買的總票數少

        if ( getMinBuy(sons[index][i]) < getMinNotBuy(sons[index][i])

        || ( getMinBuy(sons[index][i])==getMinNotBuy(sons[index][i])

&& minBuy[sons[index][i]].singleNum+

   minBuy[sons[index][i]].familyNum

   <=

   minNotBuy[sons[index][i]].singleNum +     

   minNotBuy[sons[index][i]].familyNum ) )

        {

              price1 += minBuy[sons[index][i]].minPrice;

              singleNum1 += minBuy[sons[index][i]].singleNum;

              familyNum1 += minBuy[sons[index][i]].familyNum;

        }

        else

        {

              price1 += minNotBuy[sons[index][i]].minPrice;

              singleNum1 += minNotBuy[sons[index][i]].singleNum;

              familyNum1 += minNotBuy[sons[index][i]].familyNum;

        }

   }

 

   //當前節點買個人票

   int singleNum2, familyNum2, price2;

   singleNum2 = 1;

   familyNum2 = 0;

   price2 = single;

   for (i=0; i<sons[index].size(); i++)

   {

        price2 += getMinBuy(sons[index][i]);

        singleNum2 += minBuy[sons[index][i]].singleNum;

        familyNum2 += minBuy[sons[index][i]].familyNum;

   }

   //決定當前節點買票時,是買個人票還是買家庭票

   if ( price1 < price2 || ( price1 == price2 && singleNum1 +

        familyNum1 <= singleNum2 + familyNum2 ) )

   {

        minBuy[index].minPrice = price1;

        minBuy[index].singleNum = singleNum1;

        minBuy[index].familyNum = familyNum1;

   }

   else

   {

        minBuy[index].minPrice = price2;

        minBuy[index].singleNum = singleNum2;

        minBuy[index].familyNum = familyNum2;

   }

 

   return minBuy[index].minPrice;

}

 

int getMinNotBuy (int index)

{

   if ( minNotBuy[index].minPrice != -1 )

        return minNotBuy[index].minPrice;

 

   int i, singleNum, familyNum, price;

   singleNum = familyNum = 0;

   price = 0;

   //取每個孩子買票的最優情況

   for (i=0; i<sons[index].size(); i++)

   {

        price += getMinBuy(sons[index][i]);

        singleNum += minBuy[sons[index][i]].singleNum;

        familyNum += minBuy[sons[index][i]].familyNum;

   }

 

   minNotBuy[index].minPrice = price;

   minNotBuy[index].singleNum = singleNum;

   minNotBuy[index].familyNum = familyNum;

   return minNotBuy[index].minPrice;

}

 

void Solve ()

{

 

   int i, price, singleNum, familyNum;

   price = singleNum = familyNum = 0;

 

   for (i=0; i<people; i++)

   {

        if ( ! notRoot[i] )

        {

              price += getMinBuy(i);

              singleNum += minBuy[i].singleNum;

              familyNum += minBuy[i].familyNum;

        }

   }

 

   printf("%d %d %d\n", singleNum, familyNum, price);

}

 

int main ()

{

   int i = 0;

   a = b = -1;

   while ( Input() )

   {

        printf("%d. ", ++i);

        Solve ();

   }

   return 0;

}

 

心得:

本題有兩個考察的地方,一是是否能對簡單的樹形DP模型進行構建,二是是否有比較強的實際編程能力。第一點我不想再重復,前面講的也比較詳細。做了這題,感覺收獲最大的就是學了不少的編程的小技巧。比如處理輸入輸出(本題的輸入讓人有些頭疼)、利用STL中的map把字符串和整數映射起來(這個很有用)、利用vector建樹、為了節省時間先把字符串讀入到字符數組,再把內容傳給string等等。雖然這些小技巧看起來有些“微不足道”,但在平時做題或者比賽時,如果不熟練掌握這些小技巧,往往會在關鍵時刻阻礙你解題的步伐。算法的理論知識是可以從書本上學到的,但那些編程的技巧卻只能從平時做題的積累中才能慢慢掌握的。其中,我感覺,使用STL可以大大減少編程的復雜程度。雖然對于那些剛接觸算法的同學來說,我并不推薦使用STL,因為這樣會屏蔽掉許多底層的原理。但在比賽時,使用STL還是可以節約不少時間與精力的。STL中,比較常用的有stringvector、map、priority_queue等等,已經有一些做題經驗的同學可以在平時順帶的看一下有關STL的內容(推薦一個網站:http://www.cplusplus.com/,里面幾乎包含所有關于C++函數以及STL的參考資料,大部分函數都帶有簡明的代碼樣例)。

posted on 2008-03-25 23:16 quxiao 閱讀(575) 評論(0)  編輯 收藏 引用 所屬分類: ACM
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品高清免费在线观看| 欧美大片在线观看| 一区二区三区四区五区视频| 国产日本欧美一区二区| 女女同性精品视频| 噜噜噜91成人网| 欧美成va人片在线观看| 久热成人在线视频| 久久成人这里只有精品| 亚洲制服丝袜在线| 亚洲欧美国产不卡| 久久久久久自在自线| 狠狠色丁香久久婷婷综合_中| 欧美色播在线播放| 国产乱码精品一区二区三区av| 国产精品豆花视频| 亚洲国产精彩中文乱码av在线播放| 亚洲欧美日韩综合| 久久午夜精品一区二区| 欧美大片专区| 香蕉国产精品偷在线观看不卡| 久久久久久91香蕉国产| 欧美视频中文一区二区三区在线观看| 米奇777在线欧美播放| 欧美伦理影院| 亚洲国产日韩美| 亚洲在线观看免费视频| 美乳少妇欧美精品| 久久精品国产成人| 国产精品一区二区久久久| 亚洲日韩中文字幕在线播放| 久久久久久久综合狠狠综合| 亚洲欧美激情视频| 国产精品女主播| 亚洲在线播放| 亚洲专区国产精品| 国产精品伊人日日| 香蕉亚洲视频| 欧美在线啊v一区| 国产丝袜一区二区| 欧美中文在线免费| 国产午夜亚洲精品理论片色戒| 久久九九热re6这里有精品| 国产精品入口麻豆原神| 一区二区精品国产| 亚洲午夜电影在线观看| 国产精品视频免费观看www| 香蕉成人久久| 久久久久久亚洲精品不卡4k岛国| 亚洲国产导航| 亚洲成人在线| 亚洲人成网站在线观看播放| 欧美激情中文字幕在线| 亚洲一区激情| 性做久久久久久久免费看| 韩国一区二区三区在线观看| 免费中文字幕日韩欧美| 欧美日韩国产成人| 久久黄色影院| 欧美午夜电影一区| 久久久综合网站| 欧美激情第10页| 久久在线免费观看| 欧美午夜视频一区二区| 久久久久久亚洲精品不卡4k岛国| 久久网站免费| 欧美一区二区视频在线观看| 欧美a级片网站| 老司机午夜精品视频在线观看| 欧美日韩国产另类不卡| 欧美激情在线| 最新高清无码专区| 久久久久久国产精品mv| 久久精品免费| 欧美亚洲一区二区在线| 久久蜜桃精品| 欧美三级乱码| 亚洲精品专区| 亚洲自拍三区| 国产一区二区三区奇米久涩| 亚洲在线播放电影| 久久国产主播精品| 一区二区三区在线免费播放| 久久精品二区| 亚洲电影天堂av| 亚洲精品社区| 久久久久www| 国产亚洲日本欧美韩国| 久久成人国产精品| 亚洲人成在线免费观看| 亚洲国产导航| 国产精品国产三级国产aⅴ入口 | 久久高清福利视频| 国产一区二区三区在线观看免费| 国产亚洲精品一区二555| 日韩视频一区二区在线观看| 欧美人与性禽动交情品| 亚洲欧美日韩第一区| 老色批av在线精品| 午夜精品久久久久久久| 亚洲国产高清一区| 国产精品永久免费在线| 欧美激情中文字幕一区二区| 亚洲在线观看免费视频| 欧美国产91| 久久久夜精品| 欧美在线播放高清精品| 亚洲国产高清视频| 在线观看欧美黄色| 国产一区日韩欧美| 国产精品一区二区久久国产| 欧美日韩在线观看一区二区| 老鸭窝亚洲一区二区三区| 欧美在线free| 亚洲欧美日韩一区在线| 中日韩美女免费视频网址在线观看 | 久久激情五月婷婷| 一本色道久久综合狠狠躁篇怎么玩 | 久久精品国产91精品亚洲| 一区二区三区毛片| 亚洲一区二区高清| 亚洲视频香蕉人妖| 亚洲欧美电影在线观看| 午夜精品福利在线| 久久九九久久九九| 久久久久国产精品麻豆ai换脸| 久久久久成人精品| 亚洲福利国产| 亚洲人体一区| 亚洲一级黄色片| 久久露脸国产精品| 欧美午夜不卡在线观看免费| 久久综合给合| 国产精品久久久久久一区二区三区 | 亚洲欧洲日产国产网站| 日韩亚洲精品视频| 久久精品主播| 国产精品久久网| 日韩午夜在线视频| 欧美一级专区免费大片| 亚洲电影免费在线| 久久久久久久久蜜桃| 欧美性猛交xxxx乱大交蜜桃 | 久久一区二区三区av| 国产精品美女黄网| 久久大逼视频| 国产日韩欧美夫妻视频在线观看| 亚洲经典一区| 欧美激情一区二区三区蜜桃视频| 亚洲欧美日韩人成在线播放| 欧美成人综合一区| 在线成人www免费观看视频| 午夜日韩在线| 性色av一区二区三区在线观看 | 欧美日韩在线免费观看| 亚洲国产精品第一区二区| 久久精品日韩欧美| 亚洲欧美激情精品一区二区| 欧美视频在线视频| 亚洲欧美精品| 欧美中文字幕视频在线观看| 国产综合在线看| 亚洲国产婷婷香蕉久久久久久99| 久久久一本精品99久久精品66| 好吊日精品视频| 亚洲人成在线观看一区二区| 欧美日韩亚洲国产精品| 欧美一区影院| 欧美精品七区| 乱码第一页成人| 欧美午夜宅男影院| 噜噜噜91成人网| 国产精品福利在线| 免费不卡亚洲欧美| 国产精品欧美日韩一区| 欧美亚洲日本网站| 欧美激情影音先锋| 亚洲午夜女主播在线直播| 欧美一区二视频在线免费观看| 亚洲大片一区二区三区| 夜夜嗨av一区二区三区网站四季av| 国产婷婷色一区二区三区在线 | 亚洲欧美日韩另类精品一区二区三区| 午夜天堂精品久久久久| 久久久国产一区二区三区| 亚洲免费人成在线视频观看| 久久香蕉国产线看观看av| 亚洲综合日韩在线| 欧美激情中文字幕一区二区| 牛人盗摄一区二区三区视频| 国产精品一区二区你懂得| 亚洲精品国产系列| 亚洲毛片av| 欧美精品一区在线观看| 亚洲精品欧美在线| 日韩一级在线观看| 欧美日韩一区二区在线观看视频| 亚洲精品免费一区二区三区| 日韩一级欧洲| 国产精品国产三级国产aⅴ无密码|