﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Sephiroth's boring days!!!</title><link>http://www.cppblog.com/sephiroth/</link><description>Love just for you.</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:06:42 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:06:42 GMT</pubDate><ttl>60</ttl><item><title>注目了阿！！！～～～</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/11/126399.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Sat, 11 Sep 2010 04:09:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/11/126399.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/126399.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/11/126399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/126399.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/126399.html</trackback:ping><description><![CDATA[更换blog ，因为特殊原因

http://www.cnblogs.com/sephirothlee/<img src ="http://www.cppblog.com/sephiroth/aggbug/126399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-11 12:09 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/11/126399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>WIN7体验</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/07/126073.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Tue, 07 Sep 2010 02:09:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/07/126073.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/126073.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/07/126073.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/126073.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/126073.html</trackback:ping><description><![CDATA[<p><font face="微软雅黑">今天用差不多半天的时间把WIN7装好了，太累人了。</font></p> <p><font face="微软雅黑">显卡还是太垃圾…… 等放假去宾馆卸一个回来！！……（有点不好的说）</font></p> <p><font face="微软雅黑">1G的内存，占用量一直在54%左右。确实很吃硬件。</font></p><img src ="http://www.cppblog.com/sephiroth/aggbug/126073.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-07 10:09 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/07/126073.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>&amp;ldquo;长沙一中学习&amp;rdquo;-day4</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/04/125849.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Fri, 03 Sep 2010 23:56:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/04/125849.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125849.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/04/125849.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125849.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125849.html</trackback:ping><description><![CDATA[<p>今天上午是搜索。</p> <ol> <li>最少乘法次数</li> <li>彩票问题</li> <li>黑白棋游戏</li> <li>pku1729</li> <li>多处理机调度问题</li> <li>魔盘</li> <li>最短编号序列</li> <li>任务安排</li> <li>数字游戏(c&amp;m)</li> <li>埃及分数</li> <li>antiprime</li></ol> <p>下午是数学的一些东西。</p> <ol> <li>第二类string数</li> <li>卡特兰数</li> <li>polya原理</li> <li>整数分解</li> <li>极值问题</li> <li>Kathy函数</li> <li>错排问题</li> <li>求Fibonacci数列</li> <li>毛毛虫</li> <li>一序列</li> <li>圆桌吃饭</li> <li>01串问题</li> <li>算术表达式的计算</li></ol><img src ="http://www.cppblog.com/sephiroth/aggbug/125849.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-04 07:56 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/04/125849.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>时间奔流</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125687.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 13:54:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125687.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125687.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125687.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125687.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125687.html</trackback:ping><description><![CDATA[<p>时间</p> <p>奔流。</p> <p>本来是EVA的一个同人小说、某网友的ID。现在突然有这种感觉了。</p> <p>五天的集训，现在第三天就要结束了，明天就是3号。</p> <p>4号，5号，6号回东区。然后猛补文化课，再有一个月，就开始最后的准备了。</p> <p>11月21日，上战场的日子。</p> <p>为了宝贝儿，为了我自己。</p> <p><a href="http://www.cppblog.com/images/cppblog_com/sephiroth/WindowsLiveWriter/25a36ba4ed17_6CE7/001_2.jpg"><img title="001" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="480" alt="001" src="http://www.cppblog.com/images/cppblog_com/sephiroth/WindowsLiveWriter/25a36ba4ed17_6CE7/001_thumb.jpg" width="368" border="0"></a></p><img src ="http://www.cppblog.com/sephiroth/aggbug/125687.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 21:54 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125687.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>&amp;ldquo;长沙一中学习&amp;rdquo;-day3</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125682.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 12:58:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125682.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125682.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125682.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125682.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125682.html</trackback:ping><description><![CDATA[<p>上午讲的动态规划的优化。有很多经典的例题。</p> <ol> <li>宝物筛选</li> <li>农田个数</li> <li>花店橱窗布置</li> <li>乘车路线</li> <li>叠放箱子</li> <li>演唱组</li> <li>divide</li> <li>添加号</li> <li>走道铺砖问题</li> <li>股票问题</li> <li>火车票</li> <li>杀蚂蚁</li> <li>瑰丽华尔兹</li></ol> <p>下午开始了搜索，有点晕。</p> <ol> <li>走迷宫问题</li> <li>订货单</li> <li>计算机网络连接</li> <li>N皇后</li> <li>Betsy’s tour</li></ol> <p>晚上好好休息。</p><img src ="http://www.cppblog.com/sephiroth/aggbug/125682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 20:58 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树归-珠宝</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125678.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 12:40:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125678.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125678.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125678.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125678.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125678.html</trackback:ping><description><![CDATA[<p>【描述】  <p>给一棵n个结点的树，给每个点安排一个正整数编号，使得相邻点具有不同的编号，编号的总和尽量小。<br>【输入】<br>第一行：n(n&lt;=50,000)<br>以下n-1行，每行两个数u,v(1&lt;=u,v&lt;=n)，表示u 和v有一条边<br>【输出】<br>仅一行，为最小编号和<br>【样例输入】<br>8<br>1 2<br>1 3<br>1 4<br>1 5<br>5 6<br>5 7<br>5 8<br>【样例输出】<br>11  <p>【分析】  <p>f[i][j]表示i这个点标j这个数所能到达的最小总值。控制j的范围到30肯定过。</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define MAXINT 10000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: #define maxn 50010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> f[maxn][31];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> bl[maxn][maxn/100];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> son[maxn][maxn/100],root[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> n;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> x,y;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">int</span> ans=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: <span style="color: #0000ff">void</span> maket(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=bl[x][0];++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:         <span style="color: #0000ff">int</span> k=bl[x][i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:         <span style="color: #0000ff">if</span> (k==root[x]) <span style="color: #0000ff">continue</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         son[x][++son[x][0]]=k;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         root[k]=x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:         maket(k);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: <span style="color: #0000ff">void</span> dp(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:     <span style="color: #0000ff">if</span> (f[x][1]) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=30;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=son[x][0];++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:             <span style="color: #0000ff">int</span> tt=son[x][j];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:             dp(tt);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:             <span style="color: #0000ff">int</span> minn=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> jj=1;jj&lt;=30;++jj)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:                 <span style="color: #0000ff">if</span> (jj!=i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:                     <span style="color: #0000ff">if</span> (f[tt][jj]&lt;minn)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:                         minn=f[tt][jj];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:             f[x][i]+=minn;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:         f[x][i]+=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:     freopen("<span style="color: #8b0000">gems.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:     freopen("<span style="color: #8b0000">gems.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:     scanf("<span style="color: #8b0000">%d</span>",&amp;n);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54:         scanf("<span style="color: #8b0000">%d%d</span>",&amp;x,&amp;y);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 55:         bl[x][++bl[x][0]]=y;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 56:         bl[y][++bl[y][0]]=x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 57:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 58:     maket(1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 59:     dp(1);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 60:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=30;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 61:         <span style="color: #0000ff">if</span> (f[1][i]&lt;ans)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 62:             ans=f[1][i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 63:     printf("<span style="color: #8b0000">%d\n</span>",ans);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 64:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 65: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 66: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125678.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 20:40 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125678.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划-皇宫看守</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125675.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 11:58:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125675.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125675.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125675.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125675.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125675.html</trackback:ping><description><![CDATA[<h5></h5> <p>【问题描述】</p> <p>太平王世子事件后，陆小凤成了皇上特聘的御前一品侍卫。</p> <p>皇宫以午门为起点，直到后宫嫔妃们的寝宫，呈一棵树的形状；某些宫殿间可以互相望见。大内保卫森严，三步一岗，五步一哨，每个宫殿都要有人全天候看守，在不同的宫殿安排看守所需的费用不同。</p> <p>可是陆小凤手上的经费不足，无论如何也没法在每个宫殿都安置留守侍卫。</p> <p>帮助陆小凤布置侍卫，在看守全部宫殿的前提下，使得花费的经费最少。</p> <p>【数据输入】</p> <p>输入数据由文件名为INPUT.TXT的文本文件提供。输入文件中数据表示一棵树，描述如下：</p> <p>第1行 n，表示树中结点的数目。</p> <p>第2行至第n+1行，每行描述每个宫殿结点信息，依次为：该宫殿结点标号i（0&lt;i&lt;=n），在该宫殿安置侍卫所需的经费k，该边的儿子数m，接下来m个数，分别是这个节点的m个儿子的标号r1，r2，...，rm。</p> <p>对于一个n（0 &lt; n &lt;= 1500）个结点的树，结点标号在1到n之间，且标号不重复。</p> <p>【数据输出】</p> <p>输出到OUTPUT.TXT文件中。输出文件仅包含一个数，为所求的最少的经费。</p> <p><img height="159" src="http://www.ahhf45.com/info/Data_Structures_and_Algorithms/problems/problem_set/guard/images/sgoi21.gif" width="159" align="left"></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>输入数据示例 　输出数据示例</p> <p><img height="148" src="http://www.ahhf45.com/info/Data_Structures_and_Algorithms/problems/problem_set/guard/images/sgoi22.gif" width="78" align="textTop">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 25</p> <p>【分析】</p> <p>分别用f[i][0]表示i点放看守，f[i][1]表示i点不放看守i点被儿子监视，f[i][2]表示i点不放看守i点被父节点监视三个情况下的最小费用。</p> <p>f[i][0]=所有子节点t的f[t][0],f[t][1],f[t][2]中最小的一个的合+k[i]</p> <p>f[i][1]=某个子节点放看守+其他节点的f[t][0],f[t][1]中最小的一个的合</p> <p>f[i][2]=所有子节点的f[t][1]的合</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define maxn 1510
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: #define MAXINT 10000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> son[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> m[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> n,x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> k[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> tem[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">bool</span> ro[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: <span style="color: #0000ff">int</span> v;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: <span style="color: #0000ff">int</span> f[maxn][3];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16: <span style="color: #0000ff">void</span> dp(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     <span style="color: #0000ff">if</span> (f[x][0]) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=m[x];++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         <span style="color: #0000ff">int</span> t=son[x][i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:         dp(t);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:         f[x][0]+=min(f[t][0],min(f[t][1],f[t][2]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:         f[x][2]+=f[t][1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:     f[x][0]+=k[x];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     memset(tem,0,<span style="color: #0000ff">sizeof</span>(tem));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:     <span style="color: #0000ff">int</span> tot=0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=m[x];++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:         <span style="color: #0000ff">int</span> t=son[x][i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:         tem[i]=min(f[t][0],f[t][1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:         tot+=tem[i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:     f[x][1]=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=m[x];++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:         <span style="color: #0000ff">int</span> t=son[x][i];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:         <span style="color: #0000ff">if</span> (tot-tem[i]+f[t][0]&lt;f[x][1]) f[x][1]=tot-tem[i]+f[t][0];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:     freopen("<span style="color: #8b0000">guard.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:     freopen("<span style="color: #8b0000">guard.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:     scanf("<span style="color: #8b0000">%d</span>",&amp;n);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:         scanf("<span style="color: #8b0000">%d</span>",&amp;x);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:         scanf("<span style="color: #8b0000">%d%d</span>",&amp;k[x],&amp;m[x]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=m[x];++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 55:             scanf("<span style="color: #8b0000">%d</span>",&amp;son[x][j]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 56:             ro[son[x][j]]=1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 57:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 58:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 59:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 60:         <span style="color: #0000ff">if</span> (!ro[i])
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 61:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 62:             v=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 63:             <span style="color: #0000ff">break</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 64:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 65:     <span style="color: #008000">//for (int i=1;i&lt;=n;++i)</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 66:     <span style="color: #008000">//f[i][2]=f[i][1]=MAXINT;</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 67:     dp(v);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 68:     printf("<span style="color: #8b0000">%d\n</span>",min(f[v][0],f[v][1]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 69:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 70: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 71: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 19:58 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>聚会的快乐</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125672.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 11:15:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125672.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125672.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125672.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125672.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125672.html</trackback:ping><description><![CDATA[<p>题目见ural1039</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;stdlib.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: #define maxn 6010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">struct</span> ss
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9:     <span style="color: #0000ff">int</span> ro,nu;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: } a[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> w[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">int</span> f[maxn][2];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: <span style="color: #0000ff">int</span> n;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14: <span style="color: #0000ff">int</span> x,y;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15: <span style="color: #0000ff">int</span> t[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17: <span style="color: #0000ff">int</span> cmp(<span style="color: #0000ff">const</span> <span style="color: #0000ff">void</span>*a,<span style="color: #0000ff">const</span> <span style="color: #0000ff">void</span>*b)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:     ss c=*(ss*)a,d=*(ss*)b;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:     <span style="color: #0000ff">if</span> (c.ro&lt;d.ro) <span style="color: #0000ff">return</span> -1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:     <span style="color: #0000ff">if</span> (c.ro&gt;d.ro) <span style="color: #0000ff">return</span> 1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25: <span style="color: #0000ff">void</span> dp(<span style="color: #0000ff">int</span> x)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     <span style="color: #0000ff">if</span> (f[x][1]) <span style="color: #0000ff">return</span>;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=t[x];i&lt;t[x+1];++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         <span style="color: #0000ff">int</span> k=a[i].nu;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:         dp(k);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:         f[x][1]+=f[k][0];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:         f[x][0]+=max(f[k][0],f[k][1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:     f[x][1]+=w[x];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:     freopen("<span style="color: #8b0000">party.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:     freopen("<span style="color: #8b0000">party.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:     scanf("<span style="color: #8b0000">%d</span>",&amp;n);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) scanf("<span style="color: #8b0000">%d</span>",&amp;w[i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;x,&amp;y);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46:     <span style="color: #0000ff">while</span> (!((!x)&amp;&amp;(!y)))
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 48:         a[x].nu=x;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 49:         a[x].ro=y;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 50:         scanf("<span style="color: #8b0000">%d%d</span>",&amp;x,&amp;y);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 51:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 52:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 53:         <span style="color: #0000ff">if</span> (!a[i].ro)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 54:             a[i].nu=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 55:     a[0].ro=-1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 56:     qsort(a,n+1,<span style="color: #0000ff">sizeof</span>(ss),cmp);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 57:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 58:         <span style="color: #0000ff">if</span> (!t[a[i].ro])
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 59:             t[a[i].ro]=i;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 60:     t[n+1]=n+1;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 61:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=n;i&gt;0;--i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 62:         <span style="color: #0000ff">if</span> (!t[i])
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 63:             t[i]=t[i+1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 64:     dp(0);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 65:     printf("<span style="color: #8b0000">%d\n</span>",max(f[0][1],f[0][0]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 66:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 67: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 68: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125672.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 19:15 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125672.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>任务安排</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125671.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 10:39:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125671.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125671.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125671.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125671.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125671.html</trackback:ping><description><![CDATA[<p>【描述】 <p>N个任务排成一个序列在一台机器上等待完成（顺序不得改变），这N个任务被分成若干批，每批包含相邻的若干任务。从时刻0开始，这些任务被分批加工，第i个任务单独完成所需的时间是Ti。在每批任务开始前，机器需要启动时间S，而完成这批任务所需的时间是各个任务需要时间的总和（同一批任务将在同一时刻完成）。每个任务的费用是它的完成时刻乘以一个费用系数Fi。请确定一个分组方案，使得总费用最小。 例如：S=1；T={1,3,4,2,1}；F={3,2,3,3,4}。如果分组方案是{1,2}、{3}、{4,5}，则完成时间分别为{5,5,10,14,14}，费用C{15,10,30,42,56}，总费用就是153。 </p> <p>【输入】</p> <p>第一行是N(1&lt;=N&lt;=5000)。 第二行是S(0&lt;=S&lt;=50)。 下面N行每行有一对数，分别为Ti和Fi，均为不大于100的正整数，表示第i个任务单独完成所需的时间是Ti及其费用系数Fi。 </p> <p>【输出】 </p> <p>一个数，最小的总费用。<br>【样例】</p> <table cellspacing="0" cellpadding="2" width="400" border="0"> <tbody> <tr> <td valign="top" width="200">BATCH.IN</td> <td valign="top" width="200">BATCH.OUT </td></tr> <tr> <td valign="top" width="200">5<br> 1<br> 1 3<br> 3 2<br> 4 3<br>2 3<br> 1 4</td> <td valign="top" width="200">153</td></tr></tbody></table> <p>【分析】</p> <p>dp[i]：完成工作1到工作I的费用+<u>因增加S从I到N增加的费用的</u>总和的最小费用。</p> <p>dp[i]=min{dp[k-1]+s*(f[n]-f[k-1])+t[i]*(f[i]-f[k-1])}</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define MAXINT 10000000
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: #define maxn 5010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> n,s;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> t[maxn],f[maxn],dp[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> tem;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13:     freopen("<span style="color: #8b0000">batch.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:     freopen("<span style="color: #8b0000">batch.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;s);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:         scanf("<span style="color: #8b0000">%d%d</span>",&amp;t[i],&amp;f[i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         t[i]+=t[i-1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:         f[i]+=f[i-1];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) dp[i]=MAXINT;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:     dp[0]=0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> k=1;k&lt;=i;++k)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:             tem=dp[k-1]+s*(f[n]-f[k-1])+t[i]*(f[i]-f[k-1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:             <span style="color: #0000ff">if</span> (tem&lt;dp[i]) dp[i]=tem;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     printf("<span style="color: #8b0000">%d\n</span>",dp[n]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125671.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 18:39 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125671.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划-关灯</title><link>http://www.cppblog.com/sephiroth/archive/2010/09/02/125617.html</link><dc:creator>Sephiroth Lee</dc:creator><author>Sephiroth Lee</author><pubDate>Thu, 02 Sep 2010 03:54:00 GMT</pubDate><guid>http://www.cppblog.com/sephiroth/archive/2010/09/02/125617.html</guid><wfw:comment>http://www.cppblog.com/sephiroth/comments/125617.html</wfw:comment><comments>http://www.cppblog.com/sephiroth/archive/2010/09/02/125617.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sephiroth/comments/commentRss/125617.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sephiroth/services/trackbacks/125617.html</trackback:ping><description><![CDATA[<p>【描述】 <p>多瑞卡得到了一份有趣而高薪的工作。每天早晨他必须关掉他所在村庄的街灯。所有的街灯都被设置在一条直路的同一侧。<br>多瑞卡每天到早晨5点钟都按时起床，然后他开始关灯。开始时，他站在某一盏路灯的旁边。<br>每盏灯都有一个给定功率的电灯泡，因为多端卡有着自觉的节能意识，他希望在耗电能总数最少的情况下将所有的灯关掉。<br>多端卡因为太累了，所以只能以1m/s的速度行走。关灯不需要花费额外的时间，因为当他通过时就能将灯关掉。<br>编写程序，计算在给定路灯设置，灯泡功率以及多端卡的起始位置的情况下关掉所有的灯需耗费的最小能量。<br>【输入】<br>输入文件的第一行包含一个整数N，2≤N≤1000，表示该村庄路灯的数量。<br>第二行包含一个整数V，1≤V≤N，表示多瑞卡开始关灯的路灯号码。<br>接下来的N行中，每行包含两个用空格隔开的整数D和W，用来描述每盏灯的参数，其中0≤D≤1000，0≤W≤1000。D表示该路灯与村庄开始处的距离(用米为单位来表示)，W表示灯泡的功率，即在每秒钟该灯泡所消耗的能量数。路灯是按顺序给定的。<br>【输出】<br>输出文件的第一行即唯一的一行应包含一个整数，即消耗能量之和的最小值。注意结果不超过1,000,000,000。<br>【样例】</p> <table cellspacing="0" cellpadding="2" width="400" border="0"> <tbody> <tr> <td valign="top" width="200">POWER.IN</td> <td valign="top" width="200">POWER.OUT</td></tr> <tr> <td valign="top" width="200">4<br>3<br>2 2<br>5 8<br>6 1<br>8 7</td> <td valign="top" width="200">56</td></tr></tbody></table> <p>【分析】</p> <p>用c[i][j]表示关掉i到j的灯，<u>剩下的灯</u>的功率。</p> <p>f[i][j][0]表示v左面关掉i，右面关掉j盏灯，最后停到左面。</p> <p>f[i][j][1]表示v左面关掉i，右面关掉j盏灯，最后停到右面。</p> <p>初始条件：</p> <p>f[0,0,0]=f[1,0,0]=0 </p> <p>转移方程：//对于所有的i和j，满足0&lt;i&lt;=v-1，0&lt;j&lt;=n-v </p> <p>f[0,I,0]=f[0,i-1,0]+c[v-i+1,v]*(d[v-i+1]-d[v-i]){从i-1走到i所耗功率}</p> <p>f[1,I,0]=f[0,I,0]+c[v-I,v]*(d[v]-d[v-i]){ {从i走到v所耗功率} </p> <p>f[1,0,j]=f[1,0,j-1]+c[v,v+j-1]*(d[v+j]-d[v+j-1]) </p> <p>f[0,0,j]=f[1,0,j]+c[v,v+j]*(d[v+j]-d[v]) </p> <p>f[0,I,j]=min{f[0,i-1,j]+(d[v-i+1]-d[v-i])*c[v-i+1,v+j]，f[1,i-1,j]+(d[v+j]-d[v-i])*c[v-i+1,v+j]} </p> <p>f[1,I,j]=min{f[1,I,j-1]+(d[v+j]-d[v+j-1])*c[v-I,v+j-1]，f[0][I,j-1]+(d[v+j]-d[v-i])*c[v-I,v+j-1]} </p> <p>最终结果： Result=min{f[0,v-1,n-v],f[1,v-1,n-v]}</p><pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 650px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  1: #include &lt;stdio.h&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  2: #include &lt;iostream&gt;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  3: #define maxn 1010
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  4: <span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  5: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  6: <span style="color: #0000ff">int</span> c[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  7: <span style="color: #0000ff">int</span> sum[maxn][maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff">  8: <span style="color: #0000ff">int</span> d[maxn],w[maxn];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  9: <span style="color: #0000ff">int</span> n,v;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 10: <span style="color: #0000ff">int</span> f[maxn][maxn][2];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 11: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 12: <span style="color: #0000ff">int</span> main()
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 13: {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 14:     freopen("<span style="color: #8b0000">power.in</span>","<span style="color: #8b0000">r</span>",stdin);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 15:     freopen("<span style="color: #8b0000">power.out</span>","<span style="color: #8b0000">w</span>",stdout);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 16:     
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 17:     scanf("<span style="color: #8b0000">%d%d</span>",&amp;n,&amp;v);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 18:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i) scanf("<span style="color: #8b0000">%d%d</span>",&amp;d[i],&amp;w[i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 19:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 20:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i;j&lt;=n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 21:             sum[i][j]=sum[i][j-1]+w[j];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 22:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=n;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 23:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=i;j&lt;=n;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 24:             c[i][j]=sum[1][n]-sum[i][j];
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 25:     memset(f,63,<span style="color: #0000ff">sizeof</span>(f));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 26:     f[0][0][0]=f[0][0][1]=0; <span style="color: #008000">//0left 1right</span>
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 27:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=v-1;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 28:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 29:         f[i][0][0]=f[i-1][0][0]+c[v-i+1][v]*(d[v-i+1]-d[v-i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 30:         f[i][0][1]=f[i][0][0]+c[v-i][v]*(d[v]-d[v-i]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 31:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 32:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=n-v;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 33:     {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 34:         f[0][j][1]=f[0][j-1][1]+c[v][v+j-1]*(d[v+j]-d[v+j-1]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 35:         f[0][j][0]=f[0][j][1]+c[v][v+j]*(d[v+j]-d[v]);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 36:     }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 37:     <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i=1;i&lt;=v-1;++i)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 38:         <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j=1;j&lt;=n-v;++j)
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 39:         {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 40:             f[i][j][0]=min(f[i-1][j][0]+c[v-i+1][v+j]*(d[v-i+1]-d[v-i]),f[i-1][j][1]+c[v-i+1][v+j]*(d[v+j]-d[v-i]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 41:             f[i][j][1]=min(f[i][j-1][1]+c[v-i][v+j-1]*(d[v+j]-d[v+j-1]),f[i][j-1][0]+c[v-i][v+j-1]*(d[v+j]-d[v-i]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 42:         }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 43:     printf("<span style="color: #8b0000">%d\n</span>",min(f[v-1][n-v][1],f[v-1][n-v][0]));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 44:     <span style="color: #0000ff">return</span> 0;
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 45: }
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #ffffff"> 46: 
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"> 47: </pre></pre><img src ="http://www.cppblog.com/sephiroth/aggbug/125617.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sephiroth/" target="_blank">Sephiroth Lee</a> 2010-09-02 11:54 <a href="http://www.cppblog.com/sephiroth/archive/2010/09/02/125617.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>