﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-心如止水-随笔分类-算法与数据结构</title><link>http://www.cppblog.com/rakerichard/category/12750.html</link><description>Je n'ai pas le temps
</description><language>zh-cn</language><lastBuildDate>Tue, 16 Aug 2011 17:12:59 GMT</lastBuildDate><pubDate>Tue, 16 Aug 2011 17:12:59 GMT</pubDate><ttl>60</ttl><item><title>网络流24题</title><link>http://www.cppblog.com/rakerichard/archive/2011/08/15/153472.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Mon, 15 Aug 2011 14:19:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2011/08/15/153472.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/153472.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2011/08/15/153472.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/153472.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/153472.html</trackback:ping><description><![CDATA[大概一个星期前小猪发给我一份网络流24题，直到昨天才开始做。<br /><br />1、飞行员配对问题<br />二分图匹配问题，直接Hopcroft Karp即可。<br /><br />2、太空飞行计划问题<br />题目大意是有M个实验，有N个仪器，每个实验需要一个或多个仪器，做实验有收益，购买仪器需要消耗，求收益最多的方案。<br />最大权闭合图问题。设源点汇点为S、T，从S向M个实验连一条容量为所得收益的有向边，N个仪器向T连一条容量为所需消耗的有向边，另外，从某个实验向所需要的仪器连一条无穷大的有向边。<br />求该图最小割为MinCut，设所以实验的收益为TOT，则答案为TOT-MinCut。<br /><br />3、最小路径覆盖问题<br />对于一个有向无环图，将每个顶点X，拆成X1、X2，如果原图中有一条有向边&lt;X,Y&gt;，则在X1、Y2之间连一条边，答案即为原图中顶点数减去该二分图的最大匹配数。<br /><br />4、魔术球问题<br />题目大意是给出N个柱子，在这些柱子上依次放入编号为1、2、3&#8230;&#8230;的球，要求一个柱子上相邻两个数的和为完全平方数，求最多可以放置多少个球。<br />顺序枚举可以放入的球数，将每个数字看作一个顶点，如果i+j(i&lt;j)为完全平方数，则从i向j连一条有向边，最终的图为有向无环图，求此图的最小路径覆盖。枚举直到ans的最小路径覆盖为N，ans+1的最小路径覆盖为N+1。<br /><br />5、圆桌问题<br />题目大意是有N个代表团，第i个代表团有r[i]个人，有M个圆桌，第i个圆桌可容纳c[i]个人，同一个代表团的不能坐在一起，求一种安排方案。<br />二分图多重匹配问题。从S向N个代表团各连一条容量为r[i]的边，每个代表团向M个圆桌连一条容量为1的边，每个圆桌向T连一条容量为c[i]的边，如果网络流的最大流为代表团总人数，则存在这样的方案。<br /><br />7、试题库问题<br />题目大意是有N道试题，共有K种类型，每道试题可能属于一个或多个类别，现要求选出M道试题，使第i种类型题数为need[i]。求一种方案。<br />二分图多重匹配问题。设S、S'、T，从S向S'连一条容量为M的边，从S'向每道试题连一条容量为1的边，对于试题i，如果属于类型j，则从试题i向类型j连一条容量为1边，每个类型向T连一条容量为need[j]的边。如果网络流的最大流为M，则存在这样的方案。<img src ="http://www.cppblog.com/rakerichard/aggbug/153472.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2011-08-15 22:19 <a href="http://www.cppblog.com/rakerichard/archive/2011/08/15/153472.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>快速幂取模</title><link>http://www.cppblog.com/rakerichard/archive/2010/04/04/111604.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Sun, 04 Apr 2010 11:18:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/04/04/111604.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/111604.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/04/04/111604.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/111604.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/111604.html</trackback:ping><description><![CDATA[快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是ab mod c=(a mod c)(b mod c)mod c，因此很容易设计出一个基于二分的递归算法。<br>以下是我的代码，以下代码必须保证输入的是合法的表达式，比如不能出现0^0 mod b：<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%; 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: #0000ff">long</span><span style="COLOR: #000000">&nbsp;exp_mod(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;b)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">b;<br>&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">exp_mod(a,n</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">t</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">((n</span><span style="COLOR: #000000">&amp;</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;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;t;<br>}</span></div>
<br><br>
<img src ="http://www.cppblog.com/rakerichard/aggbug/111604.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-04-04 19:18 <a href="http://www.cppblog.com/rakerichard/archive/2010/04/04/111604.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最小费用最大流算法</title><link>http://www.cppblog.com/rakerichard/archive/2010/04/01/111336.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Thu, 01 Apr 2010 15:04:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/04/01/111336.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/111336.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/04/01/111336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/111336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/111336.html</trackback:ping><description><![CDATA[基本思路：每次使用SPFA求出一条可增广流的最短路，对最短路上的流进行增流；重复上述过程，直至不存在可增广路。<br>以下是我的代码，仅供参考：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;maxn&nbsp;507</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;INF&nbsp;200000007</span><span style="color: #000000;"><br>FILE&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">fin</span><span style="color: #000000;">=</span><span style="color: #000000;">fopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">),</span><span style="color: #000000;">*</span><span style="color: #000000;">fout</span><span style="color: #000000;">=</span><span style="color: #000000;">fopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>typedef&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;count,front,rear,item[maxn];<br>}queue;<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Clear(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;q.front</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;q.rear</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Push(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">++</span><span style="color: #000000;">;q.rear</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(q.rear</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">maxn)&nbsp;q.rear</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.item[q.rear]</span><span style="color: #000000;">=</span><span style="color: #000000;">x;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Pop(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">--</span><span style="color: #000000;">;x</span><span style="color: #000000;">=</span><span style="color: #000000;">q.item[q.front];<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(q.front</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">maxn)&nbsp;q.front</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;Empty(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(q.count</span><span style="color: #000000;">==</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br>}<br><br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n,m,cap[maxn][maxn],cost[maxn][maxn];<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;s,t,minc,maxf,flow[maxn][maxn];<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;min(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;b)<br>{</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(a</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">b</span><span style="color: #000000;">?</span><span style="color: #000000;">a:b);}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;input()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;fscanf(fin,</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cap[i][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;cost[i][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">INF;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;a,b,c,d;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fscanf(fin,</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">b,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">c,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">d);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cap[a][b]</span><span style="color: #000000;">=</span><span style="color: #000000;">c;cost[a][b]</span><span style="color: #000000;">=</span><span style="color: #000000;">d;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Mincost_Maxflow()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;tmp,d[maxn],father[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;inq[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;queue&nbsp;q;Clear(q);<br>&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;t</span><span style="color: #000000;">=</span><span style="color: #000000;">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;flow[i][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;minc</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;maxf</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;">(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">(i</span><span style="color: #000000;">==</span><span style="color: #000000;">s</span><span style="color: #000000;">?</span><span style="color: #000000;">0</span><span style="color: #000000;">:INF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inq[i]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[s]</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;Push(q,s);inq[s]</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">Empty(q))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i;Pop(q,i);inq[i]</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;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(cap[i][j]</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">flow[i][j]</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">cost[i][j]</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">INF</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">d[i]</span><span style="color: #000000;">+</span><span style="color: #000000;">cost[i][j]</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">d[j])<br>&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;d[j]</span><span style="color: #000000;">=</span><span style="color: #000000;">d[i]</span><span style="color: #000000;">+</span><span style="color: #000000;">cost[i][j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[j]</span><span style="color: #000000;">=</span><span style="color: #000000;">i;<br>&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;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">inq[j])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{Push(q,j);inq[j]</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;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(d[t]</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">INF)&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp</span><span style="color: #000000;">=</span><span style="color: #000000;">INF;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">t;i</span><span style="color: #000000;">!=</span><span style="color: #000000;">s;i</span><span style="color: #000000;">=</span><span style="color: #000000;">father[i])&nbsp;tmp</span><span style="color: #000000;">=</span><span style="color: #000000;">min(tmp,cap[father[i]][i]</span><span style="color: #000000;">-</span><span style="color: #000000;">flow[father[i]][i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">t;i</span><span style="color: #000000;">!=</span><span style="color: #000000;">s;i</span><span style="color: #000000;">=</span><span style="color: #000000;">father[i])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flow[father[i]][i]</span><span style="color: #000000;">+=</span><span style="color: #000000;">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flow[i][father[i]]</span><span style="color: #000000;">-=</span><span style="color: #000000;">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minc</span><span style="color: #000000;">+=</span><span style="color: #000000;">tmp</span><span style="color: #000000;">*</span><span style="color: #000000;">d[t];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxf</span><span style="color: #000000;">+=</span><span style="color: #000000;">tmp;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;output()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(fout,</span><span style="color: #000000;">"</span><span style="color: #000000;">Mincost&nbsp;:=&nbsp;%ld\nMaxflow&nbsp;:=&nbsp;%ld\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,minc,maxf);<br>}<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;input();<br>&nbsp;&nbsp;&nbsp;&nbsp;Mincost_Maxflow();<br>&nbsp;&nbsp;&nbsp;&nbsp;output();<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/111336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-04-01 23:04 <a href="http://www.cppblog.com/rakerichard/archive/2010/04/01/111336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数组模拟链表操作</title><link>http://www.cppblog.com/rakerichard/archive/2010/03/16/109854.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Tue, 16 Mar 2010 14:39:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/03/16/109854.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/109854.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/03/16/109854.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/109854.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/109854.html</trackback:ping><description><![CDATA[很久就知道链表可以用数组来模拟（其实很多数据结构数组都可以实现），一开始感觉用数组模拟还不简单吗？于是很长的一段时间内都没有去写这个程序。今天突然想到了，想去实现它，结果竟然思考了大概20min才想起如何构造数据结构，看来程序还得多动手实践。<br>以下是我的代码：<br>insert(a,b)：在链表中值为b的元素前面插入a，如果找不到b，将a插入链表末端；<br>print()：遍历整个链表，并输出。<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">stdio.h</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;">.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;maxn&nbsp;1007</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n,a[maxn],next[maxn];<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;insert(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="color: #000000;">++</span><span style="color: #000000;">;a[n]</span><span style="color: #000000;">=</span><span style="color: #000000;">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(next[p]</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">a[next[p]]</span><span style="color: #000000;">!=</span><span style="color: #000000;">y)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">next[p];<br>&nbsp;&nbsp;&nbsp;&nbsp;next[n]</span><span style="color: #000000;">=</span><span style="color: #000000;">next[p];<br>&nbsp;&nbsp;&nbsp;&nbsp;next[p]</span><span style="color: #000000;">=</span><span style="color: #000000;">n;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;print()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">next[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;first</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(p)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(first)&nbsp;first</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;printf(</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;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,a[p]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="color: #000000;">=</span><span style="color: #000000;">next[p];<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;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;m,a,b;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">char</span><span style="color: #000000;">&nbsp;cmd[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(next,</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(next));<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(m</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%s</span><span style="color: #000000;">"</span><span style="color: #000000;">,cmd);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(cmd[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">==</span><span style="color: #000000;">'</span><span style="color: #000000;">I</span><span style="color: #000000;">'</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">b);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(a,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print();putchar(</span><span style="color: #000000;">'</span><span style="color: #000000;">\n</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/109854.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-03-16 22:39 <a href="http://www.cppblog.com/rakerichard/archive/2010/03/16/109854.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Splay树的基本操作</title><link>http://www.cppblog.com/rakerichard/archive/2010/03/12/109577.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Fri, 12 Mar 2010 14:28:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/03/12/109577.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/109577.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/03/12/109577.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/109577.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/109577.html</trackback:ping><description><![CDATA[最近对树型数据结构特别感兴趣！<br>以下是我的Splay树代码：<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"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;maxn&nbsp;10007</span><span style="COLOR: #000000"><br>typedef&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;data,fa,ls,rs;<br>}Node;<br>Node&nbsp;splay[maxn];<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;m,root;<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Zig(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;node)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[node].fa;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[t].rs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[node].ls;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(splay[node].ls)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;splay[splay[node].ls].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[node].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[t].fa;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[node].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(splay[t].fa)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[splay[t].fa].ls)&nbsp;splay[splay[t].fa].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;splay[splay[t].fa].rs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[t].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Zag(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;node)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[node].fa;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[t].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[node].rs;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(splay[node].rs)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;splay[splay[node].rs].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[node].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[t].fa;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[node].rs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(splay[t].fa)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[splay[t].fa].ls)&nbsp;splay[splay[t].fa].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;splay[splay[t].fa].rs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[t].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Splay(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;node)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(splay[node].fa)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[node].fa;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(splay[t].fa</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(node</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[t].ls)&nbsp;Zag(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;Zig(node);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(t</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[splay[t].fa].ls)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(node</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[t].ls)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{Zag(t);Zag(node);}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;{Zig(node);Zag(node);}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(node</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">splay[t].ls)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{Zag(node);Zig(node);}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;{Zig(t);Zig(node);}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;root</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">node;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Insert(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;p,q;<br>&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[m].data</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[m].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[m].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[m].rs</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">if</span><span style="COLOR: #000000">(root</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m;</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">(p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">root;p;&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">splay[p].data)&nbsp;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[p].ls;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[p].rs;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;splay[m].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">q;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(x</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">splay[q].data)&nbsp;splay[q].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;splay[q].rs</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m;<br>&nbsp;&nbsp;&nbsp;&nbsp;Splay(m);<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.in</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.out</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">root</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">(</span><span style="COLOR: #0000ff">long</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;splay[i].data</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[i].fa</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[i].ls</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">splay[i].rs</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">(</span><span style="COLOR: #0000ff">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">t);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Insert(t);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;Splay(</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">(</span><span style="COLOR: #0000ff">long</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;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No.%ld&nbsp;data.%ld&nbsp;fa.%ld&nbsp;ls.%ld&nbsp;rs.%ld\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,i,splay[i].data,splay[i].fa,splay[i].ls,splay[i].rs);<br></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></div>
<br>
<img src ="http://www.cppblog.com/rakerichard/aggbug/109577.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-03-12 22:28 <a href="http://www.cppblog.com/rakerichard/archive/2010/03/12/109577.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Miller-Rabbin素数测试</title><link>http://www.cppblog.com/rakerichard/archive/2010/02/20/108099.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Sat, 20 Feb 2010 08:02:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/02/20/108099.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/108099.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/02/20/108099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/108099.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/108099.html</trackback:ping><description><![CDATA[如果n是一个正整数，如果存在和n互素的正整数a满足a^n-1&#8801;1(mod n)，我们说n是基于a的伪素数。如果一个数是伪素数，它几乎肯定是素数。<br>以下是我的miller_rabbin素数测试的代码：<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"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">time.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;maxTest&nbsp;5</span><span style="COLOR: #000000"><br>typedef&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;int64;<br>int64&nbsp;random(int64&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(int64)((</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">)rand()</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">RAND_MAX</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">0.5</span><span style="COLOR: #000000">);<br>}<br>int64&nbsp;mod_exp(int64&nbsp;a,int64&nbsp;b,int64&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;ans;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(b</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mod_exp(a,b</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(b</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;ans</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ans</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">a</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n;<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;miller_rabbin(int64&nbsp;n)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">maxTest;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">random(n</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">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(mod_exp(a,n</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,n)</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<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: #008000">/*</span><span style="COLOR: #008000"><br>&nbsp;&nbsp;&nbsp;&nbsp;freopen("data.in","r",stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen("data.out","w",stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;//</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;srand(time(NULL));<br>&nbsp;&nbsp;&nbsp;&nbsp;int64&nbsp;n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%I64d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</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;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(miller_rabbin(n))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">True\n\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">False\n\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></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></div>
<br>
<img src ="http://www.cppblog.com/rakerichard/aggbug/108099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-02-20 16:02 <a href="http://www.cppblog.com/rakerichard/archive/2010/02/20/108099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>第一个线段树程序</title><link>http://www.cppblog.com/rakerichard/archive/2010/02/17/107961.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 17 Feb 2010 05:33:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/02/17/107961.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/107961.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/02/17/107961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/107961.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/107961.html</trackback:ping><description><![CDATA[被线段树折磨了这么久，今天终于把它给写出来了&#8230;&#8230;用的是数组模拟树。<br>功能：创建一棵线段树；给a[i]加上一个值；求一个区间的最大值。<br>以下是我的代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;"><br>&nbsp;&nbsp;&nbsp;&nbsp;Author&nbsp;&gt;&gt;&nbsp;&nbsp;Lee1R<br>&nbsp;&nbsp;&nbsp;&nbsp;Time&nbsp;&gt;&gt;&nbsp;02,16,2010&nbsp;23:37<br>//</span><span style="color: #008000;">*/</span><span style="color: #000000;"><br><br>#include</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">10007</span><span style="color: #000000;">;<br>typedef&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;NODE<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;a,b,sum;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;left,right;<br>}node;<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n,m,a[maxn];<br>node&nbsp;tree[maxn];</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;tot;<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;creat(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;now,mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000;">++</span><span style="color: #000000;">;now</span><span style="color: #000000;">=</span><span style="color: #000000;">tot;<br>&nbsp;&nbsp;&nbsp;&nbsp;tree[now].a</span><span style="color: #000000;">=</span><span style="color: #000000;">begin;tree[now].b</span><span style="color: #000000;">=</span><span style="color: #000000;">end;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(end</span><span style="color: #000000;">-</span><span style="color: #000000;">begin</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">1</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].left</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].right</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].sum</span><span style="color: #000000;">=</span><span style="color: #000000;">a[begin]</span><span style="color: #000000;">+</span><span style="color: #000000;">a[end];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].left</span><span style="color: #000000;">=</span><span style="color: #000000;">tot</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;creat(begin,mid);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].right</span><span style="color: #000000;">=</span><span style="color: #000000;">tot</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;creat(mid,end);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tree[now].sum</span><span style="color: #000000;">=</span><span style="color: #000000;">tree[tree[now].left].sum</span><span style="color: #000000;">+</span><span style="color: #000000;">tree[tree[now].right].sum</span><span style="color: #000000;">-</span><span style="color: #000000;">a[mid];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;add(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;pos,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;det,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;node)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin</span><span style="color: #000000;">=</span><span style="color: #000000;">tree[node].a,end</span><span style="color: #000000;">=</span><span style="color: #000000;">tree[node].b,mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;tree[node].sum</span><span style="color: #000000;">+=</span><span style="color: #000000;">det;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(end</span><span style="color: #000000;">-</span><span style="color: #000000;">begin</span><span style="color: #000000;">==</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pos</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">begin</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">pos</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">mid)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(pos,det,tree[node].left);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(pos</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">mid</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">pos</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(pos,det,tree[node].right);<br>}<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;getsum(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;node)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;re</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,t</span><span style="color: #000000;">=</span><span style="color: #000000;">(tree[node].a</span><span style="color: #000000;">+</span><span style="color: #000000;">tree[node].b)</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;">if</span><span style="color: #000000;">(tree[node].b</span><span style="color: #000000;">-</span><span style="color: #000000;">tree[node].a</span><span style="color: #000000;">==</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;re</span><span style="color: #000000;">=</span><span style="color: #000000;">tree[node].sum;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(end</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">t)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re</span><span style="color: #000000;">=</span><span style="color: #000000;">getsum(begin,end,tree[node].left);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">t)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re</span><span style="color: #000000;">=</span><span style="color: #000000;">getsum(begin,end,tree[node].right);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re</span><span style="color: #000000;">=</span><span style="color: #000000;">getsum(begin,t,tree[node].left)</span><span style="color: #000000;">+</span><span style="color: #000000;">getsum(t,end,tree[node].right)</span><span style="color: #000000;">-</span><span style="color: #000000;">a[t];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;re;<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: #008000;">//</span><span style="color: #008000;">*</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">*/</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;tot</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;creat(</span><span style="color: #000000;">1</span><span style="color: #000000;">,n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Init</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;t,b,c;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">t,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">b,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">c);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">switch</span><span style="color: #000000;">(t)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">case</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[b]</span><span style="color: #000000;">+=</span><span style="color: #000000;">c;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add(b,c,</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">case</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">:<br>&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;">(b</span><span style="color: #000000;">==</span><span style="color: #000000;">c)&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,a[b]);<br>&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;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,getsum(b,c,</span><span style="color: #000000;">1</span><span style="color: #000000;">));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/107961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-02-17 13:33 <a href="http://www.cppblog.com/rakerichard/archive/2010/02/17/107961.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>各顶点间的最短路——Floyed算法</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/21/106190.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Thu, 21 Jan 2010 15:10:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/21/106190.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/106190.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/21/106190.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/106190.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/106190.html</trackback:ping><description><![CDATA[以下是我的代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;k</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">;k</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">n;k</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(k</span><span style="color: #000000;">!=</span><span style="color: #000000;">i</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">i</span><span style="color: #000000;">!=</span><span style="color: #000000;">j</span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;">j</span><span style="color: #000000;">!=</span><span style="color: #000000;">k)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">min(d[i][j],d[i][k]</span><span style="color: #000000;">+</span><span style="color: #000000;">d[k][j])<br></span></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/106190.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-21 23:10 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/21/106190.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最小生成树——Kruskal算法</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/21/106189.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Thu, 21 Jan 2010 15:04:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/21/106189.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/106189.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/21/106189.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/106189.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/106189.html</trackback:ping><description><![CDATA[以下是我的代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">107</span><span style="color: #000000;">,INF</span><span style="color: #000000;">=</span><span style="color: #000000;">10000007</span><span style="color: #000000;">;<br>typedef&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;u,v,w;<br>}edge;<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n,m,g[maxn][maxn],mst;<br>edge&nbsp;E[maxn</span><span style="color: #000000;">*</span><span style="color: #000000;">maxn];<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;f[maxn];<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;Getf(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(f[x]</span><span style="color: #000000;">==</span><span style="color: #000000;">x)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;f[x]</span><span style="color: #000000;">=</span><span style="color: #000000;">Getf(f[x]);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;f[x];<br>}<br></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;Judge(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(Getf(x)</span><span style="color: #000000;">==</span><span style="color: #000000;">Getf(y));<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Union(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">Getf(x),j</span><span style="color: #000000;">=</span><span style="color: #000000;">Getf(y);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i</span><span style="color: #000000;">==</span><span style="color: #000000;">j)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;f[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">j;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;Qsort(edge&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">begin,j</span><span style="color: #000000;">=</span><span style="color: #000000;">end,mid</span><span style="color: #000000;">=</span><span style="color: #000000;">a[(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">].w;<br>&nbsp;&nbsp;&nbsp;&nbsp;edge&nbsp;t;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">do</span><span style="color: #000000;">{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(a[i].w</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">mid)&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(a[j].w</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">mid)&nbsp;j</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t</span><span style="color: #000000;">=</span><span style="color: #000000;">a[i];a[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">a[j];a[j]</span><span style="color: #000000;">=</span><span style="color: #000000;">t;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;j</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">j);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">j)&nbsp;Qsort(a,begin,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(i</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)&nbsp;&nbsp;&nbsp;Qsort(a,i,end);<br>}<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">n,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;g[i][j]</span><span style="color: #000000;">=</span><span style="color: #000000;">INF;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld%ld%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">E[i].u,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">E[i].v,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">E[i].w);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Read&nbsp;In</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Qsort(E,</span><span style="color: #000000;">1</span><span style="color: #000000;">,m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Qsort</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;">(</span><span style="color: #0000ff;">long</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;f[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Init</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;mst</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;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,j</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;">(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;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;a</span><span style="color: #000000;">=</span><span style="color: #000000;">E[j].u,b</span><span style="color: #000000;">=</span><span style="color: #000000;">E[j].v;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">Judge(a,b))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mst</span><span style="color: #000000;">+=</span><span style="color: #000000;">E[j].w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Union(a,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,mst);<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/106189.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-21 23:04 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/21/106189.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>队列的常用操作函数</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/21/106188.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Thu, 21 Jan 2010 14:44:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/21/106188.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/106188.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/21/106188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/106188.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/106188.html</trackback:ping><description><![CDATA[以下是我的代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">1007</span><span style="color: #000000;">;<br>typedef&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;front,rear,count,item[maxn];<br>}queue;<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;clear(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;empty(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(q.count</span><span style="color: #000000;">==</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;put(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.item[q.rear]</span><span style="color: #000000;">=</span><span style="color: #000000;">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">(queue&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="color: #000000;">=</span><span style="color: #000000;">q.item[q.front];<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br>}</span></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/106188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-21 22:44 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/21/106188.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>栈的常用操作函数</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/21/106187.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Thu, 21 Jan 2010 14:39:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/21/106187.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/106187.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/21/106187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/106187.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/106187.html</trackback:ping><description><![CDATA[以下是我的代码：<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<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;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">1007</span><span style="color: #000000;">;<br>typedef&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;top,count,item[maxn];<br>}stack;<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;clear(stack&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.top</span><span style="color: #000000;">=-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;empty(stack&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;(q.count</span><span style="color: #000000;">==</span><span style="color: #000000;">0</span><span style="color: #000000;">);<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;push(stack&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.top</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.item[q.top]</span><span style="color: #000000;">=</span><span style="color: #000000;">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;pop(stack&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">q,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="color: #000000;">=</span><span style="color: #000000;">q.item[q.top];<br>&nbsp;&nbsp;&nbsp;&nbsp;q.top</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="color: #000000;">--</span><span style="color: #000000;">;<br>}<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;stack&nbsp;s;clear(s);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</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;">10</span><span style="color: #000000;">;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;push(s,i);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">empty(s))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop(s,x);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld\n</span><span style="color: #000000;">"</span><span style="color: #000000;">,x);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;getchar();<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/106187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-21 22:39 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/21/106187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>增广路算法——Edmonds-Karp算法</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/20/106087.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 20 Jan 2010 13:58:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/20/106087.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/106087.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/20/106087.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/106087.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/106087.html</trackback:ping><description><![CDATA[<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"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;maxn</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">107</span><span style="COLOR: #000000">,INF</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2000007</span><span style="COLOR: #000000">;<br>typedef&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;front,rear,count,item[maxn];<br>}queue;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;clear(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;empty(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(q.count</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;push(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.item[q.rear]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;pop(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">q.item[q.front];<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Queue</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n,m,cap[maxn][maxn],flow[maxn][maxn];<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;s,t,fmax;<br><br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;min(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;b)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(a</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">b</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">a:b);<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;read()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;s</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;cap[i][j]</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">(</span><span style="COLOR: #0000ff">long</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">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a,b,w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">w);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cap[a][b]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">w;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;EK()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;tmp[maxn],father[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;queue&nbsp;q;clear(q);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;flow[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;fmax</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">(</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;tmp[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">father[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp[s]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">INF;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push(q,s);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">empty(q))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop(q,i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">tmp[j]</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">cap[i][j]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">flow[i][j])<br>&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;tmp[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">min(tmp[i],cap[i][j]</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">flow[i][j]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push(q,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(tmp[t]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">t;i</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">s;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">father[i])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flow[father[i]][i]</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">tmp[t];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flow[i][father[i]]</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">tmp[t];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmax</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">tmp[t];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,fmax);<br>}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.in</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.out</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;read();<br>&nbsp;&nbsp;&nbsp;&nbsp;EK();<br></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></div>
<img src ="http://www.cppblog.com/rakerichard/aggbug/106087.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-20 21:58 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/20/106087.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>二分查找及拓展</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/10/105324.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Sun, 10 Jan 2010 06:05:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/10/105324.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/105324.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/10/105324.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/105324.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/105324.html</trackback:ping><description><![CDATA[bsearch()功能：在有序数组a的[begin,end)区间中查找数字x，如果找不到，返回0；<br>low_search()功能：在有序数组a的[begin,end)区间中插入一个数字x，x尽量向前插入，返回x应该插入的位置；<br>high_search()功能：在有序数组a的[begin,end)区间中插入一个数字x，x尽量向后插入，返回x应该插入的位置。<br>以下是我的代码：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;bsearch(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;[begin,end)</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(a[mid]</span><span style="color: #000000;">==</span><span style="color: #000000;">x)&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(a[mid]</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">x)&nbsp;end</span><span style="color: #000000;">=</span><span style="color: #000000;">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;begin</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<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></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;low_search(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(a[mid]</span><span style="color: #000000;">&gt;=</span><span style="color: #000000;">x)&nbsp;end</span><span style="color: #000000;">=</span><span style="color: #000000;">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;begin</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;begin;<br>}<br></span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;high_search(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(a[mid]</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">x)&nbsp;begin</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;end</span><span style="color: #000000;">=</span><span style="color: #000000;">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;begin;<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: #008000;">//</span><span style="color: #008000;">*</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">*/</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">108</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,a[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a[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;n</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br></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></div>
<br><br><img src ="http://www.cppblog.com/rakerichard/aggbug/105324.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-10 14:05 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/10/105324.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>归并排序与逆序对</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/09/105260.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Sat, 09 Jan 2010 10:26:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/09/105260.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/105260.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/09/105260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/105260.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/105260.html</trackback:ping><description><![CDATA[<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;">stdio.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;msort(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">t,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">,p</span><span style="color: #000000;">=</span><span style="color: #000000;">begin,q</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</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;">begin;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msort(a,t,begin,mid);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msort(a,t,mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,end);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(q</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">end</span><span style="color: #000000;">||</span><span style="color: #000000;">a[p]</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">a[q])&nbsp;t[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">a[p</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;t[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">a[q</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">begin;i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">end;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;a[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">t[i];<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;</span><span style="color: #008000;">//</span><span style="color: #008000;">*</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.in</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">r</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="color: #000000;">"</span><span style="color: #000000;">data.out</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">w</span><span style="color: #000000;">"</span><span style="color: #000000;">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">*/</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;maxn</span><span style="color: #000000;">=</span><span style="color: #000000;">108</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;n</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,a[maxn],t[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;first</span><span style="color: #000000;">=</span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(scanf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">a[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;n</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Read&nbsp;In</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;msort(a,t,</span><span style="color: #000000;">1</span><span style="color: #000000;">,n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;M_sort</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;">(</span><span style="color: #0000ff;">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(first)&nbsp;first</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;printf(</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;printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">%ld</span><span style="color: #000000;">"</span><span style="color: #000000;">,a[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;putchar(</span><span style="color: #000000;">'</span><span style="color: #000000;">\n</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br></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></div>
在上面的归并排序程序的基础上，很容易修改得出求一个序列逆序对个数的程序。<br>以下是我的代码，只给出函数，main()与上面的基本相同：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;count(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">a,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">t,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;begin,</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;end)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mid</span><span style="color: #000000;">=</span><span style="color: #000000;">(begin</span><span style="color: #000000;">+</span><span style="color: #000000;">end)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">,p</span><span style="color: #000000;">=</span><span style="color: #000000;">begin,q</span><span style="color: #000000;">=</span><span style="color: #000000;">mid</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;">begin,re</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;">if</span><span style="color: #000000;">(begin</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re</span><span style="color: #000000;">+=</span><span style="color: #000000;">count(a,t,begin,mid);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;re</span><span style="color: #000000;">+=</span><span style="color: #000000;">count(a,t,mid</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">,end);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">while</span><span style="color: #000000;">(i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">end)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">(q</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">end</span><span style="color: #000000;">||</span><span style="color: #000000;">a[p]</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">a[q])&nbsp;t[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">a[p</span><span style="color: #000000;">++</span><span style="color: #000000;">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t[i</span><span style="color: #000000;">++</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">a[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;re</span><span style="color: #000000;">+=</span><span style="color: #000000;">mid</span><span style="color: #000000;">-</span><span style="color: #000000;">p</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;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;i</span><span style="color: #000000;">=</span><span style="color: #000000;">begin;i</span><span style="color: #000000;">&lt;=</span><span style="color: #000000;">end;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)&nbsp;a[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">t[i];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;re;<br>}</span></div>
<br>  <img src ="http://www.cppblog.com/rakerichard/aggbug/105260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-09 18:26 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/09/105260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对于09 NOIp</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104949.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:28:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104949.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104949.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104949.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104949.html</trackback:ping><description><![CDATA[<p><font face=宋体><span>一转眼，一年的时间又过去了，一年一次的</span><span>NOIp</span><span>又要到了。经过一年的学习，感觉自己比原来进步了许多。在</span><span>09NOIp</span><span>中对自己提出几点要求和注意事项。</span></font></p>
<p><font face=宋体><span><span>1、</span></span><span>时刻保持冷静的头脑；</span></font></p>
<p><font face=宋体><span><span>2、</span></span><span>不论是什么题目（做过或是没做过，水题或难题），都要仔细阅读题目；</span></font></p>
<p><font face=宋体><span><span>3、</span></span><span>开始编码之前，先写出程序大纲，写出需要编写的函数；</span></font></p>
<p><font face=宋体><span><span>4、</span></span><span>尽量不要使用全局变量（特别是用于循环的变量），一定避免变量重名；</span></font></p>
<p><font face=宋体><span><span>5、</span></span><span>搜索时，设计的剪枝策略无法出解时，在保证正确性的情况下，适当放宽搜索条件；</span></font></p>
<p><font face=宋体><span><span>6、</span></span><span>动态规划时，一定时刻明确状态的定义，确定边界条件；</span></font></p>
<p><font face=宋体><span><span>7、</span></span><span>模拟时，理清逻辑关系；</span></font></p>
<p><span><font face=宋体>8、在不影响结果的时候，函数调用可以使用地址传递，效率较高。</font></span></p>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:28 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于堆。</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104945.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:23:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104945.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104945.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104945.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104945.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104945.html</trackback:ping><description><![CDATA[<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"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;heap_adjust(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a[],</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;begin,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;end)<br><img id=Codehighlighter1_47_263_Open_Image onclick="this.style.display='none'; Codehighlighter1_47_263_Open_Text.style.display='none'; Codehighlighter1_47_263_Closed_Image.style.display='inline'; Codehighlighter1_47_263_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_47_263_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_47_263_Closed_Text.style.display='none'; Codehighlighter1_47_263_Open_Image.style.display='inline'; Codehighlighter1_47_263_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_47_263_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_47_263_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;i,m;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[begin];<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">begin</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">end;i</span><span style="COLOR: #000000">*=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_114_245_Open_Image onclick="this.style.display='none'; Codehighlighter1_114_245_Open_Text.style.display='none'; Codehighlighter1_114_245_Closed_Image.style.display='inline'; Codehighlighter1_114_245_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_114_245_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_114_245_Closed_Text.style.display='none'; Codehighlighter1_114_245_Open_Image.style.display='inline'; Codehighlighter1_114_245_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_114_245_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_114_245_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">end&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;a[i]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">a[i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(&nbsp;m</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">a[i]&nbsp;)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[begin]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[i];<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;a[begin]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m;<br><img id=Codehighlighter1_268_281_Open_Image onclick="this.style.display='none'; Codehighlighter1_268_281_Open_Text.style.display='none'; Codehighlighter1_268_281_Closed_Image.style.display='inline'; Codehighlighter1_268_281_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_268_281_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_268_281_Closed_Text.style.display='none'; Codehighlighter1_268_281_Open_Image.style.display='inline'; Codehighlighter1_268_281_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>}</span></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_268_281_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id=Codehighlighter1_268_281_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">调整，使之成为一个堆</span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p><font face=宋体 size=3>Ps：</font></p>
<p><font face=宋体 size=3>&nbsp;&nbsp;&nbsp; 1.对堆的调整从顶至下进行。</font></p>
<p><font face=宋体 size=3>&nbsp;&nbsp;&nbsp; 2.创建一个新堆应该从最后一个叶结点的父结点开始逐步向上调整。</font></p>
<p><font face=宋体 size=3>&nbsp;&nbsp;&nbsp; 3.对heap[s..m]能够调整成功，需要heap[s+1..m]严格满足堆的定义。</font></p>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104945.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:23 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104945.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>链表常用操作</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104944.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:22:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104944.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104944.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104944.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104944.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104944.html</trackback:ping><description><![CDATA[<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"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdlib.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node<br><img id=Codehighlighter1_49_94_Open_Image onclick="this.style.display='none'; Codehighlighter1_49_94_Open_Text.style.display='none'; Codehighlighter1_49_94_Closed_Image.style.display='inline'; Codehighlighter1_49_94_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_49_94_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_49_94_Closed_Text.style.display='none'; Codehighlighter1_49_94_Open_Image.style.display='inline'; Codehighlighter1_49_94_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_49_94_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_49_94_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&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><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000">;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">结构类型的说明</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">stack(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">head)<br><img id=Codehighlighter1_145_440_Open_Image onclick="this.style.display='none'; Codehighlighter1_145_440_Open_Text.style.display='none'; Codehighlighter1_145_440_Closed_Image.style.display='inline'; Codehighlighter1_145_440_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_145_440_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_145_440_Closed_Text.style.display='none'; Codehighlighter1_145_440_Open_Image.style.display='inline'; Codehighlighter1_145_440_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_145_440_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_145_440_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(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">10</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_283_418_Open_Image onclick="this.style.display='none'; Codehighlighter1_283_418_Open_Text.style.display='none'; Codehighlighter1_283_418_Closed_Image.style.display='inline'; Codehighlighter1_283_418_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_283_418_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_283_418_Closed_Text.style.display='none'; Codehighlighter1_283_418_Open_Image.style.display='inline'; Codehighlighter1_283_418_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_283_418_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_283_418_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head;;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(head);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">栈</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">team(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">head)<br><img id=Codehighlighter1_483_804_Open_Image onclick="this.style.display='none'; Codehighlighter1_483_804_Open_Text.style.display='none'; Codehighlighter1_483_804_Closed_Image.style.display='inline'; Codehighlighter1_483_804_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_483_804_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_483_804_Closed_Text.style.display='none'; Codehighlighter1_483_804_Open_Image.style.display='inline'; Codehighlighter1_483_804_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_483_804_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_483_804_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">tail,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(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">10</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_627_758_Open_Image onclick="this.style.display='none'; Codehighlighter1_627_758_Open_Text.style.display='none'; Codehighlighter1_627_758_Closed_Image.style.display='inline'; Codehighlighter1_627_758_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_627_758_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_627_758_Closed_Text.style.display='none'; Codehighlighter1_627_758_Open_Image.style.display='inline'; Codehighlighter1_627_758_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_627_758_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_627_758_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(head);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">队列</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;print_list(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">head)<br><img id=Codehighlighter1_846_1032_Open_Image onclick="this.style.display='none'; Codehighlighter1_846_1032_Open_Text.style.display='none'; Codehighlighter1_846_1032_Closed_Image.style.display='inline'; Codehighlighter1_846_1032_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_846_1032_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_846_1032_Closed_Text.style.display='none'; Codehighlighter1_846_1032_Open_Image.style.display='inline'; Codehighlighter1_846_1032_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_846_1032_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_846_1032_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">p;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Begin</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(p&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;NULL)<br><img id=Codehighlighter1_941_1005_Open_Image onclick="this.style.display='none'; Codehighlighter1_941_1005_Open_Text.style.display='none'; Codehighlighter1_941_1005_Closed_Image.style.display='inline'; Codehighlighter1_941_1005_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_941_1005_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_941_1005_Closed_Text.style.display='none'; Codehighlighter1_941_1005_Open_Image.style.display='inline'; Codehighlighter1_941_1005_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_941_1005_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_941_1005_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-&gt;%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">-&gt;End\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">链表输出</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;detele_x(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">head,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;x)<br><img id=Codehighlighter1_1080_1279_Open_Image onclick="this.style.display='none'; Codehighlighter1_1080_1279_Open_Text.style.display='none'; Codehighlighter1_1080_1279_Closed_Image.style.display='inline'; Codehighlighter1_1080_1279_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_1080_1279_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1080_1279_Closed_Text.style.display='none'; Codehighlighter1_1080_1279_Open_Image.style.display='inline'; Codehighlighter1_1080_1279_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_1080_1279_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1080_1279_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">p,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">q;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;x&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;NULL)<br><img id=Codehighlighter1_1193_1215_Open_Image onclick="this.style.display='none'; Codehighlighter1_1193_1215_Open_Text.style.display='none'; Codehighlighter1_1193_1215_Closed_Image.style.display='inline'; Codehighlighter1_1193_1215_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1193_1215_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1193_1215_Closed_Text.style.display='none'; Codehighlighter1_1193_1215_Open_Image.style.display='inline'; Codehighlighter1_1193_1215_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1193_1215_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1193_1215_Open_Text><span style="COLOR: #000000">{&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q;&nbsp;q&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;x)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(q);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">删除数据域为x的结点</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;add_x_y(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">head,</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)<br><img id=Codehighlighter1_1338_1608_Open_Image onclick="this.style.display='none'; Codehighlighter1_1338_1608_Open_Text.style.display='none'; Codehighlighter1_1338_1608_Closed_Image.style.display='inline'; Codehighlighter1_1338_1608_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_1338_1608_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1338_1608_Closed_Text.style.display='none'; Codehighlighter1_1338_1608_Open_Image.style.display='inline'; Codehighlighter1_1338_1608_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_1338_1608_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1338_1608_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">add_new,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">q;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add_new&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;node));<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add_new</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;x;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;head</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(q&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;NULL&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">data&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;y)<br><img id=Codehighlighter1_1536_1558_Open_Image onclick="this.style.display='none'; Codehighlighter1_1536_1558_Open_Text.style.display='none'; Codehighlighter1_1536_1558_Closed_Image.style.display='inline'; Codehighlighter1_1536_1558_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1536_1558_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1536_1558_Closed_Text.style.display='none'; Codehighlighter1_1536_1558_Open_Image.style.display='inline'; Codehighlighter1_1536_1558_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1536_1558_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1536_1558_Open_Text><span style="COLOR: #000000">{&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q;&nbsp;q&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;add_new;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add_new</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;q;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">在数据为y的结点前插入数据域为x的结点<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104944.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:22 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104944.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态规划随记</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104943.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:20:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104943.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104943.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104943.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104943.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104943.html</trackback:ping><description><![CDATA[<p><font face=宋体 size=3>//------------------------------------------------------------</font></p>
<p><font face=宋体 size=3>对于环形的动态规划，</font></p>
<p><font face=宋体 size=3>可以把数组下标复制一份然后进行动态规划</font></p>
<p><font face=宋体 size=3>//------------------------------------------------------------</font></p>
<p><span><font face=宋体 size=3>在动态规划的具体编程中，可能许多时候写出了正确的状态转移方程，但编写后的程序却得不到正确解，这很有可能是因为动态规划的循环的顺逆序，或者循环的内外层出现了错误。</font></span></p>
<p><span><font face=宋体 size=3>在这里忽略其他资料中的理论知识，而具体说动态规划是如何实现的。</font></span></p>
<p><font face=宋体><font size=3><span>//<span> </span></span><span>在这里的给出的全部代码都没有经过调试而直接写出</span></font></font></p>
<p><font face=宋体><font size=3><span>//<span> </span></span><span>如果出现错误，请指出</span></font></font></p>
<p><font face=宋体><font size=3><span>从</span><span>01</span><span>背包开始说起。</span></font></font></p>
<p><span><font face=宋体 size=3>稍微掌握动态规划的人都可以熟练地写出如下代码：</font></span></p>
<p>&nbsp;</p>
<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"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(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><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v;j</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">c[i];j</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;d[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(d[j],d[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">c[i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">w[i])；<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p>&nbsp;</p>
<p><span><font face=宋体 size=3>一般的动态规划都是先枚举状态再枚举决策，可能初学者不明白以上的代码是怎么回事，因为这是降维之后的状态表示，降维之前的是：</font></span></p>
<p><font face=宋体><font size=3><span>d[i][j]=max(d[i-1][j],d[i-1][j-c[i]]+w[i])</span><span>；</span></font></font></p>
<p><font face=宋体><font size=3><span>之所以可以降维是因为：观察状态转移方程，第</span><span>i</span><span>次的决策只与第</span><span>i-1</span><span>次所记录的状态有关，于是想，能不能有一种方法只用一维数组，就可以在状态转移能够正确进行？答案是肯定的，那就是第二层使用逆序循环。</span></font></font></p>
<p>&nbsp;</p>
<p><font face=宋体><font size=3><span>NOIp 2006 </span><span>金明的预算方案</span></font></font></p>
<p><span><font face=宋体 size=3>先处理一下给出的物品，分成若干组，转化为分组背包问题。</font></span></p>
<p><span><font face=宋体 size=3>写出分组背包的状态转移方程：</font></span></p>
<p><font face=宋体><font size=3><span>d[k][j]=max(d[k-1][j],d[k-1][j-c[k][i]]+w[k][i])</span><span>；</span></font></font></p>
<p><span><font face=宋体 size=3>对于分组背包，给出如下代码：</font></span></p>
<p><font face=宋体><font size=3><span>&nbsp;</p>
<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"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;所有组k<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&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">V&nbsp;to&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;所有物品i属于组k<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">max(d[j],d[j</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">c[k][i]]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">w[k][i])；<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<p></span></font></font>&nbsp;</p>
<p><font face=宋体><font size=3><span>//<span> </span></span><span>《背包问题九讲》中的代码似乎有点错误</span></font></font></p>
<p><font face=宋体><font size=3><span>同样，先观察状态转移方程，还是只取决于第</span><span>k-1</span><span>组记录的状态，又要使每组中物品只选一件或者一件都不选。这样就确定了分组背包只能是上面的循环次序，因为只有这样在第</span><span>k</span><span>组决策的时候才能保证从第</span><span>k-1</span><span>组的状态转移而来；简单地说就是，在第二层固定了前</span><span>k</span><span>组物品占用的空间，那么无论选择哪一个</span><span>i</span><span>，最终这个状态都只能被第</span><span>k</span><span>组的一个物品占用；试想如果像《背包问题九讲》中那样把第二层循环和第三层循环颠倒过来，那么就有可能第</span><span>k</span><span>层的状态由第</span><span>k</span><span>层的状态转移而来，从而不能保证最多选择一件物品。</span></font></font></p>
<p>&nbsp;</p>
<p><span><font face=宋体 size=3>Vijos GF</font></span></p>
<p><span><font face=宋体 size=3>二维费用的背包。</font></span></p>
<p><span><font face=宋体 size=3>状态转移方程直接给出如下：</font></span></p>
<p><font face=宋体><font size=3><span>d[i][j][k]=max(d[i-1][j][k],d[i-1][j-c1[i]][k-c2[i]]+w[i])</span><span>；</span></font></font></p>
<p><font face=宋体><font size=3><span>同样可以降到二维，因为是每个</span><span>GF</span><span>只能选择一次（题目中好像没有这么说，很久之前做的了，不太记得了，但是现实生活中的经验告诉我们每个</span><span>GF</span><span>只能选择一次），所以在循环枚举</span><span>j</span><span>和</span><span>k</span><span>的时候要使用逆序。</span></font></font></p>
<p>&nbsp;</p>
<p><font face=宋体><font size=3><span>Vijos </span><span>情敌</span></font></font></p>
<p><span><font face=宋体 size=3>当然可以理解为和&#8220;金明的预算方案&#8221;一样，是有依赖关系的背包问题，但是这道题却在数据的规模上告诉我们用那种方法是不可行的。更好的做法是搜索和动态规划结合，每次枚举超级情敌在第一个月消灭、第二个月消灭、第三个月消灭（即不消灭），然后对其余情敌进行动态规划。</font></span></p>
<p><span><font face=宋体 size=3>省略搜索部分，而着重说说动态规划。</font></span></p>
<p><span><font face=宋体 size=3>状态转移方程：</font></span></p>
<p><font face=宋体><font size=3><span>d[i][j][k]=max(d[i-1][j][k],d[i-1][j-c1[i]][k],d[i-1][j][k-c2[i]])</span><span>；</span></font></font></p>
<p><font face=宋体><font size=3><span>由于有可能某个超级情敌没有被消灭，而在枚举</span><span>i</span><span>的时候枚举了这个超级情敌，如果在这时直接判断后</span><span>continue</span><span>，可能造成</span><span>d[i]</span><span>层的空缺，以至于</span><span>d[i+1]</span><span>层的错误。所以在处理这样可能出现某一层出现空缺的情况时，要把空缺的那一层全部从它的上一层把状态继承下来，以保证后来的状态转移的正确。</span></font></font></p>
<p>&nbsp;</p>
<p><span><font face=宋体 size=3>总结一下。</font></span></p>
<p><span><font face=宋体 size=3>对于一道题目写出状态转移方程之后，程序的实现有时候是显而易见的，但有时候却又需要思考一番。这时候还是需要立足于状态转移方程，另外还要考虑到题目中的一些限制条件，设计合适的循环顺逆序和内外层。</font></span></p>
<p><font face=宋体 size=3>//------------------------------------------------------------</font></p>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104943.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:20 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104943.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>优化的Bellman-Ford——SPFA</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104942.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:17:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104942.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104942.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104942.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104942.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104942.html</trackback:ping><description><![CDATA[<p>SPFA的基本思路是：只有被修改过最短路的顶点，才有可能导致修改与它邻接的顶点的最短路。于是很容易想到用一个队列维护。<br>以下是我的代码：</p>
<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"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;min(a,b)&nbsp;(a&lt;b?a:b)</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;maxn</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">507</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;INF</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">100000007</span><span style="COLOR: #000000">;<br>typedef&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;front,rear,count,item[maxn];<br>}queue;<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;clear(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;empty(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(q.count</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;push(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;q.rear</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.item[q.rear]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>}<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;pop(queue&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">q)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">q.item[q.front];<br>&nbsp;&nbsp;&nbsp;&nbsp;q.front</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;q.count</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;x;<br>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Queue</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">typedef&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;EDGE_NODE<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;u,v,w;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;EDGE_NODE&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">next;<br>}edge_node;<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n,m,d[maxn];<br>edge_node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">a[maxn];<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Var</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;insert(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;begin,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;end,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;weight)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;edge_node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;edge_node;<br>&nbsp;&nbsp;&nbsp;&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">u</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">begin;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">v</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">end;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">weight;<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">a[begin]</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;<br>&nbsp;&nbsp;&nbsp;&nbsp;a[begin]</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p;<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;bellman_ford(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;s)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;in_num[maxn];<br>&nbsp;&nbsp;&nbsp;&nbsp;queue&nbsp;q;clear(q);<br>&nbsp;&nbsp;&nbsp;&nbsp;edge_node&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(i</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">s</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">:INF);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in_num[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;push(q,s);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[s]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;in_num[s]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">empty(q))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">pop(q);</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(p</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">a[i]</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next;p;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;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">v;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(d[i]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">d[j])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">d[i]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[j])<br>&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;push(q,j);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000">[j]</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in_num[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">(in_num[j]</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">n)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<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: #008000">//</span><span style="COLOR: #008000">*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.in</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdin);<br>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.out</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">*/</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">n,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">m);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;edge_node;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">next</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a,b,w;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">w);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(a,b,w);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insert(b,a,w);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(bellman_ford(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,d[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Error\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br></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></div>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104942.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:17 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104942.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>搜索算法小结</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104941.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:11:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104941.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104941.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104941.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104941.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104941.html</trackback:ping><description><![CDATA[<br>
<p><font face=宋体 size=3>最近一段时间仔细地学习了搜索方面的书籍，对搜索有了一些新的体会。搜索的算法有很多，从最简单的DFS、BFS，到稍稍有点优化的迭代加深、迭代加宽，再到A*、IDA*&#8230;&#8230;感觉对于搜索的学习和运用，绝对不能只局限于这些常用的方法中，而应该对各个方法有一个整体的把握，分析各种方法的优点和缺点，学会比较各种方法的时空性能，针对题目的特点入手。</font></p>
<p><font face=宋体 size=3><strong>DFS</strong></font></p>
<p><font face=宋体 size=3>最常用的一种搜索方法，几乎学过搜索的都会。适用于搜索的深度已知，比较适合搜索全部解。常用的优化方法是在结点扩展时加入剪枝策略。</font></p>
<p><font face=宋体 size=3><strong>BFS</strong></font></p>
<p><font face=宋体 size=3>相对与DFS可能用得少了点，但也是必会的基本方法。如果搜索的深度不知，DFS可能陷入死循环，同时，如果空间上可以承受，这时候可以考虑BFS。侧重寻求最优解。</font></p>
<p><font face=宋体 size=3><strong>双向广度优先搜索</strong></font></p>
<p><font face=宋体 size=3>适用于知道目标状态，每次扩展两个结点，但不一定非要是交替扩展，扩展方式很多。如果目标状态始终不变，而有多个初始状态，就可以&#8220;周界搜索&#8221;。要注意的是，如何判断已经扩展结点是否在另一端的方法，是需要认真考虑，选取最优判断方法的。</font></p>
<p><font face=宋体 size=3><strong>迭代加深搜索 ID</strong></font></p>
<p><font face=宋体 size=3>每次限制搜索的深度，找到解就停止，否则加大深度再次搜索。相对于DFS不会陷入死循环，相对于BFS不会在空间上有压力，也可以用来寻求最优解，不过缺点是重复搜索。</font></p>
<p><font face=宋体 size=3><strong>迭代加宽搜索 IB</strong></font></p>
<p><font face=宋体 size=3>没有太多接触，无视&#8230;&#8230;</font></p>
<p><font face=宋体 size=3><strong>A*</strong></font></p>
<p><font face=宋体 size=3>按f(s)的值扩展结点，是一种启发式搜索。需要两个表，每次判断是否在表1中、是否在表二中、同时在表一表二中，判断f(s)和f(s')的大小，选择是否替换。其中f(s)=g(s)+h(s)，h(s)为估价函数。要求h函数相容，对于这种说法，我自己的理解就是，状态每转移一次，h减少量最多为1。A*算法求得的第一个解必是最优解。缺点依然是空间需求太大。</font></p>
<p><font face=宋体 size=3><strong>IDA*</strong></font></p>
<p><font face=宋体 size=3>迭代加深的A*。每次搜索加上一个深度限制，扩展的时候判断最好情况是否会超过深度，也算是一种极端法的剪枝思想。适用与深度不定，最优解的深度又不一定很深，而状态转移的方式又有很多，使得状态空间无法承受。</font></p>
<p><font face=宋体 size=3>关于<strong>剪枝</strong>：1、可行性剪枝 2、最优性剪枝</font></p>
<p><font face=宋体 size=3>具体操作时：1、极端法 2、数学方法</font></p>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104941.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:11 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104941.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>并查集常用操作</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104939.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 10:05:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104939.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104939.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104939.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104939.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104939.html</trackback:ping><description><![CDATA[<p><font face=宋体 size=3>并查集（Union-Find Sets）的两种功能：合并两个集合；查找一个元素属于哪个集合。</font></p>
<p><font face=宋体 size=3>并查集的两个优化：基于rank的启发式合并；路径压缩。</font></p>
<p><font face=宋体 size=3>以下是我的代码</font><span style="COLOR: #000000">:</span><br></p>
<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"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;maxn</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10008</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;n,f[maxn],rank[maxn];<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Init()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;Getf(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(f[x]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">x)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;x;<br>&nbsp;&nbsp;&nbsp;&nbsp;f[x]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Getf(f[x]);</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;路径压缩&nbsp;</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;f[x];<br>}<br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;Same(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;(Getf(x)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">Getf(y));<br>}<br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;Union(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;x,</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;y)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;fx</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Getf(x),fy</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">Getf(y);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(fx</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">fy)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(rank[fx]</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">rank[fy])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[fx]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fy;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank[fy]</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(rank[fx]</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">rank[fy])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[fx]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fy;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;f[fy]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fx;<br>&nbsp;&nbsp;&nbsp;&nbsp;}</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;启发式合并&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">}<br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;Init();<br></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></div>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104939.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 18:05 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104939.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>拓扑排序(topo sort)</title><link>http://www.cppblog.com/rakerichard/archive/2010/01/06/104935.html</link><dc:creator>lee1r</dc:creator><author>lee1r</author><pubDate>Wed, 06 Jan 2010 09:49:00 GMT</pubDate><guid>http://www.cppblog.com/rakerichard/archive/2010/01/06/104935.html</guid><wfw:comment>http://www.cppblog.com/rakerichard/comments/104935.html</wfw:comment><comments>http://www.cppblog.com/rakerichard/archive/2010/01/06/104935.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rakerichard/comments/commentRss/104935.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rakerichard/services/trackbacks/104935.html</trackback:ping><description><![CDATA[<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"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;maxv</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">108</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;v,e,count,a[maxv],used[maxv];<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;g[maxv][maxv],ans;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;init()<br><img id=Codehighlighter1_110_325_Open_Image onclick="this.style.display='none'; Codehighlighter1_110_325_Open_Text.style.display='none'; Codehighlighter1_110_325_Closed_Image.style.display='inline'; Codehighlighter1_110_325_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_110_325_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_110_325_Closed_Text.style.display='none'; Codehighlighter1_110_325_Open_Image.style.display='inline'; Codehighlighter1_110_325_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_110_325_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_110_325_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">v,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">e);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[i][j]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">e;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_249_323_Open_Image onclick="this.style.display='none'; Codehighlighter1_249_323_Open_Text.style.display='none'; Codehighlighter1_249_323_Closed_Image.style.display='inline'; Codehighlighter1_249_323_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_249_323_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_249_323_Closed_Text.style.display='none'; Codehighlighter1_249_323_Open_Image.style.display='inline'; Codehighlighter1_249_323_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_249_323_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_249_323_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;a,b;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">a,</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">b);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[a][b]</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;dfs(</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;now)<br><img id=Codehighlighter1_346_629_Open_Image onclick="this.style.display='none'; Codehighlighter1_346_629_Open_Text.style.display='none'; Codehighlighter1_346_629_Closed_Image.style.display='inline'; Codehighlighter1_346_629_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_346_629_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_346_629_Closed_Text.style.display='none'; Codehighlighter1_346_629_Open_Image.style.display='inline'; Codehighlighter1_346_629_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_346_629_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_346_629_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">ans)&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;used[now]</span><span style="COLOR: #000000">=-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(g[now][i])<br><img id=Codehighlighter1_440_584_Open_Image onclick="this.style.display='none'; Codehighlighter1_440_584_Open_Text.style.display='none'; Codehighlighter1_440_584_Closed_Image.style.display='inline'; Codehighlighter1_440_584_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_440_584_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_440_584_Closed_Text.style.display='none'; Codehighlighter1_440_584_Open_Image.style.display='inline'; Codehighlighter1_440_584_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_440_584_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_440_584_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(used[i]</span><span style="COLOR: #000000">==-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_476_530_Open_Image onclick="this.style.display='none'; Codehighlighter1_476_530_Open_Text.style.display='none'; Codehighlighter1_476_530_Closed_Image.style.display='inline'; Codehighlighter1_476_530_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_476_530_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_476_530_Closed_Text.style.display='none'; Codehighlighter1_476_530_Open_Image.style.display='inline'; Codehighlighter1_476_530_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_476_530_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_476_530_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&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">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">used[i])<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(i);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;a[count]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">now;count</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;used[now]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;toposort()<br><img id=Codehighlighter1_647_1279_Open_Image onclick="this.style.display='none'; Codehighlighter1_647_1279_Open_Text.style.display='none'; Codehighlighter1_647_1279_Closed_Image.style.display='inline'; Codehighlighter1_647_1279_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_647_1279_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_647_1279_Closed_Text.style.display='none'; Codehighlighter1_647_1279_Open_Image.style.display='inline'; Codehighlighter1_647_1279_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_647_1279_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_647_1279_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;begin;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_716_838_Open_Image onclick="this.style.display='none'; Codehighlighter1_716_838_Open_Text.style.display='none'; Codehighlighter1_716_838_Closed_Image.style.display='inline'; Codehighlighter1_716_838_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_716_838_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_716_838_Closed_Text.style.display='none'; Codehighlighter1_716_838_Open_Image.style.display='inline'; Codehighlighter1_716_838_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_716_838_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_716_838_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(g[j][i])<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Find&nbsp;a&nbsp;Vertex&nbsp;to&nbsp;Start</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans)<br><img id=Codehighlighter1_887_978_Open_Image onclick="this.style.display='none'; Codehighlighter1_887_978_Open_Text.style.display='none'; Codehighlighter1_887_978_Closed_Image.style.display='inline'; Codehighlighter1_887_978_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_887_978_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_887_978_Closed_Text.style.display='none'; Codehighlighter1_887_978_Open_Image.style.display='inline'; Codehighlighter1_887_978_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_887_978_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_887_978_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;used[i]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">v;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dfs(begin);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Topo_Sort</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(ans)<br><img id=Codehighlighter1_1014_1207_Open_Image onclick="this.style.display='none'; Codehighlighter1_1014_1207_Open_Text.style.display='none'; Codehighlighter1_1014_1207_Closed_Image.style.display='inline'; Codehighlighter1_1014_1207_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1014_1207_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1014_1207_Closed_Text.style.display='none'; Codehighlighter1_1014_1207_Open_Image.style.display='inline'; Codehighlighter1_1014_1207_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1014_1207_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1014_1207_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;first</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">long</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">v;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img id=Codehighlighter1_1077_1178_Open_Image onclick="this.style.display='none'; Codehighlighter1_1077_1178_Open_Text.style.display='none'; Codehighlighter1_1077_1178_Closed_Image.style.display='inline'; Codehighlighter1_1077_1178_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1077_1178_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1077_1178_Closed_Text.style.display='none'; Codehighlighter1_1077_1178_Open_Image.style.display='inline'; Codehighlighter1_1077_1178_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1077_1178_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1077_1178_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(first)&nbsp;first</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;putchar(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%ld</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,a[i]);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No&nbsp;answer.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;Write&nbsp;down&nbsp;the&nbsp;Answer</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img id=Codehighlighter1_1292_1419_Open_Image onclick="this.style.display='none'; Codehighlighter1_1292_1419_Open_Text.style.display='none'; Codehighlighter1_1292_1419_Closed_Image.style.display='inline'; Codehighlighter1_1292_1419_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_1292_1419_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1292_1419_Closed_Text.style.display='none'; Codehighlighter1_1292_1419_Open_Image.style.display='inline'; Codehighlighter1_1292_1419_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_1292_1419_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1292_1419_Open_Text><span style="COLOR: #000000">{<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">*</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.in</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdin);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;freopen(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">data.out</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,stdout);<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">*/</span><span style="COLOR: #008000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;init();<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;toposort();<br><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></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 src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/rakerichard/aggbug/104935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rakerichard/" target="_blank">lee1r</a> 2010-01-06 17:49 <a href="http://www.cppblog.com/rakerichard/archive/2010/01/06/104935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>