锘??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.
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) {
tarjan(u) { DFN[u]=Low[u]=++Index // 涓鴻妭鐐箄璁懼畾嬈″簭緙栧彿鍜孡ow鍒濆?br> Stack.push(u) // 灝嗚妭鐐箄鍘嬪叆鏍堜腑 for each (u, v) in E // 鏋氫婦姣忎竴鏉¤竟 if (v is not visted) // 濡傛灉鑺傜偣v鏈璁塊棶榪?br> tarjan(v) // 緇х畫鍚戜笅鎵?br> Low[u] = min(Low[u], Low[v]) else if (v in S) // 濡傛灉鑺傜偣v榪樺湪鏍堝唴 Low[u] = min(Low[u], DFN[v]) if (DFN[u] == Low[u]) // 濡傛灉鑺傜偣u鏄己榪為氬垎閲忕殑鏍?br> repeat v = S.pop // 灝唙閫鏍堬紝涓鴻寮鴻繛閫氬垎閲忎腑涓涓《鐐?br> print v until (u== v) }
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) return0; for (i =1; i <= n; i ++) { scanf("%d%d",&x,&y); inert_graph(x,y,true);//鏄湁鍚戝浘 } return1; }
//鎷撴墤鎺掑簭 涓嬮潰鏄痶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"); } return0; }