标签:std tin break length blog mil ever build 表示
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23309 | Accepted: 9997 |
Description
Input
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int n,m,x,tot,y,fa[1005]; 7 #define maxm 10010 8 struct node{ 9 int from,to,value; 10 bool operator < (const node& a) const { 11 return value<a.value; 12 } 13 }edge[maxm*4]; 14 void add_edge(int u,int v,int w){ 15 edge[++tot].from=u;edge[tot].to=v;edge[tot].value=w; 16 } 17 int find(int x){ 18 if(x==fa[x]) return x; 19 else return fa[x]=find(fa[x]); 20 } 21 int main() 22 { 23 scanf("%d",&n); 24 for(int i=1;i<=n;i++) 25 for(int j=1;j<=n;j++){ 26 scanf("%d",&x); 27 add_edge(i,j,x); 28 } 29 scanf("%d",&m); 30 for(int i=1;i<=m;i++){ 31 scanf("%d%d",&x,&y); 32 add_edge(x,y,0);add_edge(y,x,0); 33 } 34 sort(edge+1,edge+tot+1); 35 int t=0,ans=0; 36 for(int i=1;i<=n+5;i++) fa[i]=i; 37 for(int k=1;k<=tot;k++){ 38 int x=edge[k].from,y=edge[k].to; 39 int rx=find(x),ry=find(y); 40 if(rx!=ry){ 41 ans+=edge[k].value; 42 fa[rx]=ry; 43 t++; 44 } 45 if(t== n-1 ) break; 46 } 47 printf("%d\n",ans); 48 return 0; 49 }
思路:还用说嘛!!啊!说一下吧,前面的数据建立边表,后面的数据将从x到y、y到x的距离改成0,跑一边Kruskal
标签:std tin break length blog mil ever build 表示
原文地址:http://www.cnblogs.com/suishiguang/p/6347043.html