﻿<?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++博客-止于自娱-随笔分类-图论</title><link>http://www.cppblog.com/yuziyu/category/11019.html</link><description>每天进步一点点,Coding Everyday!</description><language>zh-cn</language><lastBuildDate>Mon, 27 Jul 2009 23:06:38 GMT</lastBuildDate><pubDate>Mon, 27 Jul 2009 23:06:38 GMT</pubDate><ttl>60</ttl><item><title>USACO 4.1 Fence Loops</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 17 Jul 2009 06:26:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/90336.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/90336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/90336.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 求无向图中的一个最小环的长度&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/90336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-17 14:26 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/17/90336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.4 American Heritage</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Fri, 10 Jul 2009 10:51:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89748.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89748.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89748.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 给出一个树的中序遍历和先序遍历，求它的后序遍历。&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-10 18:51 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/10/89748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.3 Shopping Offers</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 07 Jul 2009 11:16:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89483.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89483.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89483.html</trackback:ping><description><![CDATA[背包问题，dp解决。<br />dp[i][j][k][m][n]表示第0-4种物品分别购买i-n个时，所需的最小费用。<br />那么对于每一个offer<br />dp[i][j][k][m][n] = min ( dp[i][j][k][m][n], dp[i-o[0]][j-o[1]][k-o[2]][m-o[3]][n-o[4]]+offer.cost);<br /><br />analysis中提出用最短路径的方法来解，思路很巧妙。把”装有不同种数和数量的物品“的篮子看作结点，把offer作为边(把购买一件物品的原始价格看成一种退化的offer)，把价格作为边长度。这样就转换成从篮子(0,0,0,0,0)到所求结点的最短路径问题。<br /><br />代码如下:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">shopping.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">shopping.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> dp[</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> offer{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_num;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offer_num;<br />offer offers[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> map[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">]; //product id到”按物品出现顺序所给的编号“的映射<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">];//product id对应的物品价格<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product_num;//物品总数目<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> products[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];//存放product id<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];//product 所需的数量<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offer_num;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].pro_num;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[i].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].product[j]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].amount[j];<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].price;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_idx </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">product_num;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">product_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">products[i];<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">amount[i];<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">price[i];<br />        map[ products[i] ] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />    }<br /><br />    //没有折扣时的价格<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j)<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />                </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> m</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;m</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">m)<br />                    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;n</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">n){<br />                        dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> <br />                            price[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">k</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">n;<br />                    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j)<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />                </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> m</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;m</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">m)<br />                    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;n</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">n){<br />                        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />                        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> s</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;s</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">s){<br /><br />                            memset(tmp,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(tmp));    <br />                            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;t</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[s].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">t){<br />                                tmp[map[offers[s].product[t]]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> offers[s].amount[t];<br />                            }<br /><br />                            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">k</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]){<br />                                dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min( dp[i][j][k][m][n],<br />                                        dp[i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][k</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][m</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][n</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        offers[s].price);<br />                            }<br />                        }<br />                    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">dp[amount[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br />上面代码有个严重的bug，谢谢网友“我也凑热闹"指出。由于map所有值都为0。所以未在商品列表中出现的商品的map值都为0，即都映射为第一个商品。现改成将map初始化为-1，并增加判断语句。此外将初始化dp的语句合并到后面，以简化代码。<br /><br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">shopping.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">shopping.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> dp[</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">struct</span><span style="color: rgb(0, 0, 0);"> offer{<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_num;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price;<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />};<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> offer_num;<br />offer offers[</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> map[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> price[</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> product_num;<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> products[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> amount[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offer_num;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].pro_num;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[i].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].product[j]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].amount[j];<br />        }<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">offers[i].price;<br />    }<br />    <br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> pro_idx </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">product_num;<br /><br />    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">2009.07.27修改</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    memset(map,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(map));<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">product_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">products[i];<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">amount[i];<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">price[i];<br />        map[ products[i] ] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> i;<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j)<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />                </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> m</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;m</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">m)<br />                    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;n</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">n){<br /><br />                         dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> <br />                            price[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">k</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                            price[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">n;<br /><br />                        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> tmp[</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">];<br />                        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> s</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;s</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offer_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">s){<br />                            memset(tmp,</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(tmp));    <br />                            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> t</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;t</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">offers[s].pro_num;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">t){<br />                                </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">2009.07.27修改</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">                                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( map[offers[s].product[t]]</span><span style="color: rgb(0, 0, 0);">!=-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />                                    tmp[map[offers[s].product[t]]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> offers[s].amount[t];<br />                            }<br /><br />                            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">k</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">m</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">n</span><span style="color: rgb(0, 0, 0);">&gt;=</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]){<br />                                dp[i][j][k][m][n] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min( dp[i][j][k][m][n],<br />                                        dp[i</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][j</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][k</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][m</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][n</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">tmp[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                                        offers[s].price);<br />                            }<br />                        }<br />                    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">dp[amount[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]][amount[</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">]]</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /><br /></span></div><br /><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/89483.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-07 19:16 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/07/89483.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.3 Riding The Fences </title><link>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Tue, 07 Jul 2009 08:59:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89469.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89469.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89469.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 欧拉路径&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89469.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-07 16:59 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/07/89469.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.2 Sweet Butter</title><link>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 06 Jul 2009 12:05:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/89393.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/89393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/89393.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 稀疏图的多源最短路径问题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/89393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-07-06 20:05 <a href="http://www.cppblog.com/yuziyu/archive/2009/07/06/89393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 3.1 Agri-Net</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Mon, 29 Jun 2009 13:48:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88836.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88836.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88836.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最小生成树问题<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88836.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-29 21:48 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/29/88836.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Bessie Come Home</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sun, 28 Jun 2009 09:25:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88701.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88701.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88701.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最短路径问题&nbsp;&nbsp;<a href='http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html'>阅读全文</a><img src ="http://www.cppblog.com/yuziyu/aggbug/88701.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-28 17:25 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/28/88701.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>USACO 2.4 Cow Tours</title><link>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html</link><dc:creator>YZY</dc:creator><author>YZY</author><pubDate>Sat, 27 Jun 2009 14:10:00 GMT</pubDate><guid>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html</guid><wfw:comment>http://www.cppblog.com/yuziyu/comments/88662.html</wfw:comment><comments>http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuziyu/comments/commentRss/88662.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuziyu/services/trackbacks/88662.html</trackback:ping><description><![CDATA[
		<br />这题综合了dfs,floyd算法。<br /><br />首先用 floyd计算出两两之间的最短路径。然后用dfs将图着色。对每两两不同颜色的结点，我们把它们相连，然后看产生的图的直径的大小。<br />直径的大小只可能仍为原两连通图直径之一，或者是包括新添加的路径所产生的最长路径，取这三者中的最大值。新路径产生的最长路径只可能是两点的距离加上两点到其他点的最长距离。最开始的时候只考虑了新添加的路径，没考虑原直径会比新路径大的情况，wa了n次。。<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">iostream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">fstream</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cfloat</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cmath</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">using</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">namespace</span><span style="color: rgb(0, 0, 0);"> std;<br /><br />ifstream fin(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">cowtour.in</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />ofstream fout(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">cowtour.out</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /><br />#ifdef _DEBUG<br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out cout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in cin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#else</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> out fout</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> in fin</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">#endif</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> path[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> graph[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">][</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> y[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> n;<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> color[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> diameter[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /></span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> maxPath[</span><span style="color: rgb(0, 0, 0);">150</span><span style="color: rgb(0, 0, 0);">];<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> floyd()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;k</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">k)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">j){<br />                    path[i][j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">min(path[i][j],path[i][k]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">path[k][j]);<br />                }<br />            }<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> make_color(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( color[i]</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);"> )<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">;<br /><br />    color[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> c;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">graph[i][j])<br />            make_color(j,c);<br />    }<br />}<br /><br /><br /></span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> solve()<br />{<br />    </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">n;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">x[i]</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">y[i];<br />    }<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> s;<br />        </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">&gt;&gt;</span><span style="color: rgb(0, 0, 0);">s;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            graph[i][j]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">s[j]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(graph[i][j]){<br />                path[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])<br />                    </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j]) );<br />            }</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">{<br />                path[i][j] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DBL_MAX</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">;<br />            }<br />        }<br />    }<br /><br />    floyd();<br /><br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">)<br />            make_color(i,</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">c);<br /><br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i){<br />        maxPath[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">j</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[j]){<br />                maxPath[i] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(maxPath[i],path[i][j]);<br />            }<br />        }<br /><br />        diameter[color[i]] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(diameter[color[i]],maxPath[i]);<br />    }<br /><br /><br />    </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> dia </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DBL_MAX;<br /><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">i)<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;j</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">j){<br /><br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[j]) </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);">;<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> d1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;p</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">p){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(i</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">p</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[i]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[p])<br />                    d1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(d1,path[i][p]);<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> d2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;p</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">n;</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">p){<br />                </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(j</span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);">p</span><span style="color: rgb(0, 0, 0);">&amp;&amp;</span><span style="color: rgb(0, 0, 0);">color[j]</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">color[p])<br />                    d2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> max(d2,path[j][p]);<br />            }<br /><br />            </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> dist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqrt( (x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(x[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">x[j])</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"><br />                    (y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j])</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">(y[i]</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">y[j]) );<br /><br />            dia </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> min(dia,max(d1</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">d2</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">dist,max(diameter[color[i]],diameter[color[j]])) );<br />        }<br /><br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">.setf(ios::</span><span style="color: rgb(0, 0, 255);">fixed</span><span style="color: rgb(0, 0, 0);">,ios::floatfield);<br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">.precision(</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">);<br />    </span><span style="color: rgb(0, 0, 255);">out</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">dia</span><span style="color: rgb(0, 0, 0);">&lt;&lt;</span><span style="color: rgb(0, 0, 0);">endl;<br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> argc,</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">argv[])<br />{<br />    solve(); <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /><br /></span></div><br /><img src ="http://www.cppblog.com/yuziyu/aggbug/88662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuziyu/" target="_blank">YZY</a> 2009-06-27 22:10 <a href="http://www.cppblog.com/yuziyu/archive/2009/06/27/88662.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>