<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Architecting Life &#187; Algorithm</title>
	<atom:link href="http://www.xujiwei.com/blog/posts/develop/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xujiwei.com/blog</link>
	<description>Just do it</description>
	<lastBuildDate>Wed, 21 Jul 2010 05:56:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel='hub' href='http://www.xujiwei.com/blog/?pushpress=hub'/>
		<item>
		<title>[C] 贪心算法</title>
		<link>http://www.xujiwei.com/blog/2005/12/10/c-greedy/</link>
		<comments>http://www.xujiwei.com/blog/2005/12/10/c-greedy/#comments</comments>
		<pubDate>Sat, 10 Dec 2005 08:48:06 +0000</pubDate>
		<dc:creator>Xu Jiwei</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Greedy]]></category>

		<guid isPermaLink="false">http://tmp.xujiwei.com/blog/?p=126</guid>
		<description><![CDATA[去图书馆借了本算法与数据结构，第一节里有个贪心算法，回寝室便试了下，不想碰到了一些问题。 例1.1 快速送达疫苗 已 知有邻近的五个村子发生了疫情，见图1-1。我们要用汽车快速地将疫苗送达到5个村子，目标是寻找一条耗时最少的路线。 画图不方便就不放 了：）代码里s数组存放的就是各个村子之间的距离。 我用C写的代码如下： #include &#60;stdio.h&#62; int s[5][5]={ {0,1,2,7,5}, {1,0,4,4,3}, {2,4,0,1,2}, {7,4,1,0,3}, {5,3,2,3,0}, }; int visited[5]={0}; int mindis(int start) { int i; int d=10; int n; for(i=0;i&#60;5;i++) { if(visited[i]==0&#38;&#38;s[start][i]&#60;d) { d=s[start][i]; n=i; } } visited[n]=1; return n; } void greedy(int start) { int cost; int i; int n; int ss; ss=start-1; printf(&#8220;%d&#8221;,start); <a href="http://www.xujiwei.com/blog/2005/12/10/c-greedy/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>去图书馆借了本算法与数据结构，第一节里有个贪心算法，回寝室便试了下，不想碰到了一些问题。</p>
<p>例1.1 快速送达疫苗</p>
<p>已 知有邻近的五个村子发生了疫情，见图1-1。我们要用汽车快速地将疫苗送达到5个村子，目标是寻找一条耗时最少的路线。</p>
<p>画图不方便就不放 了：）代码里s数组存放的就是各个村子之间的距离。</p>
<p>我用C写的代码如下：</p>
<blockquote><p><span style="font-style: normal;">#include &lt;stdio.h&gt;</span></p>
<p><span style="font-style: normal;">int s[5][5]={</span></p>
<p><span style="font-style: normal;"> {0,1,2,7,5},</span></p>
<p><span style="font-style: normal;"> {1,0,4,4,3},</span></p>
<p><span style="font-style: normal;"> {2,4,0,1,2},</span></p>
<p><span style="font-style: normal;"> {7,4,1,0,3},</span></p>
<p><span style="font-style: normal;"> {5,3,2,3,0},</span></p>
<p><span style="font-style: normal;">};</span></p>
<p><span style="font-style: normal;">int visited[5]={0};</span></p>
<p><span style="font-style: normal;">int mindis(int start)</span></p>
<p><span style="font-style: normal;">{</span></p>
<p><span style="font-style: normal;"> int i;</span></p>
<p><span style="font-style: normal;"> int d=10;</span></p>
<p><span style="font-style: normal;"> int n;</span></p>
<p><span style="font-style: normal;"> for(i=0;i&lt;5;i++)</span></p>
<p><span style="font-style: normal;"> {</span></p>
<p><span style="font-style: normal;"> if(visited[i]==0&amp;&amp;s[start][i]&lt;d)</span></p>
<p><span style="font-style: normal;"> {</span></p>
<p><span style="font-style: normal;"> d=s[start][i];</span></p>
<p><span style="font-style: normal;"> n=i;</span></p>
<p><span style="font-style: normal;"> }</span></p>
<p><span style="font-style: normal;"> }</span></p>
<p><span style="font-style: normal;"> visited[n]=1;</span></p>
<p><span style="font-style: normal;"> return n;</span></p>
<p><span style="font-style: normal;">}</span></p>
<p><span style="font-style: normal;">void greedy(int start)</span></p>
<p><span style="font-style: normal;">{</span></p>
<p><span style="font-style: normal;"> int cost;</span></p>
<p><span style="font-style: normal;"> int i;</span></p>
<p><span style="font-style: normal;"> int n;</span></p>
<p><span style="font-style: normal;"> int ss;</span></p>
<p><span style="font-style: normal;"> ss=start-1;</span></p>
<p><span style="font-style: normal;"> printf(&#8220;%d&#8221;,start);</span></p>
<p><span style="font-style: normal;"> visited[ss]=1;</span></p>
<p><span style="font-style: normal;"> for(i=0;i&lt;4;i++)</span></p>
<p><span style="font-style: normal;"> {</span></p>
<p><span style="font-style: normal;"> n=mindis(ss);</span></p>
<p><span style="font-style: normal;"> cost+=s[ss][n];</span></p>
<p><span style="font-style: normal;"> printf(&#8220;-%d&#8221;,n+1);</span></p>
<p><span style="font-style: normal;"> ss=n;</span></p>
<p><span style="font-style: normal;"> }</span></p>
<p><span style="font-style: normal;"> cost+=s[ss][start-1];</span></p>
<p><span style="font-style: normal;"> printf(&#8220;-%d\ncost is:%d\n&#8221;,start,cost);</span></p>
<p><span style="font-style: normal;">}</span></p>
<p><span style="font-style: normal;">int main()</span></p>
<p><span style="font-style: normal;">{</span></p>
<p><span style="font-style: normal;"> greedy(1);</span></p>
<p><span style="font-style: normal;"> return 0;</span></p>
<p><span style="font-style: normal;">}</span></p></blockquote>
<p>在 写时，调试了好几次，再查了一下书才真正搞定。</p>
<p>1. 数组的初始化。</p>
<p>在初始化村子距离数组时，我先用的是这样语句：</p>
<p>int s[5][5]={</p>
<p>(0,1,2,7,5),</p>
<p>(1,0,4,4,3),</p>
<p>(2,4,0,1,2),</p>
<p>(7,4,1,0,3),</p>
<p>(5,3,2,3,0),</p>
<p>};</p>
<p>结 果弄了半天得不到正确结果，把小括号改成花括号后才解决了问题。数组初始化时用花括号是按行初始化。</p>
<p>2. 数组下标起始值</p>
<p>数 组的下标起始值应为0，在开始时我却没有想到，在mail()里传过来的1就直接用进去了，结果是输出里有一个超大的值。</p>
<p>当然了，我写的这 个并不好，以上的问题也只是我个人的见解，如果有什么错误还望指点一二：）</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xujiwei.com/blog/2005/12/10/c-greedy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
