• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 216615
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

            Apple Tree
            Time Limit:1000MS? Memory Limit:65536K
            Total Submit:541 Accepted:148

            Description
            Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

            Input
            There are several test cases in the input
            Each test case contains three parts.
            The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)
            The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i.
            The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent.
            Input will be ended by the end of file.

            Note: Wshxzt starts at Node 1.

            Output
            For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

            Sample Input

            2 1 
            0 11
            1 2
            3 2
            0 1 2
            1 2
            1 3
            

            Sample Output

            11
            2
            

            Source
            POJ Contest,Author:magicpig@ZSU


            #include? < iostream >
            using?namespace?std;

            const ? int ?N? = ? 210 ;

            int ?adj[N][N];
            int ?n,?k;
            int ?w[N];
            int ?go[N][N],?bk[N][N];

            void ?solve();
            void ?dfs( int ,? int );
            void ?dp( int ,? int );
            inline?
            int ?max( int ?a,? int ?b)? {
            ????
            return ?a? > ?b? ? ?a?:?b;
            }


            int ?main()
            {
            ????
            while ?(scanf( " %d%d " ,? & n,? & k)? != ?EOF)? {
            ????????solve();
            ????}

            ????
            return ? 0 ;
            }


            void ?solve()? {
            ????
            int ?i,?j,?l;
            ????
            int ?x,?y;

            ????
            for ?(i = 1 ;?i <= n;?i ++ )? {
            ????????scanf(
            " %d " ,? & w[i]);
            ????????adj[i][
            0 ]? = ? 0 ;
            ????}


            ????
            for ?(i = 0 ;?i < n - 1 ;?i ++ )? {
            ????????scanf(
            " %d%d " ,? & x,? & y);
            ????????adj[x][
            ++ adj[x][ 0 ]]? = ?y;
            ????????adj[y][
            ++ adj[y][ 0 ]]? = ?x;
            ????}

            ????
            ????memset(go,?
            0 ,?sizeof(go));
            ????memset(bk,?
            0 ,?sizeof(bk));

            ????dfs(
            1 ,? 0 );

            ????
            int ?ans? = ?max(go[ 1 ][k],?bk[ 1 ][k]);
            ????printf(
            " %d\n " ,?ans? + ?w[ 1 ]);
            }


            void ?dfs( int ?p,? int ?pp)? {
            ????
            int ?i,?j,?l;
            ????
            int ?ts;????

            ????
            for ?(i = 1 ;?i <= adj[p][ 0 ];?i ++ )? {
            ????????ts?
            = ?adj[p][i];
            ????????
            if ?(ts? == ?pp)? continue ;
            ????????dfs(ts,?p);
            ????????bk[ts][
            0 ]? = ? 0 ;
            ????????bk[ts][
            1 ]? = ? 0 ;
            ????????go[ts][
            0 ]? = ? 0 ;
            ????????
            for ?(l = k;?l >= 2 ;?l -- )?bk[ts][l]? = ?bk[ts][l - 2 ]? + ?w[ts];
            ????????
            for ?(l = k;?l >= 1 ;?l -- )?go[ts][l]? = ?go[ts][l - 1 ]? + ?w[ts];
            ????????dp(p,?ts);
            ????}

            }


            void ?dp( int ?x,? int ?y)? {
            ????
            int ?i,?j,?l;
            ????
            int ?t1[N],?t2[N];
            ????memset(t1,?
            0 ,?sizeof(t1));
            ????memset(t2,?
            0 ,?sizeof(t2));
            ????
            for ?(i = 0 ;?i <= k;?i ++ )? {
            ????????
            for ?(j = 0 ;?j <= i;?j ++ )? {
            ????????????t1[i]?
            = ?max(t1[i],?max(bk[x][j] + go[y][i - j],?bk[y][j] + go[x][i - j]));
            ????????}

            ????}

            ????
            for ?(i = 0 ;?i <= k;?i ++ )? {
            ????????
            for ?(j = 0 ;?j <= i;?j ++ )? {
            ????????????t2[i]?
            = ?max(t2[i],?bk[x][j] + bk[y][i - j]);
            ????????}

            ????}

            ????
            for (i = 0 ;?i <= k;?i ++ )? {
            ????????bk[x][i]?
            = ?t2[i];
            ????????go[x][i]?
            = ?t1[i];
            ????}

            }

            posted on 2007-02-10 18:55 閱讀(1713) 評論(0)  編輯 收藏 引用 所屬分類: ACM題目
            热99re久久国超精品首页| 久久亚洲精品无码观看不卡| 蜜臀av性久久久久蜜臀aⅴ| 久久Av无码精品人妻系列| 国产精品久久自在自线观看| 久久国产三级无码一区二区| 久久婷婷五月综合成人D啪 | 7国产欧美日韩综合天堂中文久久久久| 97精品久久天干天天天按摩 | 色妞色综合久久夜夜| 亚洲狠狠婷婷综合久久蜜芽| 四虎国产精品免费久久5151| 精品久久久久成人码免费动漫| 欧美黑人又粗又大久久久| 精品久久久无码中文字幕天天| 亚洲色大成网站www久久九| 国产999精品久久久久久| 激情伊人五月天久久综合| 亚洲国产成人久久笫一页| 国产精品日韩欧美久久综合| 久久九九精品99国产精品| 97久久国产综合精品女不卡 | 亚洲成av人片不卡无码久久| 91精品国产高清久久久久久91| 久久久无码精品亚洲日韩京东传媒| 国产91久久综合| 国产69精品久久久久99| 久久se精品一区二区| 人妻少妇久久中文字幕| 久久九九兔免费精品6| 区久久AAA片69亚洲 | 7777久久亚洲中文字幕| 久久亚洲国产成人精品性色| 中文字幕日本人妻久久久免费 | 亚洲中文久久精品无码| 婷婷国产天堂久久综合五月| 人妻精品久久久久中文字幕 | 久久男人Av资源网站无码软件| 日韩精品久久久肉伦网站| 久久人人妻人人爽人人爽| 久久久久成人精品无码中文字幕|