﻿<?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++博客-powerwater-文章分类-算法</title><link>http://www.cppblog.com/powerwater/category/18012.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 03 Feb 2019 05:14:12 GMT</lastBuildDate><pubDate>Sun, 03 Feb 2019 05:14:12 GMT</pubDate><ttl>60</ttl><item><title>splay</title><link>http://www.cppblog.com/powerwater/articles/212472.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Sat, 12 Dec 2015 15:46:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/212472.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#ifndef&nbsp;_SPLAY_TREE_HPP_#define&nbsp;_SPLAY_TREE_HPP_#include&nbsp;&lt;iomanip&gt;#include&nbsp;&...&nbsp;&nbsp;<a href='http://www.cppblog.com/powerwater/articles/212472.html'>阅读全文</a><img src ="http://www.cppblog.com/powerwater/aggbug/212472.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2015-12-12 23:46 <a href="http://www.cppblog.com/powerwater/articles/212472.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>堆优化的迪杰斯特拉</title><link>http://www.cppblog.com/powerwater/articles/211714.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Sat, 29 Aug 2015 07:59:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/211714.html</guid><description><![CDATA[简洁点的：
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img id="Code_Closed_Image_235744" onclick="this.style.display='none'; Code_Closed_Text_235744.style.display='none'; Code_Open_Image_235744.style.display='inline'; Code_Open_Text_235744.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" height="16"><img style="display: none" id="Code_Open_Image_235744" onclick="this.style.display='none'; Code_Open_Text_235744.style.display='none'; Code_Closed_Image_235744.style.display='inline'; Code_Closed_Text_235744.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Code_Closed_Text_235744"></span><span style="display: none" id="Code_Open_Text_235744"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;N&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10005</span><span style="color: #000000">,&nbsp;M&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">200005</span><span style="color: #000000">,&nbsp;oo&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0x3fffffff</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;DistT{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,w;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">operator</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;DistT&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">x)</span><span style="color: #0000ff">const</span><span style="color: #000000">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;w</span><span style="color: #000000">&gt;</span><span style="color: #000000">x.w</span><span style="color: #000000">||</span><span style="color: #000000">w</span><span style="color: #000000">==</span><span style="color: #000000">x.w</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">v</span><span style="color: #000000">&gt;</span><span style="color: #000000">x.v;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />};<br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;EdgeT{&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to,w,next;&nbsp;};<br />EdgeT&nbsp;edge[M];<br />DistT&nbsp;dist[M];<br /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;ok[N];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,m,en</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,head[N],d[N];<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;insert(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c){<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[en].to&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;b;<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[en].w&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;c;<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[en].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head[a];<br />&nbsp;&nbsp;&nbsp;&nbsp;head[a]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;en</span><span style="color: #000000">++</span><span style="color: #000000">;<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;init(){&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;fill(head,head</span><span style="color: #000000">+</span><span style="color: #000000">N,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;fill(d,d</span><span style="color: #000000">+</span><span style="color: #000000">N,oo);<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">m;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,a,b,c;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">m;i</span><span style="color: #000000">++</span><span style="color: #000000">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">a</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">b</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">c;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(a,b,c);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />}<br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;dijkstra(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;source){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">head[source];j</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">=</span><span style="color: #000000">edge[j].next)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[k].v</span><span style="color: #000000">=</span><span style="color: #000000">edge[j].to,dist[k</span><span style="color: #000000">++</span><span style="color: #000000">].w&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;edge[j].w,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[edge[j].to]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;edge[j].w;<br />&nbsp;&nbsp;&nbsp;&nbsp;ok[source]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;&nbsp;d[source]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;make_heap(dist,dist</span><span style="color: #000000">+</span><span style="color: #000000">k);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">d[dist[0].v]=dist[0].w;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;p</span><span style="color: #000000">=</span><span style="color: #000000">dist[</span><span style="color: #000000">0</span><span style="color: #000000">].v;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop_heap(dist,dist</span><span style="color: #000000">+</span><span style="color: #000000">k);&nbsp;k</span><span style="color: #000000">--</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(ok[p])&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000">--</span><span style="color: #000000">;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ok[p]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">head[p];j</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">=</span><span style="color: #000000">edge[j].next)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">ok[edge[j].to]</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">d[p]</span><span style="color: #000000">+</span><span style="color: #000000">edge[j].w</span><span style="color: #000000">&lt;</span><span style="color: #000000">d[edge[j].to]){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[edge[j].to]</span><span style="color: #000000">=</span><span style="color: #000000">d[p]</span><span style="color: #000000">+</span><span style="color: #000000">edge[j].w;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[k].v</span><span style="color: #000000">=</span><span style="color: #000000">edge[j].to;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[k].w</span><span style="color: #000000">=</span><span style="color: #000000">d[edge[j].to];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">k;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push_heap(dist,dist</span><span style="color: #000000">+</span><span style="color: #000000">k);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />}<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;init();<br />&nbsp;&nbsp;&nbsp;&nbsp;dijkstra(</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">d[i]</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}<br /><br /></span><span style="color: #008000">/*</span><span style="color: #008000">测试数据<br /><br />5&nbsp;7<br />1&nbsp;2&nbsp;10<br />1&nbsp;4&nbsp;30<br />2&nbsp;3&nbsp;50<br />4&nbsp;3&nbsp;20<br />4&nbsp;5&nbsp;60<br />1&nbsp;5&nbsp;100<br />3&nbsp;5&nbsp;10<br /><br />输出：<br />0&nbsp;10&nbsp;50&nbsp;30&nbsp;60<br /></span><span style="color: #008000">*/</span><span style="color: #000000">&nbsp;</span></span></div><br /><br /><br />代码： 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img id="Code_Closed_Image_181946" onclick="this.style.display='none'; Code_Closed_Text_181946.style.display='none'; Code_Open_Image_181946.style.display='inline'; Code_Open_Text_181946.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" height="16"><img style="display: none" id="Code_Open_Image_181946" onclick="this.style.display='none'; Code_Open_Text_181946.style.display='none'; Code_Closed_Image_181946.style.display='inline'; Code_Closed_Text_181946.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Code_Closed_Text_181946"></span><span style="display: none" id="Code_Open_Text_181946"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000">//</span><span style="color: #008000">堆优化的dijsktra&nbsp;单源点最短路径&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;N&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100005</span><span style="color: #000000">,&nbsp;M&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">200005</span><span style="color: #000000">,&nbsp;oo</span><span style="color: #000000">=</span><span style="color: #000000">0x7fffffff</span><span style="color: #000000">;<br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;EdgeT{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;to,wei;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;next;<br />}edge[M];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,e</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,head[N],dist[N],a[N],b[N];<br /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;ok[N]</span><span style="color: #000000">=</span><span style="color: #000000">{</span><span style="color: #0000ff">false</span><span style="color: #000000">};<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;AddEdge(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;y,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;z){<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[e].to</span><span style="color: #000000">=</span><span style="color: #000000">y;<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[e].wei</span><span style="color: #000000">=</span><span style="color: #000000">z;<br />&nbsp;&nbsp;&nbsp;&nbsp;edge[e].next</span><span style="color: #000000">=</span><span style="color: #000000">head[x];<br />&nbsp;&nbsp;&nbsp;&nbsp;head[x]</span><span style="color: #000000">=</span><span style="color: #000000">e</span><span style="color: #000000">++</span><span style="color: #000000">;<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;init(){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;en;<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">en;<br />&nbsp;&nbsp;&nbsp;&nbsp;fill(head,head</span><span style="color: #000000">+</span><span style="color: #000000">N,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,a,b,c;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">en;i</span><span style="color: #000000">++</span><span style="color: #000000">){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">a</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">b</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">c;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AddEdge(a,b,c);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;jiaohuan(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">x,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">y){<br />&nbsp;&nbsp;&nbsp;&nbsp;swap(x,y);<br />&nbsp;&nbsp;&nbsp;&nbsp;swap(b[x],b[y]);<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;siftdown(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;root,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;child</span><span style="color: #000000">=</span><span style="color: #000000">root</span><span style="color: #000000">*</span><span style="color: #000000">2</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(child</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(child</span><span style="color: #000000">&lt;</span><span style="color: #000000">m</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">dist[a[child</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">]]</span><span style="color: #000000">&lt;</span><span style="color: #000000">dist[a[child]])&nbsp;child</span><span style="color: #000000">++</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(dist[a[child]]</span><span style="color: #000000">&lt;</span><span style="color: #000000">dist[a[root]]){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jiaohuan(a[child],a[root]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root</span><span style="color: #000000">=</span><span style="color: #000000">child;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child</span><span style="color: #000000">=</span><span style="color: #000000">root</span><span style="color: #000000">*</span><span style="color: #000000">2</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;siftup(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;child){<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;parent</span><span style="color: #000000">=</span><span style="color: #000000">(child</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(child){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(dist[a[child]]</span><span style="color: #000000">&lt;</span><span style="color: #000000">dist[a[parent]]){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jiaohuan(a[child],a[parent]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;child</span><span style="color: #000000">=</span><span style="color: #000000">parent;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent</span><span style="color: #000000">=</span><span style="color: #000000">(child</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;shortestpath(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;source){<br />&nbsp;&nbsp;&nbsp;&nbsp;fill(dist</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,dist</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,oo);<br />&nbsp;&nbsp;&nbsp;&nbsp;dist[source]</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">head[source];i</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">=</span><span style="color: #000000">edge[i].next)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[edge[i].to]</span><span style="color: #000000">=</span><span style="color: #000000">edge[i].wei;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">用源点到各顶点直达距离初始化dist数组&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;ok[source]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;a[i]</span><span style="color: #000000">=</span><span style="color: #000000">i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;swap(a[source</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">],a[n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">(n</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;siftdown(i,n</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">);&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">make_heap(a,a+n-1,comp);</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;b[a[i]]</span><span style="color: #000000">=</span><span style="color: #000000">i;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">;i</span><span style="color: #000000">&gt;=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">--</span><span style="color: #000000">){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k</span><span style="color: #000000">=</span><span style="color: #000000">a[</span><span style="color: #000000">0</span><span style="color: #000000">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ok[k]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">到顶点k的最短路径已求出，置标记&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jiaohuan(a[</span><span style="color: #000000">0</span><span style="color: #000000">],a[i]);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;siftdown(</span><span style="color: #000000">0</span><span style="color: #000000">,i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">head[k];j</span><span style="color: #000000">!=-</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">=</span><span style="color: #000000">edge[j].next){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">ok[j]</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">dist[k]</span><span style="color: #000000">+</span><span style="color: #000000">edge[j].wei</span><span style="color: #000000">&lt;</span><span style="color: #000000">dist[edge[j].to]){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[edge[j].to]</span><span style="color: #000000">=</span><span style="color: #000000">dist[k]</span><span style="color: #000000">+</span><span style="color: #000000">edge[j].wei;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">松弛操作&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;siftup(b[edge[j].to]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(){<br />&nbsp;&nbsp;&nbsp;&nbsp;init();&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;shortestpath(</span><span style="color: #000000">1</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">dist[i]</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></span></div>参考数据：<br />输入：<br />6 11<br />1 2 5<br />1 3 30<br />1 4 18<br />2 3 12<br />2 4 10<br />3 5 25<br />4 1 15<br />4 5 8<br />5 2 5<br />5 3 20<br />5 6 4<br />输出：<br />0 5 17 15 23 27<br /><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/powerwater/shortestpath.png" width="500" longdesc="" height="309" /><br />样例如图<br />  <img src ="http://www.cppblog.com/powerwater/aggbug/211714.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2015-08-29 15:59 <a href="http://www.cppblog.com/powerwater/articles/211714.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> Floyd算法_任意两点间最短距离</title><link>http://www.cppblog.com/powerwater/articles/207763.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Wed, 23 Jul 2014 14:05:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/207763.html</guid><description><![CDATA[#include<iostream>
using namespace std;
const int N = 1000, oo = 0x3fffffff;
int n,dist[N][N];
void init(){
	int e;
	cin>>n>>e;
	fill(dist[0],dist[0]+N*N,oo);
	for (int i=0;i<n;i++) dist[i][i]=0;
	for (int i=0,a,b,c;i<e;i++){
		cin>>a>>b>>c;
		dist[a][b]=c;
	}		
}
void floyd(){
	for (int k=0;k<n;k++)
		for (int i=0;i<n;i++)
			for (int j=0;j<n;j++)
				dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
void output(){
	for (int i=0;i<n;i++){
		for (int j=0;j<n;j++)
			if (dist[i][j]==oo) printf("%5s","oo");
			else printf("%5d",dist[i][j]);
		printf("\n");
	}
}
int main(){
	init();
	floyd();
	output();	
	return 0;	
}
测试数据：
5 7
0 1 10
0 3 30
0 4 100
1 2 50
2 4 10
3 2 20
3 4 60
结果：
    0   10   50   30   60
   oo    0   50   oo   60
   oo   oo    0   oo   10
   oo   oo   20    0   30
   oo   oo   oo   oo    0
<img src ="http://www.cppblog.com/powerwater/aggbug/207763.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2014-07-23 22:05 <a href="http://www.cppblog.com/powerwater/articles/207763.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>高精度运算</title><link>http://www.cppblog.com/powerwater/articles/197705.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Mon, 04 Feb 2013 03:31:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/197705.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/197705.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/197705.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/197705.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/197705.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->高精度运算：Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-...&nbsp;&nbsp;<a href='http://www.cppblog.com/powerwater/articles/197705.html'>阅读全文</a><img src ="http://www.cppblog.com/powerwater/aggbug/197705.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2013-02-04 11:31 <a href="http://www.cppblog.com/powerwater/articles/197705.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1167: 【提高】跳马</title><link>http://www.cppblog.com/powerwater/articles/187455.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Fri, 17 Aug 2012 02:12:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/187455.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/187455.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/187455.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/187455.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/187455.html</trackback:ping><description><![CDATA[<div id="main">
<center>
<h2><a href="http://oj.jzxx.net/problem.php?id=1167">
<h2>1167: 【提高】跳马</h2></a><span class="green">时间限制: </span>1 Sec&nbsp;&nbsp;<span class="green">内存限制: </span>16 MB<br /><span class="green">提交: </span>613&nbsp;&nbsp;<span class="green">解决: </span>218<br />[<a href="http://oj.jzxx.net/submitpage.php?id=1167">提交</a>][<a href="http://oj.jzxx.net/problemstatus.php?id=1167">状态</a>][<a href="http://oj.jzxx.net/bbs.php?pid=1167">讨论版</a>]</h2></center>
<h2>题目描述</h2>
<div class="content">
<p>国际象棋中马走&#8220;日&#8221;，现请编程输出马从入口跳到出口的一条路径。棋盘上有些点是不能落脚的。如图A中值为1的点不能落脚，值为0的点可落脚。</p>
<p>&nbsp;<img alt="" src="http://oj.jzxx.net/upload/1167.jpg" /></p>
<p>马在格子中，有8个方向可以探索。探索顺序按图B中的序号顺序进行。</p></div>
<h2>输入</h2>
<div class="content">
<p>第一行两个整数m,n 表示棋盘有m行n列（m＜＝１０,n＜＝１０） 下面m行每行n个数，全是0或1 ，1表示不能落脚。</p></div>
<h2>输出</h2>
<div class="content">
<p>一条路径用坐标点表示中间用&#8220;-&#8221;隔开。</p></div>
<h2>样例输入</h2>
<div class="content"><span class="sampledata"><pre>6  6
0 1 1 1 1 1
1 1 1 0 1 1
1 0 1 1 1 0
1 1 1 0 0 1
1 1 0 1 1 1
1 1 1 1 0 0</pre></span></div>
<h2>样例输出</h2>
<div class="content"><span class="sampledata"><pre>(1,1)-(3,2)-(5,3)-(6,5)-(4,4)-(3,6)-(2,4)-(4,5)-(6,6)
(1,1)-(3,2)-(5,3)-(4,5)-(6,6)
(1,1)-(3,2)-(4,4)-(6,5)-(5,3)-(4,5)-(6,6)
(1,1)-(3,2)-(4,4)-(3,6)-(2,4)-(4,5)-(6,6)
(1,1)-(3,2)-(2,4)-(4,5)-(6,6)
(1,1)-(3,2)-(2,4)-(3,6)-(4,4)-(6,5)-(5,3)-(4,5)-(6,6)</pre></span></div>
<h2>提示</h2>
<div class="content">
<p>&nbsp;</p>
<p>注意： 当不止一条路径时，马选择探索方向的顺序直接影响着输出的路径。 本题规定了马探索方向的顺序。</p>
<p>&nbsp;</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img id="Code_Closed_Image_101053" onclick="this.style.display='none'; Code_Closed_Text_101053.style.display='none'; Code_Open_Image_101053.style.display='inline'; Code_Open_Text_101053.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" height="16"><img style="display: none" id="Code_Open_Image_101053" onclick="this.style.display='none'; Code_Open_Text_101053.style.display='none'; Code_Closed_Image_101053.style.display='inline'; Code_Closed_Text_101053.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Code_Closed_Text_101053">code</span><span style="display: none" id="Code_Open_Text_101053"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img id="Codehighlighter1_56_78_Open_Image" onclick="this.style.display='none'; Codehighlighter1_56_78_Open_Text.style.display='none'; Codehighlighter1_56_78_Closed_Image.style.display='inline'; Codehighlighter1_56_78_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_56_78_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_56_78_Closed_Text.style.display='none'; Codehighlighter1_56_78_Open_Image.style.display='inline'; Codehighlighter1_56_78_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dx[</span><span style="color: #000000">8</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_56_78_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_56_78_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /><img id="Codehighlighter1_97_119_Open_Image" onclick="this.style.display='none'; Codehighlighter1_97_119_Open_Text.style.display='none'; Codehighlighter1_97_119_Closed_Image.style.display='inline'; Codehighlighter1_97_119_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_97_119_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_97_119_Closed_Text.style.display='none'; Codehighlighter1_97_119_Open_Image.style.display='inline'; Codehighlighter1_97_119_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;dy[</span><span style="color: #000000">8</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_97_119_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_97_119_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;node<br /><img id="Codehighlighter1_134_146_Open_Image" onclick="this.style.display='none'; Codehighlighter1_134_146_Open_Text.style.display='none'; Codehighlighter1_134_146_Closed_Image.style.display='inline'; Codehighlighter1_134_146_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_134_146_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_134_146_Closed_Text.style.display='none'; Codehighlighter1_134_146_Open_Image.style.display='inline'; Codehighlighter1_134_146_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_134_146_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_134_146_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x,y;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m,n,map[</span><span style="color: #000000">15</span><span style="color: #000000">][</span><span style="color: #000000">15</span><span style="color: #000000">];<br /><img id="Codehighlighter1_191_193_Open_Image" onclick="this.style.display='none'; Codehighlighter1_191_193_Open_Text.style.display='none'; Codehighlighter1_191_193_Closed_Image.style.display='inline'; Codehighlighter1_191_193_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_191_193_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_191_193_Closed_Text.style.display='none'; Codehighlighter1_191_193_Open_Image.style.display='inline'; Codehighlighter1_191_193_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;visited[</span><span style="color: #000000">15</span><span style="color: #000000">][</span><span style="color: #000000">15</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_191_193_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_191_193_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">0</span><span style="color: #000000">}</span></span><span style="color: #000000">,found(</span><span style="color: #0000ff">false</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />node&nbsp;s[</span><span style="color: #000000">120</span><span style="color: #000000">];&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;t(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">ifstream&nbsp;cin("p1167.in");<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;&nbsp;&nbsp;ofstream&nbsp;cout("p1167.out");</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;search(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x0,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;y0)<br /><img id="Codehighlighter1_320_756_Open_Image" onclick="this.style.display='none'; Codehighlighter1_320_756_Open_Text.style.display='none'; Codehighlighter1_320_756_Closed_Image.style.display='inline'; Codehighlighter1_320_756_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_320_756_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_320_756_Closed_Text.style.display='none'; Codehighlighter1_320_756_Open_Image.style.display='inline'; Codehighlighter1_320_756_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_320_756_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_320_756_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;p;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(x0</span><span style="color: #000000">==</span><span style="color: #000000">m</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">y0</span><span style="color: #000000">==</span><span style="color: #000000">n)<br /><img id="Codehighlighter1_353_499_Open_Image" onclick="this.style.display='none'; Codehighlighter1_353_499_Open_Text.style.display='none'; Codehighlighter1_353_499_Closed_Image.style.display='inline'; Codehighlighter1_353_499_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_353_499_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_353_499_Closed_Text.style.display='none'; Codehighlighter1_353_499_Open_Image.style.display='inline'; Codehighlighter1_353_499_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_353_499_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_353_499_Open_Text"><span style="color: #000000">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">'</span><span style="color: #000000">(</span><span style="color: #000000">'</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">s[</span><span style="color: #000000">0</span><span style="color: #000000">].x</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000">'</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">s[</span><span style="color: #000000">0</span><span style="color: #000000">].y</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">'</span><span style="color: #000000">)</span><span style="color: #000000">'</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">t;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">-(</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">s[i].x</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000">'</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">s[i].y</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">'</span><span style="color: #000000">)</span><span style="color: #000000">'</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">8</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_525_753_Open_Image" onclick="this.style.display='none'; Codehighlighter1_525_753_Open_Text.style.display='none'; Codehighlighter1_525_753_Closed_Image.style.display='inline'; Codehighlighter1_525_753_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_525_753_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_525_753_Closed_Text.style.display='none'; Codehighlighter1_525_753_Open_Image.style.display='inline'; Codehighlighter1_525_753_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_525_753_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_525_753_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.x</span><span style="color: #000000">=</span><span style="color: #000000">x0</span><span style="color: #000000">+</span><span style="color: #000000">dx[i];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.y</span><span style="color: #000000">=</span><span style="color: #000000">y0</span><span style="color: #000000">+</span><span style="color: #000000">dy[i];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(p.x</span><span style="color: #000000">&lt;=</span><span style="color: #000000">0</span><span style="color: #000000">||</span><span style="color: #000000">p.x</span><span style="color: #000000">&gt;</span><span style="color: #000000">m</span><span style="color: #000000">||</span><span style="color: #000000">p.y</span><span style="color: #000000">&lt;=</span><span style="color: #000000">0</span><span style="color: #000000">||</span><span style="color: #000000">p.y</span><span style="color: #000000">&gt;</span><span style="color: #000000">n)&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(visited[p.x][p.y]</span><span style="color: #000000">||</span><span style="color: #000000">map[p.x][p.y]</span><span style="color: #000000">==</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s[t</span><span style="color: #000000">++</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">p;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visited[p.x][p.y]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search(p.x,p.y);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">--</span><span style="color: #000000">t;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;visited[p.x][p.y]</span><span style="color: #000000">=</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_769_1025_Open_Image" onclick="this.style.display='none'; Codehighlighter1_769_1025_Open_Text.style.display='none'; Codehighlighter1_769_1025_Closed_Image.style.display='inline'; Codehighlighter1_769_1025_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_769_1025_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_769_1025_Closed_Text.style.display='none'; Codehighlighter1_769_1025_Open_Image.style.display='inline'; Codehighlighter1_769_1025_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_769_1025_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_769_1025_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i,j;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">m</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map[</span><span style="color: #000000">0</span><span style="color: #000000">][j]</span><span style="color: #000000">=</span><span style="color: #000000">map[n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">][j]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map[i][</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">map[i][n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">m;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">map[i][j];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;s[</span><span style="color: #000000">0</span><span style="color: #000000">].x</span><span style="color: #000000">=</span><span style="color: #000000">s[</span><span style="color: #000000">0</span><span style="color: #000000">].y</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;t</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;visited[</span><span style="color: #000000">1</span><span style="color: #000000">][</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;search(</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">1</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></span></div>
<p><br /><br /></p></div></div><img src ="http://www.cppblog.com/powerwater/aggbug/187455.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2012-08-17 10:12 <a href="http://www.cppblog.com/powerwater/articles/187455.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1154【基础】N皇后问题</title><link>http://www.cppblog.com/powerwater/articles/187426.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Thu, 16 Aug 2012 15:13:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/187426.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/187426.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/187426.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/187426.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/187426.html</trackback:ping><description><![CDATA[<div><center><h2>1154: 【基础】<a href="http://oj.jzxx.net/problem.php?id=1154">N皇后问题</a></h2>时间限制: 1 Sec&nbsp;&nbsp;内存限制: 16 MB<br />提交: 787&nbsp;&nbsp;解决: 309<br />[<a href="http://oj.jzxx.net/submitpage.php?id=1154">提交</a>][<a href="http://oj.jzxx.net/problemstatus.php?id=1154">状态</a>][<a href="http://oj.jzxx.net/bbs.php?pid=1154">讨论版</a>]</center><h2>题目描述</h2><div><p>在一个N*N的棋盘上放置N个皇后，且使得每两个之间不能互相攻击，也就是使得每两个不在同一行，同一列和同一斜角线上。以下为N=8时问题的一个解：</p> <p>&nbsp;<img alt="" src="http://oj.jzxx.net/upload/1154.jpg" /></p></div><h2>输入</h2><div><p>一个整数N（4&lt;=N&lt;=10)</p></div><h2>输出</h2><div><p>N皇后的所有解。</p></div><h2>样例输入</h2> 			<div>4</div><h2>样例输出</h2> 			<div><span> <div>&nbsp;4&nbsp;&nbsp; Q&nbsp;&nbsp; &nbsp;<br />&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Q<br />&nbsp;2 Q&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp; Q &nbsp;<br />&nbsp;&nbsp; a b c d<br /><br /><br />&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp; Q &nbsp;<br />&nbsp;3 Q&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br />&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Q<br />&nbsp;1&nbsp;&nbsp; Q&nbsp;&nbsp; &nbsp;<br />&nbsp;&nbsp; a b c d</div></span></div><h2>提示</h2> 			<p>输出格式说明： a的场宽为4，其它数字和字母的场宽均为2，两种解法之间空一行。</p></div><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_231053" onclick="this.style.display='none'; Code_Closed_Text_231053.style.display='none'; Code_Open_Image_231053.style.display='inline'; Code_Open_Text_231053.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" height="16" align="top" width="11"><img id="Code_Open_Image_231053" style="display: none" onclick="this.style.display='none'; Code_Open_Text_231053.style.display='none'; Code_Closed_Image_231053.style.display='inline'; Code_Closed_Text_231053.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" height="16" align="top" width="11"><span id="Code_Closed_Text_231053" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">code</span><span id="Code_Open_Text_231053" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">#include</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">iostream</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">iomanip</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">using</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; ">&nbsp;std;<br /></span><span style="color: #0000FF; ">const</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;N(</span><span style="color: #000000; ">30</span><span style="color: #000000; ">);<br /></span><span style="color: #0000FF; ">bool</span><span style="color: #000000; ">&nbsp;a[N]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">0</span><span style="color: #000000; ">},b[N]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">0</span><span style="color: #000000; ">},c[N]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">0</span><span style="color: #000000; ">},f[N][N]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">{</span><span style="color: #000000; ">0</span><span style="color: #000000; ">};<br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;n,t(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;search(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(i</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">n)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(t</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">1</span><span style="color: #000000; ">)&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;i</span><span style="color: #000000; ">=</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;i</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">n;i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">setw(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">n</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">-</span><span style="color: #000000; ">i;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;j</span><span style="color: #000000; ">=</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;j</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">n;j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(f[i][j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">setw(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">Q</span><span style="color: #000000; ">'</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">else</span><span style="color: #000000; ">&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">setw(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">setw(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;ch</span><span style="color: #000000; ">=</span><span style="color: #000000; ">'</span><span style="color: #000000; ">a</span><span style="color: #000000; ">'</span><span style="color: #000000; ">;ch</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">'</span><span style="color: #000000; ">a</span><span style="color: #000000; ">'</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n;ch</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">setw(</span><span style="color: #000000; ">2</span><span style="color: #000000; ">)</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;j</span><span style="color: #000000; ">=</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;j</span><span style="color: #000000; ">&lt;=</span><span style="color: #000000; ">n;j</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(</span><span style="color: #000000; ">!</span><span style="color: #000000; ">a[j]</span><span style="color: #000000; ">&amp;&amp;!</span><span style="color: #000000; ">b[i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">j]</span><span style="color: #000000; ">&amp;&amp;!</span><span style="color: #000000; ">c[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">b[i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">j]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">c[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">f[i][j]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">true</span><span style="color: #000000; ">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search(i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">b[i</span><span style="color: #000000; ">+</span><span style="color: #000000; ">j]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">c[i</span><span style="color: #000000; ">-</span><span style="color: #000000; ">j</span><span style="color: #000000; ">+</span><span style="color: #000000; ">n]</span><span style="color: #000000; ">=</span><span style="color: #000000; ">f[i][j]</span><span style="color: #000000; ">=</span><span style="color: #0000FF; ">false</span><span style="color: #000000; ">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main()<br />{&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000; ">&gt;&gt;</span><span style="color: #000000; ">n;<br />&nbsp;&nbsp;&nbsp;&nbsp;search(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">cin.get();cin.get();</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />}<br /></span></span></div><img src ="http://www.cppblog.com/powerwater/aggbug/187426.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2012-08-16 23:13 <a href="http://www.cppblog.com/powerwater/articles/187426.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>斜率优化+单调队列优化DP</title><link>http://www.cppblog.com/powerwater/articles/173992.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Tue, 08 May 2012 07:42:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/173992.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/173992.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/173992.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/173992.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/173992.html</trackback:ping><description><![CDATA[<div>【题目大意】</div>
<div>有N个数，现要将它们分成连续的若干段，每段的代价为(&#8721;Ci)^2+M，求最小的代价。</div>
<div>【题目分析】</div>
<div>容易得到这样的一个动态规划算法：</div>
<div>令dp[i]表示前i个数分成若干段的最小代价，能得到一个经典的动态转移方程：&nbsp;</div>
<div><span style="background-color: #ffffff; color: #ed1c24">dp[i]= &nbsp;Min &nbsp;(dp[j] + Cost(j+1,i))+M，0&lt;=j&lt;i。&nbsp;</span></div>
<div>其中Cost(a,b)表示把a..b这些数分成一段的费用。</div>
<div>这个算法的时间复杂度是O(N^2)。 这题N有500000，必须优化。</div>
<div>我们看到Cost(j+1,i)。如果预处理出1..i的和，记为s[i]，那么Cost可以表示为(s[i]-s[j])^2。</div>
<div>将它展开并且代入转移方程中可以得到：<span style="color: #ed1c24">dp[i]= Min (dp[j] + s[i]^2 -2s[i]*s[j] +s[j]^2)+M</span>。</div>
<div>因为s[i]^2是只和i有关的，可以移到Min的外面，得到</div>
<div><span style="color: #ed1c24">dp[i]= Min (dp[j] -2*s[i]*s[j] +s[j]^2) + M + s[i]^2</span>。</div>
<div>现在我们单独考虑Min()里面的内容。</div>
<div>如果我们设<span style="color: #ed1c24">k=2*s[i],x=s[j],y=dp[j]+s[j]^2</span>的话（注意将和i有关的量以及和j有关的量结合到一起）</div>
<div>再令G=dp[i]，那么将得到：</div>
<div><span style="color: #ed1c24">G=-k*x+y</span>。</div>
<div>移项得：</div>
<div><span style="color: #ed1c24">y=k*x+G。</span></div>
<div>看到这个式子，知道斜率优化的朋友显然可以做出来了。</div>
<div>这里我讲讲何谓斜率优化。</div>
<div>得到这个式子之后，我们可以看到，k是一个常数（由当前枚举的i在O(1)时间内计算得出）。将这个式子看成是一个直线的函数表达式的话，k就是斜率，也就是说这是一个斜率固定的直线。</div>
<div>y和x则是和j有关的常量。而j的这些值应该都已经在之前计算过了。（因为j&lt;i）</div>
<div>这个式子中，G是未知的，G和y以及x有关。</div>
<div>如果我们把每个j对应的x和y值看成一个坐标系中的点的话。</div>
<div>那么当我们枚举到i时，坐标系中就有一系列的点。</div>
<div>对于每个点，做一条斜率为k的直线，就能得到一个G的值。G的值为这条直线与y轴交点的纵坐标。</div>
<div>我们可以看到，实际上，如果我们让G的值由负无穷变化到正无穷，相当于一条直线，它满足斜率为k，然后从坐标系的下方慢慢地向上平移到坐标系的上方。</div>
<div>那么，我们要找到，G的最小值，就是在这个过程中，这条直线所碰到的第一个点！</div>
<div>而这个点，必然是这个点集的凸包上的点。（不知道凸包概念的去baidu一下好了）</div>
<div>再加上很关键的一点。随着i的增加，点的坐标是单调不下降的（s[i]），直线的斜率也是单调不降的(2s[i])！</div>
<div>满足这两个单调性，我们就可以利用单调队列，来维护一个凸壳。因为我们要找G的最小值，所以要维护一个下凸壳。方法和之前那篇数形结合题一样，类似Garham求凸包的算法。</div>
<div>之所以可以用单调队列是因为坐标和斜率都满足单调性的话，可以证明每个点如果不是某个i的最优决策，也不能会是之后的i的最优决策，可以被抛弃。</div>
<div>因为每个i只会进出队列一次，所以时间复杂度降为O(N)，只需要保存单列就可以，空间复杂度为O(N)，比较完美地解决了这个问题。&nbsp;</div> <img src ="http://www.cppblog.com/powerwater/aggbug/173992.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2012-05-08 15:42 <a href="http://www.cppblog.com/powerwater/articles/173992.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】ACM题目分类+高质量题解</title><link>http://www.cppblog.com/powerwater/articles/164232.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Mon, 16 Jan 2012 00:38:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/164232.html</guid><description><![CDATA[<p><span style="font-size: 12pt">字符串处理</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/iiywj/blog/item/2bfd6487b985e73767096eb7.html">http://hi.baidu.com/iiywj/blog/item/2bfd6487b985e73767096eb7.html</a>&nbsp;</p>
<p><span style="font-size: 12pt">搜索</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%CB%D1%CB%F7">http://hi.baidu.com/forverlin1204/blog/category/%CB%D1%CB%F7</a>&nbsp;</p>
<p><span style="font-size: 12pt">图论</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%CD%BC%C2%DB">http://hi.baidu.com/forverlin1204/blog/category/%CD%BC%C2%DB</a>&nbsp;</p>
<p><span style="font-size: 12pt">数学</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%CA%FD%D1%A7">http://hi.baidu.com/forverlin1204/blog/category/%CA%FD%D1%A7</a>&nbsp;</p>
<p><span style="font-size: 12pt">模拟</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%C4%A3%C4%E2">http://hi.baidu.com/forverlin1204/blog/category/%C4%A3%C4%E2</a>&nbsp;</p>
<p><span style="font-size: 12pt">动规</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%B6%AF%B9%E9">http://hi.baidu.com/forverlin1204/blog/category/%B6%AF%B9%E9</a>&nbsp;</p>
<p><span style="font-size: 12pt">数据结构</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%CA%FD%BE%DD%BD%E1%B9%B9">http://hi.baidu.com/forverlin1204/blog/category/%CA%FD%BE%DD%BD%E1%B9%B9</a>&nbsp;</p>
<p><span style="font-size: 12pt">网络流</span></p>
<p><span style="font-size: 12pt"></span><a href="http://hi.baidu.com/forverlin1204/blog/category/%CD%F8%C2%E7%C1%F7">http://hi.baidu.com/forverlin1204/blog/category/%CD%F8%C2%E7%C1%F7</a>&nbsp;</p>
<p><span style="font-size: 12pt">计算几何</span></p>
<p><span style="font-size: 12pt"></span><a href="http://www.cppblog.com/Felicia/category/4905.html?Show=All">http://www.cppblog.com/Felicia/category/4905.html?Show=All</a><br /><span style="font-size: 12pt"><br />转自：</span><a href="http://hi.baidu.com/daizhy_acm/blog/item/338aa0217aefb443935807ce.html"><span style="font-size: 12pt">http://hi.baidu.com/daizhy_acm/blog/item/338aa0217aefb443935807ce.html</span></a></p><img src ="http://www.cppblog.com/powerwater/aggbug/164232.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2012-01-16 08:38 <a href="http://www.cppblog.com/powerwater/articles/164232.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>N皇后问题</title><link>http://www.cppblog.com/powerwater/articles/159494.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Wed, 02 Nov 2011 04:52:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/159494.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/159494.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/159494.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/159494.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/159494.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iterator</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;N(</span><span style="color: #000000">20</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;6</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;p[N];<br /></span><span style="color: #008080">&nbsp;7</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;a[N],b[</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">N],c[</span><span style="color: #000000">2</span><span style="color: #000000">*</span><span style="color: #000000">N];&nbsp;<br /></span><span style="color: #008080">&nbsp;8</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,tot;<br /></span><span style="color: #008080">&nbsp;9</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;search(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i)<br /></span><span style="color: #008080">10</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">11</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(i</span><span style="color: #000000">&gt;</span><span style="color: #000000">n)<br /></span><span style="color: #008080">12</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">13</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy(p</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,p</span><span style="color: #000000">+</span><span style="color: #000000">n</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,ostream_iterator</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">(cout,</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">));<br /></span><span style="color: #008080">14</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /></span><span style="color: #008080">15</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000">++</span><span style="color: #000000">;<br /></span><span style="color: #008080">16</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;<br /></span><span style="color: #008080">17</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">18</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;=</span><span style="color: #000000">n;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /></span><span style="color: #008080">19</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(</span><span style="color: #000000">!</span><span style="color: #000000">a[j]</span><span style="color: #000000">&amp;&amp;!</span><span style="color: #000000">b[i</span><span style="color: #000000">+</span><span style="color: #000000">j]</span><span style="color: #000000">&amp;&amp;!</span><span style="color: #000000">c[i</span><span style="color: #000000">-</span><span style="color: #000000">j</span><span style="color: #000000">+</span><span style="color: #000000">n])<br /></span><span style="color: #008080">20</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">21</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p[i]</span><span style="color: #000000">=</span><span style="color: #000000">j;<br /></span><span style="color: #008080">22</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: #000000">=</span><span style="color: #000000">b[i</span><span style="color: #000000">+</span><span style="color: #000000">j]</span><span style="color: #000000">=</span><span style="color: #000000">c[i</span><span style="color: #000000">-</span><span style="color: #000000">j</span><span style="color: #000000">+</span><span style="color: #000000">n]</span><span style="color: #000000">=</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /></span><span style="color: #008080">23</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search(i</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">24</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[j]</span><span style="color: #000000">=</span><span style="color: #000000">b[i</span><span style="color: #000000">+</span><span style="color: #000000">j]</span><span style="color: #000000">=</span><span style="color: #000000">c[i</span><span style="color: #000000">-</span><span style="color: #000000">j</span><span style="color: #000000">+</span><span style="color: #000000">n]</span><span style="color: #000000">=</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">26</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">27</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">28</span>&nbsp;<span style="color: #000000"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /></span><span style="color: #008080">29</span>&nbsp;<span style="color: #000000">{<br /></span><span style="color: #008080">30</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n)<br /></span><span style="color: #008080">31</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080">32</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">33</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(a,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(a));<br /></span><span style="color: #008080">34</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(b,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(b));<br /></span><span style="color: #008080">35</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(c,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(c));<br /></span><span style="color: #008080">36</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search(</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">37</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">tot</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /></span><span style="color: #008080">38</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080">39</span>&nbsp;<span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">40</span>&nbsp;<span style="color: #000000">}<br /></span><span style="color: #008080">41</span>&nbsp;<span style="color: #000000"></span></div><br /><img src ="http://www.cppblog.com/powerwater/aggbug/159494.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2011-11-02 12:52 <a href="http://www.cppblog.com/powerwater/articles/159494.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>N皇后的位运算解法</title><link>http://www.cppblog.com/powerwater/articles/159487.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Wed, 02 Nov 2011 01:23:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/159487.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/159487.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/159487.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/159487.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/159487.html</trackback:ping><description><![CDATA[<p>题解如下：</p>
<p>n皇后问题是啥我就不说了吧，学编程的肯定都见过。下面的十多行代码是n皇后问题 的一个高效位运算程序，看到过的人都夸它牛。初始时，upperlim:=(1 shl n)-1。主程序调用test(0,0,0)后sum的值就是n皇后总的解数。拿这个去交USACO，0.3s，暴爽。<br /><code><span style="font-size: 12pt">procedure test(row,ld,rd:longint);</span></code><br /><code><span style="font-size: 12pt">var</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos,p:longint;</span></code><br /><code><span style="font-size: 12pt">begin</span></code><br /><br /><code><span style="font-size: 12pt">&nbsp;&nbsp; if row&lt;&gt;upperlim then</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp; begin</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp; pos:=upperlim and not (row or ld or rd);</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp; while pos&lt;&gt;0 do</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp; begin</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p:=pos and -pos;</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos:=pos-p;</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test(row+p,(ld+p)shl 1,(rd+p)shr 1);</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp; end;</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp; end</span></code><br /><code><span style="font-size: 12pt">&nbsp;&nbsp; else inc(sum);</span></code><br /><br /><code><span style="font-size: 12pt">end;</span></code><br />&nbsp;&nbsp;&nbsp;&nbsp;乍一看似乎完全摸不着头脑，实际上整个程序是非常容易理解的。这里还是建议大家自己单步运行一探究竟，实在没研究出来再看下面的解说。<br /><br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;和 普通算法一样，这是一个递归过程，程序一行一行地寻找可以放皇后的地方。过程带三个参数，row、ld和rd，分别表示在纵列和两个对角线方向的限制条件 下这一行的哪些地方不能放。我们以6x6的棋盘为例，看看程序是怎么工作的。假设现在已经递归到第四层，前三层放的子已经标在左图上了。红色、蓝色和绿色 的线分别表示三个方向上有冲突的位置，位于该行上的冲突位置就用row、ld和rd中的1来表示。把它们三个并起来，得到该行所有的禁位，取反后就得到所 有可以放的位置（用pos来表示）。前面说过-a相当于not a + 1，这里的代码第6行就相当于pos and (not pos + 1)，其结果是取出最右边的那个1。这样，p就表示该行的某个可以放子的位置，把它从pos中移除并递归调用test过程。注意递归调用时三个参数的变 化，每个参数都加上了一个禁位，但两个对角线方向的禁位对下一行的影响需要平移一位。最后，如果递归到某个时候发现row=111111了，说明六个皇后 全放进去了，此时程序从第1行跳到第11行，找到的解的个数加一。</p> <img src ="http://www.cppblog.com/powerwater/aggbug/159487.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2011-11-02 09:23 <a href="http://www.cppblog.com/powerwater/articles/159487.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>家谱</title><link>http://www.cppblog.com/powerwater/articles/159462.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Tue, 01 Nov 2011 07:47:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/159462.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/159462.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/159462.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/159462.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/159462.html</trackback:ping><description><![CDATA[<div style="layout-grid:  15.6pt none" class="Section0">
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">源程序名</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">　　&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.???(</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'" class="15">PAS</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">,</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'" class="15">C</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'" class="15">,CPP</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">)</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">可执行文件名</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">&nbsp;&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.EXE</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">输入文件名　　</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.IN</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">输出文件名</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">&nbsp;&nbsp;&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.OUT</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">时间限制&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">2S</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"></span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">现代的人对于本家族血统越来越感兴趣，现在给出充足的父子关系，请你编写程序找到某个人的最早的祖先。</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">输入</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; text-indent: 21pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">输入文件由多行组成，首先是一系列有关父子关系的描述，其中每一组父子关系由二行组成，用#name的形式描写一组父子关系中的父亲的名字，用+name的形式描写一组父子关系中的儿子的名字；接下来用</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">?</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">name的形式表示要求该人的最早的祖先；最后用单独的一个</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">$</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">表示文件结束。规定每个人的名字都有且只有6个字符，而且首字母大写，且没有任意两个人的名字相同。最多可能有</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">1000</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">组父子关系，总人数最多可能达到</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">50000</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">人，家谱中的记载不超过</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">30</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">代。</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">输出</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; text-indent: 21pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">按照输入文件的要求顺序，求出每一个要找祖先的人的祖先，格式：本人的名字+一个空格+祖先的名字+回车。</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; text-indent: 21pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'">样例</span><span style="font-family: '宋体'; font-size: 10.5pt; font-weight: bold; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.IN</span><span style="font-style: italic; font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">#George</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">+Rodney</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">#Arthur</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">+Gareth</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">+Walter</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">#Gareth</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">+Edward</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">?Edward</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">?Walter</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">?Rodney</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">?Arthur</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">$</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">GEN</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">.OUT</span><span style="font-style: italic; font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">Edward&nbsp;Arthur</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">Walter&nbsp;Arthur</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">Rodney&nbsp;George</span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'">Arthur&nbsp;Arthur</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><img id="Code_Closed_Image_154622" onclick="this.style.display='none'; Code_Closed_Text_154622.style.display='none'; Code_Open_Image_154622.style.display='inline'; Code_Open_Text_154622.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" height="16"><img style="display: none" id="Code_Open_Image_154622" onclick="this.style.display='none'; Code_Open_Text_154622.style.display='none'; Code_Closed_Image_154622.style.display='inline'; Code_Closed_Text_154622.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" height="16"><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Code_Closed_Text_154622">code:</span><span style="display: none" id="Code_Open_Text_154622"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">fstream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;node<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;sn;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;name;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">next;<br />};<br />node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">g[</span><span style="color: #000000">127</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">{</span><span style="color: #000000">0</span><span style="color: #000000">};<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pn</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">,a[</span><span style="color: #000000">50001</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;name[</span><span style="color: #000000">50001</span><span style="color: #000000">];<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;find(</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;s)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;node&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p;<br />&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">=</span><span style="color: #000000">g[s[</span><span style="color: #000000">0</span><span style="color: #000000">]];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(p)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">name</span><span style="color: #000000">==</span><span style="color: #000000">s)&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">sn;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">=</span><span style="color: #000000">p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;name[</span><span style="color: #000000">++</span><span style="color: #000000">pn]</span><span style="color: #000000">=</span><span style="color: #000000">s;<br />&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;node;<br />&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">name</span><span style="color: #000000">=</span><span style="color: #000000">s;<br />&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">sn</span><span style="color: #000000">=</span><span style="color: #000000">pn;<br />&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">-&gt;</span><span style="color: #000000">next</span><span style="color: #000000">=</span><span style="color: #000000">g[s[</span><span style="color: #000000">0</span><span style="color: #000000">]];<br />&nbsp;&nbsp;&nbsp;&nbsp;g[s[</span><span style="color: #000000">0</span><span style="color: #000000">]]</span><span style="color: #000000">=</span><span style="color: #000000">p;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;pn;<br />}<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;f(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(a[x]</span><span style="color: #000000">!=</span><span style="color: #000000">x)&nbsp;x</span><span style="color: #000000">=</span><span style="color: #000000">a[x];<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;x;<br />}&nbsp;<br />ifstream&nbsp;cin(</span><span style="color: #000000">"</span><span style="color: #000000">gen.in</span><span style="color: #000000">"</span><span style="color: #000000">);<br />ofstream&nbsp;cout(</span><span style="color: #000000">"</span><span style="color: #000000">gen.out</span><span style="color: #000000">"</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">50000</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;a[i]</span><span style="color: #000000">=</span><span style="color: #000000">i;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;s1,s2;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x,y,k1,k2;<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(ch</span><span style="color: #000000">!=</span><span style="color: #000000">'</span><span style="color: #000000">?</span><span style="color: #000000">'</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">s1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="color: #000000">=</span><span style="color: #000000">find(s1);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k1</span><span style="color: #000000">=</span><span style="color: #000000">f(x);&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[x]</span><span style="color: #000000">=</span><span style="color: #000000">k1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(ch</span><span style="color: #000000">==</span><span style="color: #000000">'</span><span style="color: #000000">+</span><span style="color: #000000">'</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">s2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y</span><span style="color: #000000">=</span><span style="color: #000000">find(s2);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k2</span><span style="color: #000000">=</span><span style="color: #000000">f(y);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[k2]</span><span style="color: #000000">=</span><span style="color: #000000">a[y]</span><span style="color: #000000">=</span><span style="color: #000000">k1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(ch</span><span style="color: #000000">!=</span><span style="color: #000000">'</span><span style="color: #000000">$</span><span style="color: #000000">'</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">s1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">s1</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">name[f(find(s1))]</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">ch;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></span></div>
<p style="margin-top: 0pt; margin-bottom: 0pt" class="p0"></span><span style="font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'"><o:p></o:p></span></p></div><!--EndFragment--><br /><br /><img src ="http://www.cppblog.com/powerwater/aggbug/159462.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2011-11-01 15:47 <a href="http://www.cppblog.com/powerwater/articles/159462.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>迪杰斯特拉算法</title><link>http://www.cppblog.com/powerwater/articles/159383.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Mon, 31 Oct 2011 04:51:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/159383.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/159383.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/159383.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/159383.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/159383.html</trackback:ping><description><![CDATA[//dijkstra邻接矩阵实现 <br />//#include&lt;fstream&gt;<br />#include&lt;iostream&gt;<br />#include&lt;cstring&gt;<br />#include&lt;algorithm&gt;<br />#include&lt;iterator&gt;<br />using namespace std;<br />const int N(1000);<br />int dist[N];<br />int prev[N];<br />int g[N][N];<br />int n,en,source;<br />bool f[N];<br />//ifstream cin("data.in");<br />//ofstream cout("data.out");<br />void init()<br />{<br />&nbsp;&nbsp;&nbsp; int x,y,z;<br />&nbsp;&nbsp;&nbsp; cin&gt;&gt;n&gt;&gt;en&gt;&gt;source;<br />&nbsp;&nbsp;&nbsp; memset(g,0x7f,sizeof(g));<br />&nbsp;&nbsp;&nbsp; for (int i=0;i&lt;en;i++)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;x&gt;&gt;y&gt;&gt;z;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g[x][y]=g[y][x]=z;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; for (int i=1;i&lt;=n;i++) g[i][i]=0;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; for (int i=1;i&lt;=n;i++) dist[i]=g[source][i];<br />&nbsp;&nbsp;&nbsp; for (int i=1;i&lt;=n;i++) prev[i]=source;&nbsp;&nbsp;&nbsp; <br />}<br />void dijkstra()<br />{<br />&nbsp;&nbsp;&nbsp; int i,j,u;<br />&nbsp;&nbsp;&nbsp; memset(f,0,sizeof(f));<br />&nbsp;&nbsp;&nbsp; f[source]=true;<br />&nbsp;&nbsp;&nbsp; u=source;<br />&nbsp;&nbsp;&nbsp; for (i=1;i&lt;n;i++)<br />&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int tmp=0x7f7f7f7f;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=1;j&lt;=n;j++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!f[j]&amp;&amp;dist[j]&lt;tmp)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u=j;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmp=dist[j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f[u]=true;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=1;j&lt;=n;j++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!f[j]&amp;&amp;g[u][j]&lt;0x7f7f7f7f)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dist[u]+g[u][j]&lt;dist[j])<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dist[j]=dist[u]+g[u][j];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prev[j]=u;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />}&nbsp;&nbsp; <br />int main()<br />{<br />&nbsp;&nbsp;&nbsp; init();<br />&nbsp;&nbsp;&nbsp; dijkstra();<br />&nbsp;&nbsp;&nbsp; copy(dist+1,dist+n+1,ostream_iterator&lt;int&gt;(cout," "));<br />&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<br />&nbsp;&nbsp;&nbsp; system("pause");<br />&nbsp;&nbsp;&nbsp; return 0;<br />}<br /><img src ="http://www.cppblog.com/powerwater/aggbug/159383.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2011-10-31 12:51 <a href="http://www.cppblog.com/powerwater/articles/159383.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最长上升子序列</title><link>http://www.cppblog.com/powerwater/articles/159358.html</link><dc:creator>龙在江湖</dc:creator><author>龙在江湖</author><pubDate>Sun, 30 Oct 2011 09:21:00 GMT</pubDate><guid>http://www.cppblog.com/powerwater/articles/159358.html</guid><wfw:comment>http://www.cppblog.com/powerwater/comments/159358.html</wfw:comment><comments>http://www.cppblog.com/powerwater/articles/159358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/powerwater/comments/commentRss/159358.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/powerwater/services/trackbacks/159358.html</trackback:ping><description><![CDATA[<div>最优算法：时间复杂度最坏为nlogn 
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;f[</span><span style="color: #000000">100000</span><span style="color: #000000">],top;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,</span><span style="color: #000000">*</span><span style="color: #000000">p,x;<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n;<br />&nbsp;&nbsp;&nbsp;&nbsp;top</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(n</span><span style="color: #000000">--</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">x;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000">=</span><span style="color: #000000">lower_bound(f,f</span><span style="color: #000000">+</span><span style="color: #000000">top,x,greater</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">());&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">删掉greater&lt;int&gt;()或换成less&lt;int&gt;()则为最长上升子序列&nbsp;</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">p</span><span style="color: #000000">=</span><span style="color: #000000">x;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(p</span><span style="color: #000000">-</span><span style="color: #000000">f</span><span style="color: #000000">&gt;=</span><span style="color: #000000">top)&nbsp;top</span><span style="color: #000000">++</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">top</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></div><br />使用向量动态开辟空间： 
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">vector</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br />#include</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;v;<br />&nbsp;&nbsp;&nbsp;&nbsp;vector</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">::iterator&nbsp;it;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,x,top;<br />&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">n;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">&nbsp;(n</span><span style="color: #000000">--</span><span style="color: #000000">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">x;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it</span><span style="color: #000000">=</span><span style="color: #000000">lower_bound(v.begin(),v.end(),x);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;(it</span><span style="color: #000000">==</span><span style="color: #000000">v.end())&nbsp;v.push_back(x);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">it</span><span style="color: #000000">=</span><span style="color: #000000">x;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">v.size()</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br />&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br />}&nbsp;</span></div><br /><br /></div><img src ="http://www.cppblog.com/powerwater/aggbug/159358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/powerwater/" target="_blank">龙在江湖</a> 2011-10-30 17:21 <a href="http://www.cppblog.com/powerwater/articles/159358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>