锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产毛片久久久久久国产毛片,精品久久久久久国产三级,亚洲国产成人久久综合区http://www.shnenglu.com/397993401/category/13854.html鍦ㄩ剻瑙嗕腑鎴愰暱 璁板綍鎴愰暱鐨勭偣婊?/description>zh-cnFri, 03 Sep 2010 01:28:58 GMTFri, 03 Sep 2010 01:28:58 GMT60hdu 1102 prim 鏈灝忕敓鎴愭爲http://www.shnenglu.com/397993401/archive/2010/09/02/125706.html浠樼繑浠樼繑Thu, 02 Sep 2010 15:48:00 GMThttp://www.shnenglu.com/397993401/archive/2010/09/02/125706.htmlhttp://www.shnenglu.com/397993401/comments/125706.htmlhttp://www.shnenglu.com/397993401/archive/2010/09/02/125706.html#Feedback0http://www.shnenglu.com/397993401/comments/commentRss/125706.htmlhttp://www.shnenglu.com/397993401/services/trackbacks/125706.htmlConstructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3780    Accepted Submission(s): 1298


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.


#include<iostream>
#include
<string.h>
using namespace std;

#define infinity 123456789
#define max_vertexes 100 

typedef 
int Graph[max_vertexes][max_vertexes];

Graph G;
int total;
int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];
int father[max_vertexes];
void prim(Graph G,int vcount)
{
    
int i,j,k;
    
int min = infinity;
    
for (i=0;i<vcount;i++)
    {
        lowcost[i]
=G[0][i];
        closeset[i]
=0
        used[i]
=0;
        father[i]
=-1
    }
    used[
0]=1
    
    
for (i=1;i<vcount;i++)
    {
        j
=0;
        
        
while (used[j]) j++;
        min 
= lowcost[j];
        
for (k=0;k<vcount;k++)
            
if ((!used[k])&&(lowcost[k]<min)) 
            {
                min 
=lowcost[k];
                j
=k;
            }
            father[j]
=closeset[j]; 
            used[j]
=1;
            total 
+= min;
            
for (k=0;k<vcount;k++)
                
if (!used[k]&&(G[j][k]<lowcost[k]))
                { 
                    lowcost[k]
=G[j][k];
                    closeset[k]
=j; 
                }
    }
}

int main()
{
    
int N,i,j,Q;
    
int x,y;
    
while(cin>>N)
    {
        
        total 
= 0;
        
for(i =0; i< N;i++)
        {
            
for(j = 0;j < N; j ++)
                cin
>>G[i][j];
        }
        cin
>>Q;
        
for(i = 0; i < Q; i ++)
        {
            cin
>>x>>y;
            G[x
-1][y-1= 0;
            G[y
-1][x-1= 0;
        }
        prim(G,N);
        cout
<< total<<endl;
    }
    
return 0;
}




浠樼繑 2010-09-02 23:48 鍙戣〃璇勮
]]>
鏈夊悜鍥懼己榪為氬垎閲?/title><link>http://www.shnenglu.com/397993401/archive/2010/05/20/115966.html</link><dc:creator>浠樼繑</dc:creator><author>浠樼繑</author><pubDate>Thu, 20 May 2010 12:28:00 GMT</pubDate><guid>http://www.shnenglu.com/397993401/archive/2010/05/20/115966.html</guid><wfw:comment>http://www.shnenglu.com/397993401/comments/115966.html</wfw:comment><comments>http://www.shnenglu.com/397993401/archive/2010/05/20/115966.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/397993401/comments/commentRss/115966.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/397993401/services/trackbacks/115966.html</trackback:ping><description><![CDATA[<h3 style="margin: 10px 0px 5px; font-family: sans-serif; font-style: normal; font-variant: normal; font-weight: bold; font-size: 170%; line-height: 100%; font-size-adjust: none; font-stretch: normal; color: #758d38;"> <p style="margin: 0px 0px 10px; padding: 0px; line-height: 22px;">杞嚜錛?/p> <p style="margin: 0px 0px 10px; padding: 0px; line-height: 22px;"><a style="line-height: 22px; text-decoration: none; color: #556c88;">http://www.byvoid.com/blog/scc-tarjan/ <br></a></p> <p style="margin: 0px 0px 10px; padding: 0px; line-height: 22px;"><a style="line-height: 22px; text-decoration: none; color: #556c88;"><br></a></p> <br><strong> </strong></h3> <h3 style="margin: 10px 0px 5px; font-family: sans-serif; font-style: normal; font-variant: normal; font-weight: bold; font-size: 170%; line-height: 100%; font-size-adjust: none; font-stretch: normal; color: #758d38;"><strong>[鏈夊悜鍥懼己榪為氬垎閲廬</strong> </h3> <p style="margin: 0px; padding: 0px 0px 15px;">鍦ㄦ湁鍚戝浘G涓紝濡傛灉涓や釜欏剁偣闂磋嚦灝戝瓨鍦ㄤ竴鏉¤礬寰勶紝縐頒袱涓《鐐?strong> 寮鴻繛閫?/strong> (strongly connected)銆傚鏋滄湁鍚戝浘G鐨勬瘡涓や釜欏剁偣閮藉己榪為氾紝縐癎鏄竴涓?strong> 寮鴻繛閫氬浘</strong> 銆傞潪寮鴻繛閫氬浘鏈夊悜鍥劇殑鏋佸ぇ寮鴻繛閫氬瓙鍥撅紝縐頒負<strong> 寮鴻繛閫氬垎閲?/strong> (strongly connected components)銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;">涓嬪浘涓紝瀛愬浘{1,2,3,4}涓轟竴涓己榪為氬垎閲忥紝鍥犱負欏剁偣1,2,3,4涓や袱鍙揪銆倇5},{6}涔熷垎鍒槸涓や釜寮鴻繛閫氬垎閲忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;"><img title="image" src="http://www.byvoid.com/blog/wp-content/uploads/2009/04/image1.png" alt="image" style="border-width: 0px; display: inline;" width="320" border="0" height="201"></p> <p style="margin: 0px; padding: 0px 0px 15px;">鐩存帴鏍規嵁瀹氫箟錛岀敤鍙屽悜閬嶅巻鍙栦氦闆嗙殑鏂規硶姹傚己榪為氬垎閲忥紝鏃墮棿澶嶆潅搴︿負O(N^2+M)銆傛洿濂界殑鏂規硶鏄疜osaraju綆楁硶鎴朤arjan綆楁硶錛屼袱鑰呯殑鏃墮棿澶嶆潅搴﹂兘鏄疧(N+M)銆傛湰鏂囦粙緇嶇殑鏄疶arjan綆楁硶銆?/p> <h3 style="margin: 10px 0px 5px; font-family: sans-serif; font-style: normal; font-variant: normal; font-weight: bold; font-size: 170%; line-height: 100%; font-size-adjust: none; font-stretch: normal; color: #758d38;"><strong> [Tarjan綆楁硶]</strong> </h3> <p style="margin: 0px; padding: 0px 0px 15px;">Tarjan綆楁硶鏄熀浜庡鍥炬繁搴︿紭鍏堟悳绱㈢殑綆楁硶錛屾瘡涓己榪為氬垎閲忎負鎼滅儲鏍戜腑鐨勪竴媯靛瓙鏍戙傛悳绱㈡椂錛屾妸褰撳墠鎼滅儲鏍戜腑鏈鐞嗙殑鑺傜偣鍔犲叆涓涓爢鏍堬紝鍥炴函鏃跺彲浠ュ垽鏂爤欏跺埌鏍堜腑鐨勮妭鐐規槸鍚︿負涓涓己榪為氬垎閲忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;">瀹氫箟DFN(u)涓鴻妭鐐箄鎼滅儲鐨勬搴忕紪鍙?鏃墮棿鎴?錛孡ow(u)涓簎鎴杣鐨勫瓙鏍戣兘澶熻拷婧埌鐨勬渶鏃╃殑鏍堜腑鑺傜偣鐨勬搴忓彿銆傜敱瀹氫箟鍙互寰楀嚭錛?/p> <div id="rdn3d35" class="wp_codebox_msgheader" style="border-width: 1px 1px 0px; border-top: 1px solid silver; border-left: 1px solid silver; border-right: 1px solid silver; margin: 0.5em 0px 0px; overflow: hidden; width: 671px; height: 22px; font-weight: bold; background-image: url(http://www.byvoid.com/blog/wp-content/plugins/wp-codebox/images/arrow-square.gif); background-color: #f3f8d7; color: #000000; background-position: 100% 5px;"><sup><a target="_blank" title="WP-CodeBox howto?" style="color: #cc6600; text-decoration: none; font-family: Arial,Tahoma ! important; font-style: normal ! important; font-variant: normal ! important; font-weight: normal ! important; font-size: 14px ! important; line-height: normal ! important; font-size-adjust: none ! important; font-stretch: normal ! important;">?</a></sup><a href="javascript:;" onclick="javascript:showCodeTxt('p1235code1'); return false;" style="color: #cc6600; text-decoration: none; font-weight: bold;">View Code</a> CPP</div> <div id="tnxfbbv" class="wp_codebox" style="border: 1px solid silver; margin: 0px 0px 0.5em; overflow: auto; color: #110000; width: 671px; background-color: #f9f9f9;"> <table style="margin: 0px ! important; border-collapse: collapse; width: 671px;"> <tbody> <tr id="p12351"> <td class="code" id="p1235code1" style="padding: 2px 4px; vertical-align: top;"> <pre class="cpp" style="border-style: none; margin: 0px; padding: 0px; overflow: visible; background-image: none; width: auto; float: none; clear: none; font-size: 12px; line-height: 1.333; text-align: left; font-family: monospace;">Low(u)=Min<br>{<br> DFN(u),<br> Low(v),(u,v)涓烘爲鏋濊竟錛寀涓簐鐨勭埗鑺傜偣<br> DFN(v),(u,v)涓烘寚鍚戞爤涓妭鐐圭殑鍚庡悜杈?闈炴í鍙夎竟)<br>}</pre> </td> </tr> </tbody> </table> </div> <p style="margin: 0px; padding: 0px 0px 15px;">褰揇FN(u)=Low(u)鏃訛紝浠涓烘牴鐨勬悳绱㈠瓙鏍戜笂鎵鏈夎妭鐐規槸涓涓己榪為氬垎閲忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;">綆楁硶浼唬鐮佸涓?/p> <div id="z3nxnv3" class="wp_codebox_msgheader" style="border-width: 1px 1px 0px; border-top: 1px solid silver; border-left: 1px solid silver; border-right: 1px solid silver; margin: 0.5em 0px 0px; overflow: hidden; width: 671px; height: 22px; font-weight: bold; background-image: url(http://www.byvoid.com/blog/wp-content/plugins/wp-codebox/images/arrow-square.gif); background-color: #f3f8d7; color: #000000; background-position: 100% 5px;"><sup><a target="_blank" title="WP-CodeBox howto?" style="color: #cc6600; text-decoration: none; font-family: Arial,Tahoma ! important; font-style: normal ! important; font-variant: normal ! important; font-weight: normal ! important; font-size: 14px ! important; line-height: normal ! important; font-size-adjust: none ! important; font-stretch: normal ! important;">?</a></sup><a href="javascript:;" onclick="javascript:showCodeTxt('p1235code2'); return false;" style="color: #cc6600; text-decoration: none; font-weight: bold;">View Code</a> CPP</div> <div id="33fbd5l" class="wp_codebox" style="border: 1px solid silver; margin: 0px 0px 0.5em; overflow: auto; color: #110000; width: 671px; background-color: #f9f9f9;"> <table style="margin: 0px ! important; border-collapse: collapse; width: 671px;"> <tbody> <tr id="p12352"> <td class="code" id="p1235code2" style="padding: 2px 4px; vertical-align: top;"> <pre class="cpp" style="border-style: none; margin: 0px; padding: 0px; overflow: visible; background-image: none; width: auto; float: none; clear: none; font-size: 12px; line-height: 1.333; text-align: left; font-family: monospace;">tarjan(u)<br>{<br> DFN[u]=Low[u]=++Index // 涓鴻妭鐐箄璁懼畾嬈″簭緙栧彿鍜孡ow鍒濆?br> Stack.push(u) // 灝嗚妭鐐箄鍘嬪叆鏍堜腑<br> for each (u, v) in E // 鏋氫婦姣忎竴鏉¤竟<br> if (v is not visted) // 濡傛灉鑺傜偣v鏈璁塊棶榪?br> tarjan(v) // 緇х畫鍚戜笅鎵?br> Low[u] = min(Low[u], Low[v])<br> else if (v in S) // 濡傛灉鑺傜偣v榪樺湪鏍堝唴<br> Low[u] = min(Low[u], DFN[v])<br> if (DFN[u] == Low[u]) // 濡傛灉鑺傜偣u鏄己榪為氬垎閲忕殑鏍?br> repeat<br> v = S.pop // 灝唙閫鏍堬紝涓鴻寮鴻繛閫氬垎閲忎腑涓涓《鐐?br> print v<br> until (u== v)<br>}</pre> </td> </tr> </tbody> </table> </div> <p style="margin: 0px; padding: 0px 0px 15px;">鎺ヤ笅鏉ユ槸瀵圭畻娉曟祦紼嬬殑婕旂ず銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;">浠庤妭鐐?寮濮婦FS錛屾妸閬嶅巻鍒扮殑鑺傜偣鍔犲叆鏍堜腑銆傛悳绱㈠埌鑺傜偣u=6鏃訛紝DFN[6]=LOW[6]錛屾壘鍒頒簡涓涓己榪為氬垎閲忋傞鏍堝埌u=v涓烘錛寋6}涓轟竴涓己榪為氬垎閲忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;"><img title="image" src="http://www.byvoid.com/blog/wp-content/uploads/2009/04/image2.png" alt="image" style="border-width: 0px; display: inline;" width="523" border="0" height="305"></p> <p style="margin: 0px; padding: 0px 0px 15px;">榪斿洖鑺傜偣5錛屽彂鐜癉FN[5]=LOW[5]錛岄鏍堝悗{5}涓轟竴涓己榪為氬垎閲忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;"><img title="image" src="http://www.byvoid.com/blog/wp-content/uploads/2009/04/image3.png" alt="image" style="border-width: 0px; display: inline;" width="523" border="0" height="335"></p> <p style="margin: 0px; padding: 0px 0px 15px;">榪斿洖鑺傜偣3錛岀戶緇悳绱㈠埌鑺傜偣4錛屾妸4鍔犲叆鍫嗘爤銆傚彂鐜拌妭鐐?鍚戣妭鐐?鏈夊悗鍚戣竟錛岃妭鐐?榪樺湪鏍堜腑錛屾墍浠OW[4]=1銆傝妭鐐?宸茬粡鍑烘爤錛?4,6)鏄í鍙夎竟錛岃繑鍥?錛?3,4)涓烘爲鏋濊竟錛屾墍浠OW[3]=LOW[4]=1銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;"><img title="image" src="http://www.byvoid.com/blog/wp-content/uploads/2009/04/image4.png" alt="image" style="border-width: 0px; display: inline;" width="523" border="0" height="335"></p> <p style="margin: 0px; padding: 0px 0px 15px;">緇х畫鍥炲埌鑺傜偣1錛屾渶鍚庤闂妭鐐?銆傝闂竟(2,4)錛?榪樺湪鏍堜腑錛屾墍浠OW[2]=DFN[4]=5銆傝繑鍥?鍚庯紝鍙戠幇DFN[1]=LOW[1]錛屾妸鏍堜腑鑺傜偣鍏ㄩ儴鍙栧嚭錛岀粍鎴愪竴涓繛閫氬垎閲弡1,3,4,2}銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;"><img title="image" src="http://www.byvoid.com/blog/wp-content/uploads/2009/04/image5.png" alt="image" style="border-width: 0px; display: inline;" width="523" border="0" height="335"></p> <p style="margin: 0px; padding: 0px 0px 15px;">鑷蟲錛岀畻娉曠粨鏉熴傜粡榪囪綆楁硶錛屾眰鍑轟簡鍥句腑鍏ㄩ儴鐨勪笁涓己榪為氬垎閲弡1,3,4,2},{5},{6}銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;">鍙互鍙戠幇錛岃繍琛孴arjan綆楁硶鐨勮繃紼嬩腑錛屾瘡涓《鐐歸兘琚闂簡涓嬈★紝涓斿彧榪涘嚭浜嗕竴嬈″爢鏍堬紝姣忔潯杈逛篃鍙璁塊棶浜嗕竴嬈★紝鎵浠ヨ綆楁硶鐨勬椂闂村鏉傚害涓篛(N+M)銆?/p> <p style="margin: 0px; padding: 0px 0px 15px;">姹傛湁鍚戝浘鐨勫己榪為氬垎閲忚繕鏈変竴涓己鏈夊姏鐨勭畻娉曪紝涓篕osaraju綆楁硶銆侹osaraju鏄熀浜庡鏈夊悜鍥懼強鍏墮嗗浘涓ゆDFS鐨勬柟娉曪紝鍏舵椂闂村鏉傚害涔熸槸O(N+M)銆備笌Trajan綆楁硶鐩告瘮錛孠osaraju綆楁硶鍙兘浼氱◢寰洿鐩磋涓浜涖備絾鏄疶arjan鍙敤瀵瑰師鍥捐繘琛屼竴嬈FS錛屼笉鐢ㄥ緩绔嬮嗗浘錛屾洿綆媧併傚湪瀹為檯鐨勬祴璇曚腑錛孴arjan綆楁硶鐨勮繍琛屾晥鐜囦篃姣擪osaraju綆楁硶楂?0%宸﹀彸銆傛澶栵紝璇arjan綆楁硶涓?a target="_blank" style="color: #cc6600; text-decoration: none;">姹傛棤鍚戝浘鐨勫弻榪為氬垎閲?鍓茬偣銆佹ˉ)鐨凾arjan綆楁硶</a>涔熸湁鐫寰堟繁鐨勮仈緋匯傚涔犺Tarjan綆楁硶錛屼篃鏈夊姪浜庢繁鍏ョ悊瑙f眰鍙岃繛閫氬垎閲忕殑Tarjan綆楁硶錛屼袱鑰呭彲浠ョ被姣斻佺粍鍚堢悊瑙c?/p> <p style="margin: 0px; padding: 0px 0px 15px;">姹傛湁鍚戝浘鐨勫己榪為氬垎閲忕殑Tarjan綆楁硶鏄互鍏跺彂鏄庤?a target="_blank" style="color: #cc6600; text-decoration: none;">Robert Tarjan</a>鍛藉悕鐨勩俁obert Tarjan榪樺彂鏄庝簡姹?a target="_blank" style="color: #cc6600; text-decoration: none;">鍙岃繛閫氬垎閲?/a>鐨凾arjan綆楁硶錛屼互鍙婃眰鏈榪戝叕鍏辯鍏堢殑紱葷嚎Tarjan綆楁硶錛屽湪姝ゅTarjan琛ㄧず宕囬珮鐨勬暚鎰忋?/p> <p style="margin: 0px; padding: 0px 0px 15px;">闄勶細tarjan綆楁硶鐨凜++紼嬪簭</p> <div id="rdpbldn" class="wp_codebox_msgheader" style="border-width: 1px 1px 0px; border-top: 1px solid silver; border-left: 1px solid silver; border-right: 1px solid silver; margin: 0.5em 0px 0px; overflow: hidden; width: 671px; height: 22px; font-weight: bold; background-image: url(http://www.byvoid.com/blog/wp-content/plugins/wp-codebox/images/arrow-square.gif); background-color: #f3f8d7; color: #000000; background-position: 100% 5px;"><sup><a target="_blank" title="WP-CodeBox howto?" style="color: #cc6600; text-decoration: none; font-family: Arial,Tahoma ! important; font-style: normal ! important; font-variant: normal ! important; font-weight: normal ! important; font-size: 14px ! important; line-height: normal ! important; font-size-adjust: none ! important; font-stretch: normal ! important;">?</a></sup><a href="javascript:;" onclick="javascript:showCodeTxt('p1235code3'); return false;" style="color: #cc6600; text-decoration: none; font-weight: bold;">View Code</a> CPP</div> <div id="nz53lbz" class="wp_codebox" style="border: 1px solid silver; margin: 0px 0px 0.5em; overflow: auto; color: #110000; width: 671px; background-color: #f9f9f9;"> <table style="margin: 0px ! important; border-collapse: collapse; width: 671px;"> <tbody> <tr id="p12353"> <td class="line_numbers" style="padding: 2px 4px; overflow: visible; vertical-align: top; text-align: right; background-color: #ddeeff; color: #666666; table-layout: auto; width: 10px;"> <pre style="border-style: none; margin: 0px; padding: 0px; overflow: visible; background-image: none; width: auto; float: none; clear: none; font-size: 12px; line-height: 1.333; text-align: right;">1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br></pre> </td> <td class="code" id="p1235code3" style="padding: 2px 4px; vertical-align: top;"> <pre class="cpp" style="border-style: none; margin: 0px; padding: 0px; overflow: visible; background-image: none; width: auto; float: none; clear: none; font-size: 12px; line-height: 1.333; text-align: left; font-family: monospace;">void tarjan(int i)<br>{<br> int j;<br> DFN[i]=LOW[i]=++Dindex;<br> instack[i]=true;<br> Stap[++Stop]=i;<br> for (edge *e=V[i];e;e=e->next)<br> {<br> j=e->t;<br> if (!DFN[j])<br> {<br> tarjan(j);<br> if (LOW[j]<LOW[i])<br> LOW[i]=LOW[j];<br> }<br> else if (instack[j] && DFN[j]<LOW[i])<br> LOW[i]=DFN[j];<br> }<br> if (DFN[i]==LOW[i])<br> {<br> Bcnt++;<br> do<br> {<br> j=Stap[Stop--];<br> instack[j]=false;<br> Belong[j]=Bcnt;<br> }<br> while (j!=i);<br> }<br>}<br>void solve()<br>{<br> int i;<br> Stop=Bcnt=Dindex=0;<br> memset(DFN,0,sizeof(DFN));<br> for (i=1;i<=N;i++)<br> if (!DFN[i])<br> tarjan(i);<br>}</pre> </td> </tr> </tbody> </table> </div> <br><img src ="http://www.shnenglu.com/397993401/aggbug/115966.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/397993401/" target="_blank">浠樼繑</a> 2010-05-20 20:28 <a href="http://www.shnenglu.com/397993401/archive/2010/05/20/115966.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>hdu 1285 紜畾姣旇禌鍚嶆http://www.shnenglu.com/397993401/archive/2010/05/19/115856.html浠樼繑浠樼繑Wed, 19 May 2010 15:56:00 GMThttp://www.shnenglu.com/397993401/archive/2010/05/19/115856.htmlhttp://www.shnenglu.com/397993401/comments/115856.htmlhttp://www.shnenglu.com/397993401/archive/2010/05/19/115856.html#Feedback0http://www.shnenglu.com/397993401/comments/commentRss/115856.htmlhttp://www.shnenglu.com/397993401/services/trackbacks/115856.html
鍜屽叾浠栫敤鐭╅樀鐨勭◢鏈変笉鍚?br>
//閲囩敤涓寸晫琛ㄧ殑褰㈠紡 浣嗘槸鐢ㄦ暟緇勬潵瀹炵幇 鍏朵腑瑕佺敤C++ 鐨勯槦鍒梥tl 灝介噺浣夸唬鐮佺殑鍙敤搴︽彁楂?br>// 緗戜笂澶у閲囩敤 涓存帴鐭╅樀鐨勬柟寮忔潵鍋氱殑
# include<cstdio>
# include
<iostream>
# include
<queue>
# include
<vector>
using namespace std;
const int maxn = 510;
struct graph
{
    
int edge[maxn][maxn];//edge[i][j] 琛ㄧず絎琲涓妭鐐?nbsp;絎?nbsp;j+1涓竟 鍏跺艱〃紺鴻杈圭殑欏剁偣
    int outdegree[maxn +1];//  欏剁偣鐨勫嚭搴?/span>
    int nvert ;//鑺傜偣鏁?/span>
    int nedge;// 杈規暟  娉ㄦ剰瀹氱偣鎴戜滑鏄粠1 寮濮嬬畻 鐒跺悗杈規暟鎴戜滑鏄粠0 寮濮嬬殑
};
typedef 
struct graph GRAPH;

struct graph g;// 鍥劇殑鍏ㄥ眬鍙橀噺

void init_graph()
{
    
int i;
    g.nedge 
= 0;
    g.nvert 
= 0;
    
for (i = 1; i <=maxn ; i ++ ) g.outdegree[i] = 0;
}
void inert_graph(int x,int y,bool flag )
{
    
//if(g.outdegree[x] > maxn )//瀹歸敊
    g.edge[x][g.outdegree[x]] = y;
    g.outdegree[x] 
++;//鍑哄害 + 1
    if (flag == false)  inert_graph(y,x,true);//濡傛灉鏄棤鍚戝浘 鍙嶅悜涔熻鍔?/span>
    else g.nedge ++;//杈規暟 ++

}
int read_graph(bool flag) //flag 鐢ㄦ潵鎺у埗 鏄惁鏄湁鍚戝浘
{
    
//榪欓噷鍙互鏍規嵁瀹為檯鎯呭喌娣誨姞浠g爜
    int n,i;
    
int x,y;
    init_graph();
    
if (scanf("%d%d",&g.nvert,&n)==EOF)
        
return 0;
    
for (i =1; i <= n; i ++)
    {
        scanf(
"%d%d",&x,&y);
        inert_graph(x,y,
true);//鏄湁鍚戝浘
    }
    
return 1;
}

//鎷撴墤鎺掑簭  涓嬮潰鏄痶op 鎺掑簭鎵闇瑕佺殑涓滀笢
int sorted[maxn];
int indegree[maxn]; //璁$畻鍏ュ害
int visted[maxn];
void topsorted()
{
    queue
<int> zeroin;
    
int x,y;
    
int i,j;
    
//鍒濊瘯鍖?nbsp;鍏ュ害鏁扮粍
    for (i = 1; i <= maxn ; i ++)  indegree[i] = 0;
    
//璁$畻鍏ュ害
    for (i = 1; i <= g.nvert ; i++ )
        
for (j = 0; j < g.outdegree[i]; j++// 鎵浠ヨ繖閲屾垜浠槸浠?nbsp;0 寮濮?/span>
            indegree[g.edge[i][j]] ++;

    
for(i = 1; i <= g.nvert ; i ++)
    {
    
//for (i = g.nvert; i >= 1 ; i --)
        if (indegree[i] == 0) zeroin.push(i);
    }
    j 
= 0;
    memset(visted,
0,sizeof(visted));
    
for(j = 1;j <= g.nvert;j++)//  鍥犱負棰樼洰鐨勫師鍥?鏀懼純闃熷垪
    {
        
for(i = 1;i<=g.nvert;i++ )
        {
            
if(visted[i] == 0 && indegree[i] == 0)
            {
                sorted[j] 
= i,visted[i] = 1;
                
for(int k = 0;k< g.outdegree[i];k++)
                    indegree[g.edge[i][k]]
--;
                
break;
            }
        }
    }

    
//if(j != g.nvert) ;//琛ㄦ槑鍙湁 j涓畾鐐規壘鍒?/span>
}

void print_graph()
{
    
int i,j;
    
for (i = 1; i <= g.nvert; i ++)
    {
        printf(
"%d: ",i);
        
for (j = 0; j < g.nvert ; j ++)
            printf(
" %d",g.edge[i][j]);
        printf(
"\n");
    }
}
int main()
{
    freopen(
"in.txt","r",stdin);

    
while (read_graph(true)==1)
    {

        topsorted();
        
//print_graph();
        for (int i = 1; i <= g.nvert; i ++)
            printf(i
==1?"%d":" %d",sorted[i]);
        printf(
"\n");
    }
    
return 0;
}





浠樼繑 2010-05-19 23:56 鍙戣〃璇勮
]]>
久久久久亚洲国产| 久久婷婷综合中文字幕| 欧美日韩中文字幕久久久不卡| 99久久精品免费看国产| 亚洲精品无码久久久| 亚洲va久久久噜噜噜久久| 九九精品99久久久香蕉| 久久se精品一区二区| 精品国产青草久久久久福利 | 久久综合视频网| 久久99精品久久久久久久久久| 999久久久国产精品| 97久久国产露脸精品国产| 狠狠色丁香婷婷久久综合不卡| 欧美日韩成人精品久久久免费看| 久久国产色AV免费观看| 亚洲欧洲久久av| 88久久精品无码一区二区毛片| 久久人人爽人人爽人人av东京热| 国产精品久久免费| 无码人妻少妇久久中文字幕蜜桃| 久久精品中文字幕第23页| 久久久久亚洲av无码专区导航 | 久久综合综合久久狠狠狠97色88| 亚洲伊人久久成综合人影院 | 久久久久久久免费视频| 国产精品欧美久久久久天天影视| 亚洲伊人久久精品影院| 久久久久久亚洲精品不卡| 好属妞这里只有精品久久| 日韩精品久久久久久久电影蜜臀| 伊人久久大香线蕉AV一区二区| 精品久久人人做人人爽综合| 国内精品久久九九国产精品| 久久精品无码一区二区无码 | 一97日本道伊人久久综合影院| 国内精品久久久久国产盗摄| 一级做a爰片久久毛片人呢| 久久99久久99小草精品免视看| 精品久久久久久无码专区| 久久久久久毛片免费播放|