锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品久久久久a影院,久久精品国产久精国产思思,亚洲性久久久影院http://www.shnenglu.com/lvlawliet/VIMzh-cnThu, 08 May 2025 21:09:13 GMTThu, 08 May 2025 21:09:13 GMT60JOJ1043錛欰tlantis 紱繪暎鍖?鎵弿綰?/title><link>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159096.html</link><dc:creator>LLawliet</dc:creator><author>LLawliet</author><pubDate>Tue, 25 Oct 2011 15:52:00 GMT</pubDate><guid>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159096.html</guid><wfw:comment>http://www.shnenglu.com/lvlawliet/comments/159096.html</wfw:comment><comments>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159096.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/lvlawliet/comments/commentRss/159096.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/lvlawliet/services/trackbacks/159096.html</trackback:ping><description><![CDATA[<span id="hnj19dr" class="Apple-style-span" style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: normal; background-color: #ffffff; "><h2 style="font-family: Arial, Helvetica, sans-serif; font-size: 24px; font-style: normal; line-height: normal; font-weight: bold; "><img src="http://acm.jlu.edu.cn/joj/images/art.gif" width="38" height="38" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /> <a style="color: #0055ff; ">1043</a>: Atlantis</h2><hr style="height: 1px; " /><table width="90%" border="1" cellspacing="3" cellpadding="3" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; margin-top: 2px; margin-right: auto; margin-bottom: 2px; margin-left: auto; "><colgroup span="7" style="text-align: center; color: red; "></colgroup><tbody><tr style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; "><th width="10%" height="30" style="background-color: #ffee4f; padding-right: 5px; ">Result</th><th width="20%" style="background-color: #ffee4f; padding-right: 5px; ">TIME Limit</th><th width="25%" style="background-color: #ffee4f; padding-right: 5px; ">MEMORY Limit</th><th width="20%" style="background-color: #ffee4f; padding-right: 5px; ">Run Times</th><th width="20%" style="background-color: #ffee4f; padding-right: 5px; ">AC Times</th><th width="20%" style="background-color: #ffee4f; padding-right: 5px; ">JUDGE</th></tr><tr style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; "><td height="30" align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; "><span id="probinfo_placeholder"><img src="http://acm.jlu.edu.cn/joj/images/ok1.gif" width="20" height="20" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " alt="" /></span></td><td align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; ">3s</td><td align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; ">8192K</td><td align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; ">431</td><td align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; ">155</td><td align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; line-height: normal; color: #000000; ">Standard</td></tr></tbody></table><div id="1hz9dfl" class="prob_text"><p>There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.</p><strong><h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; font-style: normal; line-height: normal; font-weight: bold; ">Input</h3></strong><p>The input file consists of several test cases. Each test case starts with a line containing a single integer n(1 < n < 100) of available maps. The n following lines describe one map each. Each of these lines containsfour numbers x1;y1;x2;y2 (0 < x1 < x2 < 100000;0 < y1 < y2 < 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.The input file is terminated by a line containing a single 0. Don’t process it1.</p><strong><h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; font-style: normal; line-height: normal; font-weight: bold; ">Output</h3></strong><p>For each test case, your program should output one section. The first line of each section must be “Testcase #k”, where k is the number of the test case (starting with 1). The second one must be   “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.</p><p>Output a blank line after each test case.</p><h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; font-style: normal; line-height: normal; font-weight: bold; ">Sample Input</h3><pre style="font-size: 12pt; font-family: 'Courier New', Courier; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: red; border-right-color: red; border-bottom-color: red; border-left-color: red; background-color: #eee8aa; ">2 10 10 20 20 15 15 25 25.5 0 </pre><h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; font-style: normal; line-height: normal; font-weight: bold; ">Sample Output</h3><pre style="font-size: 12pt; font-family: 'Courier New', Courier; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: red; border-right-color: red; border-bottom-color: red; border-left-color: red; background-color: #eee8aa; ">Test case #1 Total explored area: 180.00<br /></pre><div><br /><br /></div></div></span><span id="1jl1zlx" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">棰樼洰鐨勬剰鎬濇槸緇欏畾n涓煩褰㈢殑2n涓潗鏍囷紝姹傜煩褰㈢殑瑕嗙洊闈㈢Н銆傚鏋滃紑涓涓ぇ鐨刡ool鏁扮粍錛屽皢瑕嗙洊榪囩殑閮ㄥ垎鏇存柊涓簍rue錛屽啀浠庡ご鍒板熬鎵弿涓閬嶏紝鍦ㄥ潗鏍囪寖鍥存瘮杈冨皬鐨勬儏鍐典笅錛屽彲浠ユ眰瑙c備絾鏄鏋滃潗鏍噚,y鐨勫彇鍊艱寖鍥村緢澶э紝姣斿[-10^8,10^8]錛岀敤涓婇潰榪欎釜鏂規硶灝變笉鑳芥眰瑙d簡錛涜屼笖鍧愭爣榪樻湁鍙兘鏄疄鏁幫紝涓婇潰鐨勬柟娉曞氨鏇村姞涓嶅彲琛屼簡錛岄渶瑕佸鎵句竴縐嶆柊鐨勮В娉曪紝灝辨槸涓嬮潰瑕佽鍒扮殑“紱繪暎鍖?#8221;銆?br /></span><span id="3xx13x9" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    娉ㄦ剰鍒拌琛ㄧず涓涓煩褰紝鍙渶瑕佺煡閬撳叾2涓《鐐圭殑鍧愭爣灝卞彲浠ヤ簡(鏈宸︿笅錛屾渶鍙充笂)銆傚彲浠ョ敤2涓暟緇剎[0...2n-1]錛寉[0...2n-1]璁板綍涓嬬煩褰i鐨?涓潗鏍?x1,y1)錛?x2,y2)錛岀劧鍚庡皢鏁扮粍x[0...xn-1]錛寉[0...2n-1]鎺掑簭錛屼負涓嬩竴姝ョ殑鎵弿綰夸綔鍑嗗錛岃繖灝辨槸紱繪暎鍖栫殑鎬濇兂銆傝繖棰樿繕鍙互鐢ㄧ嚎孌墊爲鍋氳繘涓姝ヤ紭鍖栵紝浣嗘槸榪欓噷鍙粙緇嶇鏁e寲鐨勬濇兂銆?br /></span><span id="t73l39f" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; color: #000000; background-color: #ffffff; ">    鐪嬩笅闈㈣繖涓緥瀛愶細鏈?涓煩褰?1,1)錛?3,3)鍜?2,2)錛?4,4)銆傚鍥撅細<br /><div align="center"><img height="227" alt="" src="http://www.shnenglu.com/images/cppblog_com/mythit/R1.jpg" width="265" border="0" /></div></span><span id="9jt5zf3" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    鍥句腑铏氱嚎琛ㄧず鎵弿綰匡紝涓嬩竴姝ュ伐浣滃彧闇瑕佸皢榪?涓煩褰㈣鐩栬繃鐨勯儴鍒嗙殑bool鏁扮粍鐨勫搴斾綅緗洿鏂頒負true錛屾帴涓嬪幓鐢ㄦ壂鎻忕嚎浠庡乏鍒板彸錛屼粠涓婂埌涓嬫壂鎻忎竴閬嶏紝灝卞彲浠ユ眰鍑虹煩褰㈣鐩栫殑鎬婚潰縐?br /></span><span id="5tl1t71" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    榪欎釜鍥懼搴旂殑bool鏁扮粍鐨勫煎涓嬶細<br /></span><span id="jpzl7xj" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    1 1 0                       1 2 3<br /></span><span id="h3ptxln" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    1 1 1       <---->       4 5 6<br /></span><span id="9ldnvtl" class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; color: #000000; ">    0 1 1                       7 8 9</span><span id="fr51tbr" class="Apple-style-span" style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: normal; background-color: #ffffff; "><div id="v13z5x9" class="prob_text"><div><br />浠g爜錛?/div></div></span><br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">#include</span><span style="color: #000000; "><</span><span style="color: #000000; ">stdio.h</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />#include</span><span style="color: #000000; "><</span><span style="color: #000000; ">iostream</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />#include</span><span style="color: #000000; "><</span><span style="color: #000000; ">algorithm</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">#define</span><span style="color: #000000; "> MAXN 201</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; "> std;<br /><br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Node<br />{<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> l, r;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">綰挎鏍戠殑宸﹀彸鏁寸偣</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> c;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">c鐢ㄦ潵璁板綍閲嶅彔鎯呭喌</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">double</span><span style="color: #000000; "> cnt, lf, rf;</span><span style="color: #008000; ">//</span><span style="color: #008000; "><br />    </span><span style="color: #008000; ">//</span><span style="color: #008000; ">cnt鐢ㄦ潵璁$畻瀹炲湪鐨勯暱搴︼紝rf,lf鍒嗗埆鏄搴旂殑宸﹀彸鐪熷疄鐨勬誕鐐規暟绔偣</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">} segTree[MAXN </span><span style="color: #000000; ">*</span><span style="color: #000000; "> </span><span style="color: #000000; ">3</span><span style="color: #000000; ">];<br /></span><span style="color: #0000FF; ">struct</span><span style="color: #000000; "> Line<br />{<br />    </span><span style="color: #0000FF; ">double</span><span style="color: #000000; "> x, y1, y2;<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> f;<br />} line[MAXN];<br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">鎶婁竴孌墊騫寵浜巠杞寸殑綰挎琛ㄧず鎴愭暟緇?nbsp;錛?br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">x鏄嚎孌電殑x鍧愭爣錛寉1,y2綰挎瀵瑰簲鐨勪笅绔偣鍜屼笂绔偣鐨勫潗鏍?br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">涓涓煩褰?nbsp;錛屽乏杈圭殑閭f潯杈筬涓?錛屽彸杈圭殑涓?1錛?br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">鐢ㄦ潵璁板綍閲嶅彔鎯呭喌錛屽彲浠ユ牴鎹繖涓潵璁$畻錛宯od鑺傜偣涓殑c</span><span style="color: #008000; "><br /></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">bool</span><span style="color: #000000; "> cmp(Line a,Line b)</span><span style="color: #008000; ">//</span><span style="color: #008000; ">sort鎺掑簭鐨勫嚱鏁?/span><span style="color: #008000; "><br /></span><span style="color: #000000; ">{<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> a.x </span><span style="color: #000000; "><</span><span style="color: #000000; "> b.x;<br />}<br /><br /></span><span style="color: #0000FF; ">double</span><span style="color: #000000; "> y[MAXN];</span><span style="color: #008000; ">//</span><span style="color: #008000; ">璁板綍y鍧愭爣鐨勬暟緇?/span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> Build(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> t,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> l,</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> r)</span><span style="color: #008000; ">//</span><span style="color: #008000; ">鏋勯犵嚎孌墊爲</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">{<br />    segTree[t].l </span><span style="color: #000000; ">=</span><span style="color: #000000; "> l;<br />    segTree[t].r </span><span style="color: #000000; ">=</span><span style="color: #000000; "> r;<br />    segTree[t].cnt </span><span style="color: #000000; ">=</span><span style="color: #000000; "> segTree[t].c </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />    segTree[t].lf </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y[l];<br />    segTree[t].rf </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y[r];<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (l </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">==</span><span style="color: #000000; "> r) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> mid </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (l </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; ">1</span><span style="color: #000000; ">;<br />    Build(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, l, mid);<br />    Build(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, mid, r);</span><span style="color: #008000; ">//</span><span style="color: #008000; ">閫掑綊鏋勯?/span><span style="color: #008000; "><br /></span><span style="color: #000000; ">}<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> calen(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> t)</span><span style="color: #008000; ">//</span><span style="color: #008000; ">璁$畻闀垮害</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">{<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (segTree[t].c </span><span style="color: #000000; ">></span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">)<br />    {<br />        segTree[t].cnt </span><span style="color: #000000; ">=</span><span style="color: #000000; "> segTree[t].rf </span><span style="color: #000000; ">-</span><span style="color: #000000; "> segTree[t].lf;<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    }<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (segTree[t].l </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">==</span><span style="color: #000000; "> segTree[t].r)<br />        segTree[t].cnt </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br />        segTree[t].cnt </span><span style="color: #000000; ">=</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].cnt </span><span style="color: #000000; ">+</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].cnt;<br />}<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> update(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> t, Line e)</span><span style="color: #008000; ">//</span><span style="color: #008000; ">鍔犲叆綰挎e錛屽悗鏇存柊綰挎鏍?/span><span style="color: #008000; "><br /></span><span style="color: #000000; ">{<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (e.y1 </span><span style="color: #000000; ">==</span><span style="color: #000000; "> segTree[t].lf </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> e.y2 </span><span style="color: #000000; ">==</span><span style="color: #000000; "> segTree[t].rf)<br />    {<br />        segTree[t].c </span><span style="color: #000000; ">+=</span><span style="color: #000000; "> e.f;<br />        calen(t);<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    }<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (e.y2 </span><span style="color: #000000; "><=</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].rf)<br />        update(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, e);<br />    </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(e.y1 </span><span style="color: #000000; ">>=</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].lf)<br />        update(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, e);<br />    </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br />    {<br />        Line tmp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> e;<br />        tmp.y2 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].rf;<br />        update(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, tmp);<br />        tmp </span><span style="color: #000000; ">=</span><span style="color: #000000; "> e;<br />        tmp.y1 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> segTree[t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].lf;<br />        update(t </span><span style="color: #000000; "><<</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">|</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, tmp);<br />    }<br />    calen(t);<br />}<br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> main()<br />{<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i, n, t, iCase </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">double</span><span style="color: #000000; "> x1, y1, x2, y2;<br />    </span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">n) </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> n)<br />    {<br />        iCase</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;<br />        t </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(i </span><span style="color: #000000; ">=</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; "> n; i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />        {<br />            scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%lf%lf%lf%lf</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">x1, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">y1, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">x2, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">y2);<br />            line[t].x </span><span style="color: #000000; ">=</span><span style="color: #000000; "> x1;<br />            line[t].y1 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y1;<br />            line[t].y2 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y2;<br />            line[t].f </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />            y[t] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y1;<br />            t</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;<br />            line[t].x </span><span style="color: #000000; ">=</span><span style="color: #000000; "> x2;<br />            line[t].y1 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y1;<br />            line[t].y2 </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y2;<br />            line[t].f </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />            y[t] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> y2;<br />            t</span><span style="color: #000000; ">++</span><span style="color: #000000; ">;<br />        }<br />        sort(line </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, line </span><span style="color: #000000; ">+</span><span style="color: #000000; "> t, cmp);<br />        sort(y </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">, y </span><span style="color: #000000; ">+</span><span style="color: #000000; "> t);<br />        Build(</span><span style="color: #000000; ">1</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; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">);<br />        update(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">, line[</span><span style="color: #000000; ">1</span><span style="color: #000000; ">]);<br />        </span><span style="color: #0000FF; ">double</span><span style="color: #000000; "> res </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> t; i</span><span style="color: #000000; ">++</span><span style="color: #000000; ">)<br />        {<br />            res </span><span style="color: #000000; ">+=</span><span style="color: #000000; "> segTree[</span><span style="color: #000000; ">1</span><span style="color: #000000; ">].cnt </span><span style="color: #000000; ">*</span><span style="color: #000000; "> (line[i].x </span><span style="color: #000000; ">-</span><span style="color: #000000; "> line[i </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">].x);<br />            update(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">, line[i]);<br />        }<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Test case #%d\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, iCase);<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">Total explored area: %.2lf\n\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, res);<br />    }<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />}<br /></span></div><img src ="http://www.shnenglu.com/lvlawliet/aggbug/159096.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/lvlawliet/" target="_blank">LLawliet</a> 2011-10-25 23:52 <a href="http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159096.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>JOJ1040錛歍rees錛堝崱鐗瑰叞鏁?閫掑綊錛?/title><link>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159082.html</link><dc:creator>LLawliet</dc:creator><author>LLawliet</author><pubDate>Tue, 25 Oct 2011 12:55:00 GMT</pubDate><guid>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159082.html</guid><wfw:comment>http://www.shnenglu.com/lvlawliet/comments/159082.html</wfw:comment><comments>http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/lvlawliet/comments/commentRss/159082.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/lvlawliet/services/trackbacks/159082.html</trackback:ping><description><![CDATA[<div>We can number binary trees using the following scheme: <p>The empty tree is numbered 0.<br /> The single-node tree is numbered 1.<br /> All binary trees having m nodes have numbers less than all those having m+1 nodes.<br /> Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either<br /><br />   Left subtrees numbered higher than L, or<br />   A left subtree = L and a right subtree numbered higher than R.</p> <p>The first 10 binary trees and tree number 20 in this sequence are shown below:</p> <p align="center"><img src="http://192.168.250.250/joj/images/problems/1040.gif" height="138" width="581" alt="" /></p> <p>Your job for this problem is to output a binary tree when given its order number.<br /> <br /> </p> <h3>Input</h3> <p>Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)<br /> <br /> </p> <h3>Output</h3> <p>For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme:</p> <p>A tree with no children should be output as X.<br /> A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R.<br />   If L is empty, just output X(R').<br />   If R is empty, just output (L')X.<br /> <br /> </p> <h3>Sample Input</h3> <pre>1 <br />20 <br />31117532 <br />0 </pre> <h3>Sample Output</h3> <pre>X <br />((X)X(X))X<br />(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X) </pre></div><br /><br />鎬濊礬錛?br />a鏁扮粍琛ㄧず鑺傜偣鏁頒負j鎵鑳借〃紺烘渶澶х殑鏁般?br />鍒欑j涓妭鐐規墍鑳借〃紺虹殑鏁癮[j]絎﹀悎鍗$壒鍏版暟錛?br />a[j] = a[0] * a[j - 1] + a[1] * a[j - 2] + ...... + a[j - 1] * a[0];<br />琛ㄧず錛氭湁j涓妭鐐?= 宸﹁竟0涓妭鐐圭殑涓暟 * 鍙寵竟j - 1涓妭鐐圭殑涓暟 + ...... + 宸﹁竟j - 1涓妭鐐圭殑涓暟 * 鍙寵竟0涓妭鐐圭殑涓暟銆?br /><br />涔嬪悗鏍規嵁璇誨叆鐨刵錛屽垽鏂嚭鑺傜偣鏁幫紝鍦ㄥ啀鍒ゆ柇鍑哄乏鍙崇殑鑺傜偣鏁板拰宸﹀彸鎵浠h〃鐨勬暟銆?br />鐒跺悗璋冪敤閫掑綊銆?br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<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; "><</span><span style="color: #000000; ">cstdio</span><span style="color: #000000; ">></span><span style="color: #000000; "><br />#include </span><span style="color: #000000; "><</span><span style="color: #000000; ">cstring</span><span style="color: #000000; ">></span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; "> std;<br /><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> a[</span><span style="color: #000000; ">25</span><span style="color: #000000; ">], b[</span><span style="color: #000000; ">25</span><span style="color: #000000; ">];<br /><br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> solve(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> n)<br />{<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> t, i, j;<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (n </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">) </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (n </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">)<br />    {<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">X</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    }<br />    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (j </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;; </span><span style="color: #000000; ">++</span><span style="color: #000000; ">j)<br />    {<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (b[j] </span><span style="color: #000000; ">>=</span><span style="color: #000000; "> n)<br />            </span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br />    }<br />    n </span><span style="color: #000000; ">=</span><span style="color: #000000; "> n </span><span style="color: #000000; ">-</span><span style="color: #000000; "> b[j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">];<br />    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> j; </span><span style="color: #000000; ">++</span><span style="color: #000000; ">i)<br />    {<br />        t </span><span style="color: #000000; ">=</span><span style="color: #000000; "> a[i] </span><span style="color: #000000; ">*</span><span style="color: #000000; "> a[j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; "> i];<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (n </span><span style="color: #000000; ">></span><span style="color: #000000; "> t)<br />        {<br />            n </span><span style="color: #000000; ">=</span><span style="color: #000000; "> n </span><span style="color: #000000; ">-</span><span style="color: #000000; "> t;<br />        }<br />        </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "><br />            </span><span style="color: #0000FF; ">break</span><span style="color: #000000; ">;<br />    }<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (i </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">)<br />    {<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />        solve(b[i </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">] </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">+</span><span style="color: #000000; "> (n </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">)</span><span style="color: #000000; ">/</span><span style="color: #000000; "> a[j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; "> i]);<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />    printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">X</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (i </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">)<br />    {<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />        solve(b[j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; "> i] </span><span style="color: #000000; ">+</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">+</span><span style="color: #000000; "> (n </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">) </span><span style="color: #000000; ">%</span><span style="color: #000000; "> a[j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; "> </span><span style="color: #000000; ">-</span><span style="color: #000000; "> i]);<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> main()<br />{<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> n;<br />    </span><span style="color: #0000FF; ">int</span><span style="color: #000000; "> i, j;<br />    b[</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; ">0</span><span style="color: #000000; ">;<br />    a[</span><span style="color: #000000; ">0</span><span style="color: #000000; ">] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> b[</span><span style="color: #000000; ">1</span><span style="color: #000000; ">] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> a[</span><span style="color: #000000; ">1</span><span style="color: #000000; ">] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">;<br />    </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (i </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">2</span><span style="color: #000000; ">; i </span><span style="color: #000000; "><</span><span style="color: #000000; "> </span><span style="color: #000000; ">20</span><span style="color: #000000; ">; </span><span style="color: #000000; ">++</span><span style="color: #000000; ">i)<br />    {<br />        a[i] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; "> (j </span><span style="color: #000000; ">=</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; "> i; </span><span style="color: #000000; ">++</span><span style="color: #000000; ">j)<br />        {<br />            a[i] </span><span style="color: #000000; ">+=</span><span style="color: #000000; "> a[j] </span><span style="color: #000000; ">*</span><span style="color: #000000; "> a[i </span><span style="color: #000000; ">-</span><span style="color: #000000; "> j </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">];<br />        }<br />        b[i] </span><span style="color: #000000; ">=</span><span style="color: #000000; "> b[i </span><span style="color: #000000; ">-</span><span style="color: #000000; "> </span><span style="color: #000000; ">1</span><span style="color: #000000; ">] </span><span style="color: #000000; ">+</span><span style="color: #000000; "> a[i];<br />    }<br />    </span><span style="color: #0000FF; ">while</span><span style="color: #000000; "> (scanf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">%d</span><span style="color: #000000; ">"</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&</span><span style="color: #000000; ">n) </span><span style="color: #000000; ">&&</span><span style="color: #000000; "> n)<br />    {<br />        solve(n);<br />        printf(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">\n</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />}<br /></span></div><img src ="http://www.shnenglu.com/lvlawliet/aggbug/159082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/lvlawliet/" target="_blank">LLawliet</a> 2011-10-25 20:55 <a href="http://www.shnenglu.com/lvlawliet/archive/2011/10/25/159082.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>JOJ1037錛歄perandhttp://www.shnenglu.com/lvlawliet/archive/2011/10/25/159078.htmlLLawlietLLawlietTue, 25 Oct 2011 11:53:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/25/159078.htmlhttp://www.shnenglu.com/lvlawliet/comments/159078.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/25/159078.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/159078.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/159078.html

Professor Maple teaches mathematics in a university. He have invented a function for the purpose of obtaining the operands from an expression. The function named op(i,e) can be described as follows: The expression e may be divided into sub-expression(s) by the operator, which has the lowest priority in the expression. For example, the expression “a*b+b*c+c*d” should be divided into three sub-expressions “a*b”, “b*c” and “c*d”, because the operator “+” has the lowest priority. The purpose of this function is to extract the ith sub-expression as the result. So, in the example above, op(2,e)=b*c.

If we regard the sub-expression as the main expression, it might be divided again and again. Obviously, the dividing process is recursive. As you see, the following example is much more complex:

Let p:=a^b*c+(d*c)^f*z+b
op(1,op(1,op(2,p)))=(d*c)
op(1,op(1,op(1,op(2,p))))=d*c
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b

Professor Maple is so lazy that he would leave the work to computer rather than do it himself, when the expression is long and complicated. Of course, without your program, the computer won’t work out the result automatically.

Input

The input file contains several test cases. The last test case in the input file is followed by a line containing a symbol “*”, indicating the end of the input data. Each test case consists of two parts. The first part describes the expression, while the second part contains several questions, which should be calculated according to the expression.

The first line of each test case contains an expression consists of the expression name, “:=” and the content of the expression. The expression name is a lowercase. And the content is composed by lowercases and operators “+”, “(”, “)”, “*” and “^”. For example, here is a valid expression, p:=a^b*c+(d*c)^f*z+b. Among those operators, “(” and “)” have the highest priority. The operator “^” has a lower priority, and then “*”. The priority of the operator “+” is the lowest.

The second line of each test case contains an integer n indicating n questions based on the above expression. This is followed by n lines. Each of them contains the description of one question, which consists of integers. For example, the question with three integers “2 1 1” describes the function op(1,op(1,op(2,e))). To compute this function, we have to keep to the following sequence: First, according to the first integer 2, divide the expression and extract the 2nd sub-expression. Then, according to the second integer 1, divide the sub-expression and extract the 1st one. Finally, according to the third integer 1, divide the outcome again, and extract the result.

Output

For each test case, display the expression name and a colon on the first line. Then display the result of each question on a line. The layout of the output is shown in the sample output.

You may assume that all expressions and functions are always valid.

Display a blank line between test cases.

Sample Input

p:=a^b*c+(d*c)^f*z+b 
4
2 1 1
2 2
3
3 1
a:=(x+y)
3
1
1 2
1 2 1
*

Sample Output

Expression p: 
op(1,op(1,op(2,p)))=(d*c)
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b

Expression a:
op(1,a)=x+y
op(2,op(1,a))=y
op(1,op(2,op(1,a)))=y


妯℃嫙棰橈紝澶勭悊濂藉師瀛楃涓茬殑浼樺厛綰у氨琛屼簡銆?br />浠g爜錛?br />
#include <iostream>
#include 
<cstring>
#include 
<cstdio>
using namespace std;

int find(char c)
{
    
if (c == '(' || c == ')'return 4;
    
if (c == '^'return 3;
    
if (c == '*'return 2;
    
if (c == '+'return 1;
    
return 1000;
}

char s[101];
string e;
int n, i, j, k, p[101], r[101], v[101], head, tail, x, ri, d = 0;

int main()
{
    
while (scanf("%s", s), s[0!= '*')
    {
        
if (d++) puts("");
        head 
= 0;
        e 
= "";
        
while (s[head] != ':')
          e 
+= s[head++];
        head 
+= 2;
        printf(
"Expression %s:\n", e.c_str());
        tail 
= strlen(s) - 1;
        x 
= 0;
        
for (i = head; i <= tail; ++i)
        {
            
if (s[i] == ')') x -= 4;
            v[i] 
= find(s[i]) + x;
            
if (s[i] == '(') x += 4;
        }
        scanf(
"%d\n"&k);
        
for (i = 0; i < k; ++i)
        {
            ri 
= 0;
            
int head1 = 3, min1, t;
            tail 
= strlen(s);
            
char c;
            
while ((c = getchar()) != '\n')
            {
                cin.putback(c);
                scanf(
"%d"&n);
                r[ri
++= n;
                min1 
= 1000;
                
for (j = head1; j < tail; ++j)
                  
if (v[j] < min1) min1 = v[j];
                
if (s[head1] == '(' && s[tail - 1== ')' && min1 == v[head1])
                {
                    head1
++;
                    tail
--;
                }
                
else
                  
if (head1 + 1 == tail){}
                
else
                {
                    p[
0= head1 - 1;
                    
for (j = head1, t = 1; j < tail; ++j)
                      
if (v[j] == min1)
                      {
                          p[t
++= j;
                      }
                    p[t] 
= tail;
                    head1 
= p[n - 1+ 1;
                    tail 
= p[n];
                }
            }
            
for (j = ri - 1; j >= 0--j)
              printf(
"op(%d,", r[j]);
            printf(
"%s", e.c_str());
            
for (j = 0; j < ri; ++j)
              printf(
")");
            printf(
"=");
            
for (j = head1; j < tail; ++j)
                printf(
"%c", s[j]);
            puts(
"");
        }
    }
    
return 0;
}


LLawliet 2011-10-25 19:53 鍙戣〃璇勮
]]>
POJ1679:The Unique MSThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.htmlLLawlietLLawlietMon, 17 Oct 2011 13:07:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.htmlhttp://www.shnenglu.com/lvlawliet/comments/158577.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158577.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158577.html
The Unique MST
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 11943
Accepted: 4112

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

2 
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3 
Not Unique!

Source

POJ Monthly--2004.06.27 srbga@P

浠g爜錛?br />
#include <iostream>
#include 
<cstdio>
#include 
<cstring>
#include 
<algorithm>
using namespace std;

typedef 
struct
{
    
int x, y, w;
} edge;

edge s[
10010];
int top, t[10010];

bool cmp(edge a, edge b)
{
    
return a.w < b.w;
}

int kru(int n, int m, int x)
{
    
int i, j, a, b, tag[110], tem, sum, k;
    
for (i = 0; i < n; ++i)
    {
        tag[i] 
= i;
    }
    k 
= 1, j = 0, sum = 0;
    
while (k < n)
    {
        a 
= s[j].x - 1;
        b 
= s[j].y - 1;
        
if (j == x)
        {
            j
++;
            
continue;
        }
        
if (tag[a] != tag[b])
        {
            
if (x == -1)
            {
                t[top] 
= j;
                top
++;
            }
            tem 
= tag[b];
            k
++, sum += s[j].w;
            
for (i =  0; i < n; ++i)
            {
                
if (tag[i] == tem)
                {
                    tag[i] 
= tag[a];
                }
            }
        }
        j
++;
    }
    
return sum;
}
int main()
{
    
int p, n, m, cmin;
    scanf(
"%d"&p);
    
while (p--)
    {
        
int flag = 1;
        scanf(
"%d%d"&n, &m);
        
for (int i = 0; i < m; ++i)
            scanf(
"%d%d%d"&s[i].x, &s[i].y, &s[i].w);
        sort(s, s 
+ m, cmp);
        top 
= 0;
        
int min = kru(n, m, -1);
        
int key = top;
        
for (int l = 0; l < key; ++l)
        {
            cmin 
= kru(n, m, t[l]);
            
if (cmin == min)
            {
                flag 
= 0;
                printf(
"Not Unique!\n");
                
break;
            }
        }
        
if (flag)
        printf(
"%d\n", min);
    }
    
return 0;
}


LLawliet 2011-10-17 21:07 鍙戣〃璇勮
]]>
POJ3463錛歋ightseeinghttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.htmlLLawlietLLawlietMon, 17 Oct 2011 08:30:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.htmlhttp://www.shnenglu.com/lvlawliet/comments/158556.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158556.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158556.html
Sightseeing
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 4917Accepted: 1688

Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

  • M lines, each with three integers AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

    The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

  • One line with two integers S and F, separated by a single space, with 1 ≤ SF ≤ N and S ≠ F: the starting city and the final city of the route.

    There will be at least one route from S to F.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.

Sample Input

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

Sample Output

3
2

Hint

The first test case above corresponds to the picture in the problem description.

Source



鎬濊礬:
渚濇嵁鎻忓啓鍙煡錛屾湰棰樼殑瑕佹眰鍗充究瑕佹眰鍑烘渶鐭礬鍜屾瘮鏈鐭礬闀?鐨勬鐭礬錛屽洜鑰屽彲鐢―ijkstra鏉ュ鐞嗐傜繑瀹炲仛娉曞涓嬶細鐢ㄤ袱緇勬暟紱誨埆鐧昏鏈鐭礬鍜屾鐭礬鐨勯暱搴︼紙dist錛夛紝鏉℃暟錛坈nt錛夛紝鎷滀細絎﹀彿錛坲sed錛夛紝寤轟竴涓紭鍏堥槦鍒楋紝鍏冪礌鍗曚綅鍖呮嫭鑺傜偣搴忓彿錛坴錛夛紝璇ヨ妭鐐硅礬緇忛暱錛坙en錛夛紝浠ュ強鐧昏璺緞縐嶇被錛坮ef錛夛紝姣忔浠庝紭鍏堥槦鍒椾腑鍙栧嚭綆$敤鑺傜偣鍚庯紝鐢ㄥ畠鎵鐧昏鐨勮礬寰勯暱鏇存柊寰呮瘮鎷熻礬寰勶紝紱誨埆鐢ㄥ畠鍜岀洰鍓嶆墍鐧昏鐨勮鑺傜偣鐨勬渶鐭礬寰勪互鍙婃孌佃礬寰勬瘮鎷燂紝涓剰鏇存柊鏉′歡鍒欑櫥璁拌礬寰勭綾伙紝騫剁敓鎴愭柊鑺傜偣鍔犲叆浼樺厛闃熷垪錛屽悓鏃舵洿鏂扮洰鍓嶈妭鐐瑰璇ョ綾昏礬寰勬潯鏁般備竾涓涓嶄腑鎰忔潯浠剁劧鑰屼腑鎰忔販鍚岃仈緋伙紝鍒欏鍔犵浉搴旂殑鏉℃暟鍒拌鑺傜偣鎵鐧昏鐨勮礬寰勬潯鏁頒笂銆?/span>

浠g爜錛?br />
#include <cstdio>
#include 
<memory.h>
#include 
<queue>
#define N 1001
#define M 10001
#define INF 0x7fffffff
#define clr(a) memset(a, 0, sizeof(a))
using namespace std;

struct Edge
{
    
int v, len, ref;
    Edge 
*link;
    Edge new_E(
int v1, int l, int r)
    {
        v 
= v1, len = l, ref = r;
        
return *this;
    }
*E[N], mempool[M];

int dist[N][2], used[N][2], cnt[N][2];
int n, m, memh, S, T;

void AddEdge(int u, int v, int len)
{
    Edge 
*= &mempool[memh++];
    e 
-> v = v;
    e 
-> len = len;
    e 
-> link = E[u];
    E[u] 
= e;
}

bool operator < (Edge a, Edge b)
{
    
return a.len > b.len;
}

priority_queue 
<Edge, vector <Edge> > Q;

void InitData()
{
    
int i, u, v, len;
    memh 
= 0;
    scanf(
"%d%d"&n, &m);
    clr(E);
    
for (i = 1; i <= m; ++i)
    {
        scanf(
"%d%d%d"&u, &v, &len);
        AddEdge(u, v, len);
    }
    scanf(
"%d%d"&S, &T);
}

int Dijstra()
{
    Edge D, P;
    clr(cnt);
    clr(used);
    
for (int i = 1; i <= n; ++i)
        dist[i][
0= dist[i][1= INF;
    dist[S][
0= 0;
    cnt[S][
0= 1;
    
while (!Q.empty())
        Q.pop();
    Q.push(D.new_E(S, 
00));
    
while (!Q.empty())
    {
        P 
= Q.top();
        Q.pop();
        
if (!used[P.v][P.ref])
        {
            used[P.v][P.
ref= 1;
            
for (Edge *= E[P.v]; e; e = e -> link)
            {
                
int tmp = P.len + e -> len;
                
if (tmp < dist[e -> v][0])
                {
                    
if (dist[e -> v][0!= INF)
                    {
                        dist[e 
-> v][1= dist[e -> v][0];
                        cnt[e 
-> v][1= cnt[e -> v][0];
                        Q.push(D.new_E(e 
-> v, dist[e -> v][0], 1));
                    }
                    dist[e 
-> v][0= tmp;
                    cnt[e 
-> v][0= cnt[P.v][P.ref];
                    Q.push(D.new_E(e 
-> v, tmp, 0));
                }
                
else
                
if (tmp == dist[e -> v][0])
                {
                    cnt[e 
-> v][0+= cnt[P.v][P.ref];
                }
                
else
                
if (tmp < dist[e -> v][1])
                {
                    dist[e 
-> v][1= tmp;
                    cnt[e 
-> v][1= cnt[P.v][P.ref];
                    Q.push(D.new_E(e 
-> v, tmp, 1));
                }
                
else
                
if (dist[e -> v][1== tmp)
                {
                    cnt[e 
-> v][1+= cnt[P.v][P.ref];
                }
            }
        }
    }
    
if (dist[T][1- 1 == dist[T][0])
        cnt[T][
0+= cnt[T][1];
    
return cnt[T][0];
}

int main()
{
    
int T;
    scanf(
"%d"&T);
    
while (T--)
    {
        InitData();
        printf(
"%d\n", Dijstra());
    }
}


LLawliet 2011-10-17 16:30 鍙戣〃璇勮
]]>
POJ3522:Slim Spanhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.htmlLLawlietLLawlietMon, 17 Oct 2011 07:54:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.htmlhttp://www.shnenglu.com/lvlawliet/comments/158548.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158548.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158548.html
Slim Span
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 4023Accepted: 2116

Description

Given an undirected weighted graph G, you should find one of spanning trees specified as follows.

The graph G is an ordered pair (VE), where V is a set of vertices {v1v2, …, vn} and E is a set of undirected edges {e1e2, …, em}. Each edge e ∈ E has its weight w(e).

A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices with n − 1 edges. The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n − 1 edges of T.


Figure 5: A graph G and the weights of the edges

For example, a graph G in Figure 5(a) has four vertices {v1v2v3v4} and five undirected edges {e1e2e3e4e5}. The weights of the edges are w(e1) = 3, w(e2) = 5, w(e3) = 6, w(e4) = 6, w(e5) = 7 as shown in Figure 5(b).


Figure 6: Examples of the spanning trees of G

There are several spanning trees for G. Four of them are depicted in Figure 6(a)~(d). The spanning tree Ta in Figure 6(a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the tree Ta is 4. The slimnesses of spanning trees TbTc and Td shown in Figure 6(b), (c) and (d) are 3, 2 and 1, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning tree Td in Figure 6(d) is one of the slimmest spanning trees whose slimness is 1.

Your job is to write a program that computes the smallest slimness.

Input

The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.

nm
a1b1w1
ambmwm

Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and 0 ≤ m ≤ n(n − 1)/2. ak and bk (k = 1, …, m) are positive integers less than or equal to n, which represent the two vertices vak and vbk connected by the kth edge ekwk is a positive integer less than or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (VE) is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices).

Output

For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, −1 should be printed. An output should not contain extra characters.

Sample Input

4 5
1 2 3
1 3 5
1 4 6
2 4 6
3 4 7
4 6
1 2 10
1 3 100
1 4 90
2 3 20
2 4 80
3 4 40
2 1
1 2 1
3 0
3 1
1 2 1
3 3
1 2 2
2 3 5
1 3 6
5 10
1 2 110
1 3 120
1 4 130
1 5 120
2 3 110
2 4 120
2 5 130
3 4 120
3 5 110
4 5 120
5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422
5 8
1 2 1
2 3 100
3 4 100
4 5 100
1 5 50
2 5 50
3 5 50
4 1 150
0 0

Sample Output

1
20
0
-1
-1
1
0
1686
50

Source



棰樼洰灝辨槸鐢熸垚涓媯墊爲錛岃姹傝竟鏉冩渶澶у噺鏈灝忕殑宸渶灝忋?br />鏍規嵁Kruskal鎬濇兂錛屾妸杈規帓搴忥紝涔嬪悗鏋氫婦涓涓嬪氨琛屼簡銆?br />
浠g爜錛?br />
#include <cmath>
#include 
<cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<iostream>
#include 
<algorithm>
using namespace std;

const int M = 5005;
const int INF = 1 << 29;

struct edge
{
    
int st, ed, w;
    
bool operator < (edge a) const
    {
        
return w < a.w;
    }
} e[M];

int n, m, ans, num, temp;
int f[105], rank[105];

void makeset()
{
    
for (int i = 1; i <= n; ++i)
        f[i] 
= i;
    memset(rank, 
0sizeof(rank));
}

int find(int x)
{
    
while (f[x] != x) x = f[x];
    
return x;
}

void unionset(int a, int b)
{
    
int p = find(a);
    
int q = find(b);
    
if (rank[p] > rank[q])
        f[q] 
= p;
    
else
    
if (rank[p] < rank[q])
        f[p] 
= q;
    
else
    {
        f[p] 
= q;
        rank[q]
++;
    }
}

void kruskal()
{
    ans 
= INF;
    
for (int i = 0; i < m - n + 2++i)
    {
        makeset();
        temp 
= -1;
        num 
= 0;
        
for (int j = i; j < m; ++j)
        {
            
if (find(e[j].st) != find(e[j].ed))
            {
                num
++;
                unionset(e[j].st, e[j].ed);
                
if (num == n - 1)
                {
                    temp 
= e[j].w - e[i].w;
                    
break;
                }
            }
        }
        
if (temp == -1break;
        
if (temp != -1 && temp < ans) ans = temp;
    }
    
if (ans >= INF) printf("-1\n");
    
else printf("%d\n", ans);
}

int main()
{
    
while (scanf("%d%d"&n, &m), n || m)
    {
        
for (int i = 0; i < m; ++i)
            scanf(
"%d%d%d"&e[i].st, &e[i].ed, &e[i].w);
        sort(e, e 
+ m);
        kruskal();
    }
    
return 0;
}


LLawliet 2011-10-17 15:54 鍙戣〃璇勮
]]>
POJ2449錛歊emmarguts' Datehttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.htmlLLawlietLLawlietMon, 17 Oct 2011 07:19:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.htmlhttp://www.shnenglu.com/lvlawliet/comments/158540.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158540.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158540.html
Remmarguts' Date
Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 12450Accepted: 3422

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 2
1 2 5
2 1 4
1 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu


絎琄鐭礬闂錛屽ぇ姒傛剰鎬濆氨鏄粰浣燦涓偣錛孧鏉¤竟錛岃竟鏄湁鍚戠殑錛岀粰浣犳瘡鏉¤竟鐨勮竟鏉冿紝緇欎綘涓涓猄璧峰鐐癸紝T緇撴潫鐐癸紝鍜屼竴涓狵錛屾眰S鍒癟鐨勭K鐭礬銆?br />SPFA+A*鍚彂寮忔悳绱€?br />
璇磋鍚彂寮忔悳绱㈠惂錛?br />

閫氬父鍦ㄨВ鍐抽棶棰樼殑鏃跺欙紝鎴戜滑闇瑕佺敤鍒版悳绱㈢畻娉曪紝鐢卞凡鐭ョ姸鎬佹帹鍑烘柊鐨勭姸鎬侊紝鐒跺悗媯楠屾柊鐨勭姸鎬佹槸涓嶆槸灝辨槸鎴戜滑瑕佹眰鐨勬渶浼樿В銆傛楠屽畬鎵鏈夌殑鐘舵佸疄闄呬笂灝辯浉褰撲簬閬嶅巻浜嗕竴寮犻殣寮忓浘銆傞仐鎲劇殑鏄紝鎵鏈夌殑鐘舵佺粍鎴愮殑鐘舵佺┖闂村線寰鏄垚鎸囨暟綰у埆澧為暱鐨勶紝涔熷氨閫犳垚浜嗛亶鍘嗛渶瑕佺敤鍒版寚鏁扮駭鍒殑鏃墮棿錛屽洜姝わ紝綰補鐨勬毚鍔涙悳绱紝鏃剁┖鏁堢巼閮芥瘮杈冧綆銆傚綋鐒訛紝鎴戜滑鍦ㄧ敓媧諱腑閬囧埌浜嗙被浼間簬鎼滅儲鐨勯棶棰橈紝鎴戜滑騫朵笉浼氱洸鐩湴鍘繪悳瀵繪瘡涓縐嶇姸鎬侊紝鎴戜滑浼氶氳繃鎴戜滑鐨勬濈淮錛岄夋嫨涓鏉℃渶鎺ヨ繎浜庣洰鏍囩殑璺緞鎴栬呮槸榪戜技浜庢渶鐭殑璺緞鍘誨畬鎴愭悳绱換鍔°傚綋鎴戜滑鎯寵璁$畻鏈哄幓瀹屾垚榪欐牱涓欏規悳绱換鍔$殑鏃跺欙紝灝卞緱璁╄綆楁満鍍忎漢涓鏍瘋兘澶熷尯鍒嗗敖閲忕煭鐨勮礬寰勶紝浠ヤ究楂樻晥鍦版壘鍒版渶浼樿В銆傝繖鏃跺彲浠ユ妸璁$畻鏈虹湅浣滄槸涓縐嶆櫤鑳戒綋(agent)鍙互瀹炵幇鐢卞垵濮嬬姸鎬佸悜鐩爣鐘舵佺殑杞Щ銆?/span>

       鏈変竴縐嶈椽蹇冪瓥鐣ワ紝鍗蟲瘡涓姝ヨ漿縐婚兘鐢辮綆楁満閫夋嫨褰撳墠鐨勬渶浼樿В鐢熸垚鏂扮殑鐘舵侊紝涓鐩村埌杈劇洰鏍囩姸鎬佷負姝€傝繖鏍峰仛鐨勬椂闂存晥鐜囪櫧鐒惰緝楂橈紝浣嗘槸璐績鐨勭瓥鐣ュ彧鏄敤鍒頒簡灞閮ㄧ殑鏈浼樿В錛屽茍涓嶈兘淇濊瘉鏈鍚庡埌杈劇洰鏍囩姸鎬佸緱鍒扮殑鏄叏灞鏈浼樿В銆傚湪鑳戒繚璇佸叏灞鏈浼樿В鐨勮寖鍥村唴錛岃椽蹇冪畻娉曡繕鏄緢鏈夌敤鐨勩傛瘮濡傝鎴戜滑鐔熺煡鐨?/span>Dijkstra綆楁硶姹傚崟婧愭渶鐭礬銆傛瘡嬈¢夋嫨璺濈婧愯妭鐐規渶鐭窛紱葷殑寰呮墿灞曡妭鐐硅繘琛屾墿灞曪紝鏈鍚庡氨鑳界敓鎴愭簮鑺傜偣鍒版墍鏈夎妭鐐圭殑鏈鐭礬寰勩備笅闈細璁插埌Dijkstra鐨勬墿灞曪紝褰撶悊瑙d簡榪欎釜綆楁硶涔嬪悗錛屾垜鎯籌紝浣犱細瀵?/span>Dijkstra鏈夋洿娣卞叆鐨勭悊瑙c?/span>

       榪欏氨鏄?/span>A*綆楁硶銆傚畾涔夊垵濮嬬姸鎬?/span>S錛岀洰鏍囩姸鎬?/span>t錛?/span>g(s)鏄敱鍒濆鐘舵佽漿縐誨埌褰撳墠鐘舵?/span>s鎵緇忚繃鐨勮礬寰勯暱搴︼紝h*(s)鏄綋鍓嶇姸鎬?/span>s璺濈鐩爣鐘舵?/span>t鐨勫疄闄呴暱搴︼紝浣嗘槸涓鑸儏鍐典笅鎴戜滑鏄笉鐭ラ亾h*(s)鐨勫肩殑錛屾墍浠ヨ繕瑕佸畾涔変竴涓及浠峰嚱鏁?/span>h(s)錛屾槸瀵?/span>h*(s)鍑芥暟鍊肩殑涓嬬晫鐨勪及璁★紝涔熷氨鏄湁h(s)<=h*(s)錛岃繖鏍烽渶瑕佷竴涓潯浠訛紝浣垮緱鐢?/span>s1鐢熸垚鐨勬瘡鐘舵?/span>s2錛岄兘鏈?/span>h(s1)<=h(s2)錛岃繖鏄竴涓浉瀹圭殑浼頒環鍑芥暟銆傚啀瀹氫箟f(s)=g(s)+h(s)涓哄惎鍙戝嚱鏁幫紝鍥犱負h(s)鏄崟璋冮掑鐨勶紝鎵浠?/span>f(s)涔熸槸鍗曡皟閫掑鐨勩傝繖鏍?/span>f(s)灝變及璁″嚭浜嗙敱鍒濆鐘舵佺殑鎬諱綋浠d環銆?/span>A*綆楁硶灝遍氳繃鏋勯犺繖鏍蜂竴涓惎鍙戝嚱鏁幫紝灝嗘墍鏈夌殑寰呮墿灞曠姸鎬佸姞鍏ュ埌闃熷垪閲岋紝姣忔浠庨槦鍒楅噷閫夋嫨f(s)鍊兼渶灝忕殑鐘舵佽繘琛屾墿灞曘傜敱浜庡惎鍙戝嚱鏁扮殑浣滅敤錛屼嬌寰楄綆楁満鍦ㄨ繘琛岀姸鎬佽漿縐葷殑鏃跺欏敖閲忛伩寮浜嗕笉鍙兘浜х敓鏈浼樿В鐨勫垎鏀紝鑰岄夋嫨鐩稿杈冩帴榪戞渶浼樿В鐨勮礬寰勮繘琛屾悳绱紝鎻愰珮浜嗘悳绱㈡晥鐜囥?/span>

       璁插埌榪欓噷錛屽彲鑳藉凡緇忓A*綆楁硶鐨勬蹇墊湁鐐圭湁鐩簡銆備笅闈㈡垜鍐嶆潵鍋氫竴涓瘮杈冿紝灝辯敤涓婇潰璁插埌鐨?/span>Dijkstra鐨勪緥瀛愩?/span>Dijkstra綆楁硶璇寸殑鏄瘡嬈¢夋嫨璺濈婧愮偣鏈鐭窛紱葷殑鐐硅繘琛屾墿灞曘傚綋鐒跺彲浠ョ湅鍋氫簨鍏堝皢婧愮偣鍒版墍鏈夎妭鐐硅窛紱葷殑鍊間繚瀛樺湪涓涓紭鍏堥槦鍒楅噷錛屾瘡嬈′粠浼樺厛闃熷垪閲屽嚭闃熸渶鐭窛紱葷殑鐐規墿灞曪紝姣忎釜鐐圭殑鎵╁睍娑夊強鍒拌鏇存柊闃熷垪閲屾墍鏈夊緟鎵╁睍鑺傜偣鐨勮窛紱誨鹼紝姣忎釜鑺傜偣鍙兘榪涢槦涓嬈★紝灝遍渶瑕佹湁涓涓〃鏉ヨ褰曟瘡涓妭鐐圭殑鍏ラ槦嬈℃暟錛堝氨鏄畻娉曚腑鐢ㄥ埌鐨勬爣璁版暟緇勶級銆傚皢Dijkstra姹傛渶鐭礬鐨勬柟娉曟墿灞曪紝榪欓亾棰樼洰瑕佹眰鐨勬槸涓ょ偣闂寸k鐭礬銆傜被姣斾簬Dijkstra綆楁硶鍙互棣栧厛紜畾涓嬮潰鍑犱釜鎼滅儲絳栫暐錛?/span>

1銆?/span>鐢ㄤ紭鍏堥槦鍒椾繚瀛樿妭鐐硅繘琛屾悳绱€?/span>

2銆?/span>鏀懼紑姣忎釜鑺傜偣鐨勫叆闃熸鏁幫紝姹?/span>k鐭礬錛屾瘡涓妭鐐瑰彲浠ュ叆闃?/span>k嬈°?/span>

棣栧厛鐪嬬涓涓瓥鐣ワ紝鍦?/span>A*綆楁硶涓敤浼樺厛闃熷垪灝辨槸瑕佺敤鍒板惎鍙戝嚱鏁?/span>f(s)紜畾鐘舵佸湪浼樺厛闃熷垪閲岄潰鐨勪紭鍏堢駭銆傚叾瀹?/span>Dijkstra鐢ㄥ埌鐨勪紭鍏堥槦鍒楀疄闄呬笂灝辨槸浼頒環鍑芥暟鍊間負0錛屽惎鍙戝嚱鏁?/span>f(s)=g(s)錛屽嵆鏄夊彇鍒版簮鐐硅窛紱繪渶榪戠殑鐐硅繘琛屾墿灞曘傚洜涓?/span>h(s)=0婊¤凍浜嗕及浠峰嚱鏁扮浉瀹硅繖涓潯浠躲傝繖棰樻眰k鐭礬灝變笉鑳藉崟綰殑浣跨敤h(s)=0榪欎釜浼頒環鍑芥暟銆傝В鍐寵繖閬撻鐨勬椂鍊欓夊彇h(x)=dt(x), dt(x)鏄?/span>x鑺傜偣鍒扮洰鏍囪妭鐐圭殑鏈鐭窛紱匯傛渶鐭窛紱誨彲浠ュ紑濮嬬敱Dijkstra鐩存帴姹傚緱銆?/span>

鍐嶇湅絎簩涓瓥鐣ワ紝鎺у埗姣忎釜鑺傜偣鐨勫叆闃燂紙鎴栧嚭闃燂級嬈℃暟涓?/span>k嬈★紝鍙互鎵懼埌絎?/span>k鐭礬寰勩傚彲鑳借繖鏍鋒兂鏈夌偣涓昏鐨勫鐢紝閭d箞鎴戝氨鍏堟潵璇佹槑榪欐牱涓涓粨璁猴細

濡傛灉x鏄?/span>s鍒?/span>t鐨勭k鐭礬寰勪笂鐨勪竴涓妭鐐癸紝閭d箞鐢辮繖鏉¤礬寰?/span>s鍒?/span>x鏄?/span>s鍒?/span>x鐨勭m鐭礬寰勶紝鍒欎笉鍙兘鏈?/span>m>k銆傜敤鍙嶈瘉娉曞緢瀹規槗寰楀嚭錛氬鏋滆繖鏉¤礬寰勬槸s鍒?/span>x鐨勭m鐭礬寰勶紝濡傛灉m>k錛岄偅涔堢粡榪?/span>x鍒?/span>t鐨勮礬寰勫氨鏈?/span>m-1鏉℃瘮褰撳墠璺緞瑕佺煭錛屼笉絎﹀悎褰撳墠璺緞鏄?/span>s鍒?/span>t鐨勭k鐭礬寰勩?br />

浠g爜錛?br />
#include <cstdio>
#include 
<algorithm>
#include 
<queue>
#include 
<vector>
#include 
<cstring>
#define MAXN 10005 //杈規暟
#define inf 1 << 25
using namespace std;

int dis[MAXN];

struct node
{
    
int v, dis;
};

struct edge
{
    
int v, w;
    friend 
bool operator < (edge a, edge b)
    {
        
return (a.w + dis[a.v]) > (b.w + dis[b.v]);
    }
};

vector 
<node> map[MAXN];
vector 
<node> remap[MAXN];

int n, m; //n鏄妭鐐規暟錛宮鏄竟鏁般?/span>
int s, t, k;  //s鏄搗濮嬬偣錛宼鏄粨鏉熺偣錛宬鏄k灝忋?/span>

void init()
{
    
int i;
    
for (i = 0; i < MAXN; ++i)
        map[i].clear();
    
for (i = 0; i < MAXN; ++i)
        remap[i].clear();
}

void spfa(int s)
{
    
int i;
    
bool used[MAXN];
    memset(used, 
0sizeof(used));
    
for (i = 0; i < MAXN; ++i)
        dis[i] 
= inf;
    dis[s] 
= 0;
    used[s] 
= true;
    queue 
<int> q;
    q.push(s);
    
while (!q.empty())
    {
        
int u = q.front();
        q.pop();
        used[u] 
= false;
        
for (i = 0; i < remap[u].size(); ++i)
        {
            node p 
= remap[u][i];
            
if (dis[p.v] > dis[u] + p.dis)
            {
                dis[p.v] 
= dis[u] + p.dis;
                
if (!used[p.v])
                {
                    used[p.v] 
= true;
                    q.push(p.v);
                }
            }
        }
    }
}

int a_star()
{
    
if (s == t)
        k
++;   //娉ㄦ剰錛岃搗濮嬪拰緇撴潫涓鏍鳳紝k瑕?1錛?/span>
    if (dis[s] == inf)
        
return -1;
    
int i, x, len, cnt[MAXN];
    edge n1, n2;
    priority_queue 
<edge> q;
    memset(cnt, 
0sizeof(cnt));
    n1.v 
= s;
    n1.w 
= 0;
    q.push(n1);
    
while (!q.empty())
    {
        n2 
= q.top();
        q.pop();
        x 
= n2.v;
        len 
= n2.w;
        cnt[x]
++;
        
if (cnt[t] == k)
            
return len;
        
if (cnt[x] > k)
            
continue;
        
for (i = 0; i < map[n2.v].size(); ++i)
        {
            n1.v 
= map[n2.v][i].v;
            n1.w 
= len + map[n2.v][i].dis;
            q.push(n1);
        }
    }
    
return -1;
}

int main()
{
    
int i;
    node p;
    
while (scanf("%d%d"&n, &m) != EOF)
    {
        init();
        
int a, b, l;
        
for (i = 1; i <= m; ++i)
        {
            scanf(
"%d%d%d"&a, &b, &l);
            p.v 
= b;
            p.dis 
= l;
            map[a].push_back(p);
            p.v 
= a;
            remap[b].push_back(p);
        }
        scanf(
"%d%d%d"&s, &t, &k);
        spfa(t);
        
int ans = a_star();
        printf(
"%d\n", ans);
    }
    
return 0;
}


LLawliet 2011-10-17 15:19 鍙戣〃璇勮
]]>
模特私拍国产精品久久| 久久国产精品无码网站| 亚洲av日韩精品久久久久久a| 99久久精品国产一区二区三区 | 色噜噜狠狠先锋影音久久| 伊人 久久 精品| 久久AV无码精品人妻糸列| 18岁日韩内射颜射午夜久久成人| 久久亚洲精品无码VA大香大香| A级毛片无码久久精品免费| 99久久国产宗和精品1上映 | 久久天天躁狠狠躁夜夜2020一| 亚洲欧美成人综合久久久| 久久精品国产清自在天天线| 国产精品久久久久久福利69堂| 色狠狠久久综合网| 久久久久99精品成人片三人毛片| 国产综合久久久久| 久久青青草原亚洲av无码app| 久久99热这里只频精品6| 国产精品99久久久久久猫咪| 久久久婷婷五月亚洲97号色| 18禁黄久久久AAA片| 青春久久| 精品伊人久久久| 久久人人添人人爽添人人片牛牛 | 国产精品久久久久久久午夜片| 久久成人国产精品| 久久久久久久久久久久中文字幕| 囯产精品久久久久久久久蜜桃| 亚洲欧美久久久久9999| 久久伊人亚洲AV无码网站| 久久成人18免费网站| 久久综合九色综合97_久久久| 国产成年无码久久久久毛片| 久久亚洲私人国产精品| 久久A级毛片免费观看| 国产精品久久久久久影院| 97久久天天综合色天天综合色hd | 亚洲欧洲精品成人久久奇米网| 久久久久久久久久免免费精品 |