POJ 1258-Agri Net最小生成ツリーPrimアルゴリズム
タイトルのソース:http://acm.pku.edu.cn/JudgeOnline/problem?id=1258
問題解決報告:
典型的な最小生成樹の問題ですが、今回はPrimアルゴリズムを使いました.queueの使用では、最初に優先キューを使ったが、Decrease auがないことがわかった.key操作はその後Setに変更されましたが、カスタマイズ後に問題が発生しましたので、使えません.最後に直接放棄して、直接O(n)の複雑さを使ってkeyの最小値を探します.効率は下がりましたが、やはり合格しました.
ヽoo.ツ........................................................................define INF 99999999 int n;/ポイント数int A[101][101]//隣接マトリックス表示法int key[101]//ブックフラッグ[101];Prim i(int){intessult=0;for(int i=0;i+i++){key[i]=INF;fag[i]=true;key[r]=0;for(int=i=0;i=======""""""minum=INF;minminum=INF;int=int=minminminminminminum=int=0;int=int=int=0=minminminminminmini=0;int=int=int=int=int=int=0=0;minminminminminminminminminmin=0;int=int=int=int=int=int=int=int=+=key[min];flags[min]=false;for(int j=0;j+){if(flags[j]&key[j]>A[min]、[j])key[j]=A[min]、[j];return reresult;int main(){while("%d"、&n)!=EOF){for(inti=0;;i+){for(int j=0;>A[i];cout
付録:
Agri-Net
Time Limit:1000 MS
メモリLimit:100000 K
Total Submissions:15910
Acceepted:6468
Description
Farmer John has been elected mayor of his town!One of his campaign promises was to bring internet connectivity to all farms in the ara.He needs your help,of course.
Farmer John orded a High speed connection for his farm and is going to sharehis connectivity with the other farmers.To minimze costt,he wants to lay the minimum amount of optical fiber to connect the the therm.farm.the.the.
Given a list of how much fiber it Taes to connect each pair of farms,you must find the minimum amount of fiber need to connect them all together.Each farm must to to to to some other farmt
The distance between any two farms will not exceed 100,000.
Input
The input includies several cases.For each case,the first line contains the number of farms,N(3<=N==100).The follwing inininininininininininininininthe N N conectivitymarix,whehere eeeeleeeeeeeemenshshshemenshshemenshshshs shs shshshshs shows shows shs thethethethedidididididididistastastastastastastathethethe thethe thethe thethethethethethethethethethethethethethethethe didididididididincncncncnce e e e are limited in length to 80 characters、so me line s continue onto others.Of course、the diagonal will be 0、since the distance from farm i to itself is not interesting for this problem.
Output
For each case,output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
問題解決報告:
典型的な最小生成樹の問題ですが、今回はPrimアルゴリズムを使いました.queueの使用では、最初に優先キューを使ったが、Decrease auがないことがわかった.key操作はその後Setに変更されましたが、カスタマイズ後に問題が発生しましたので、使えません.最後に直接放棄して、直接O(n)の複雑さを使ってkeyの最小値を探します.効率は下がりましたが、やはり合格しました.
ヽoo.ツ........................................................................define INF 99999999 int n;/ポイント数int A[101][101]//隣接マトリックス表示法int key[101]//ブックフラッグ[101];Prim i(int){intessult=0;for(int i=0;i+i++){key[i]=INF;fag[i]=true;key[r]=0;for(int=i=0;i=======""""""minum=INF;minminum=INF;int=int=minminminminminminum=int=0;int=int=int=0=minminminminminmini=0;int=int=int=int=int=int=0=0;minminminminminminminminminmin=0;int=int=int=int=int=int=int=int=+=key[min];flags[min]=false;for(int j=0;j+){if(flags[j]&key[j]>A[min]、[j])key[j]=A[min]、[j];return reresult;int main(){while("%d"、&n)!=EOF){for(inti=0;;i+){for(int j=0;
付録:
Agri-Net
Time Limit:1000 MS
メモリLimit:100000 K
Total Submissions:15910
Acceepted:6468
Description
Farmer John has been elected mayor of his town!One of his campaign promises was to bring internet connectivity to all farms in the ara.He needs your help,of course.
Farmer John orded a High speed connection for his farm and is going to sharehis connectivity with the other farmers.To minimze costt,he wants to lay the minimum amount of optical fiber to connect the the therm.farm.the.the.
Given a list of how much fiber it Taes to connect each pair of farms,you must find the minimum amount of fiber need to connect them all together.Each farm must to to to to some other farmt
The distance between any two farms will not exceed 100,000.
Input
The input includies several cases.For each case,the first line contains the number of farms,N(3<=N==100).The follwing inininininininininininininininthe N N conectivitymarix,whehere eeeeleeeeeeeemenshshshemenshshemenshshshs shs shshshshs shows shows shs thethethethedidididididididistastastastastastastathethethe thethe thethe thethethethethethethethethethethethethethethethe didididididididincncncncnce e e e are limited in length to 80 characters、so me line s continue onto others.Of course、the diagonal will be 0、since the distance from farm i to itself is not interesting for this problem.
Output
For each case,output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output28