标签:logic val mem amp accept its sam require freopen
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 60255 | Accepted: 24985 |
Description
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
Source
1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <iostream> 9 #include "algorithm" 10 using namespace std; 11 typedef long long LL; 12 const int MAX=105; 13 int n,m; 14 int a[MAX][MAX],fa[MAX*MAX]; 15 struct Edge{ 16 int u,v; 17 int val; 18 bool operator <(const Edge &xx) const { 19 return val<xx.val; 20 } 21 }edge[MAX*MAX]; 22 int getfather(int x){ 23 if (fa[x]==x) return x; 24 return fa[x]=getfather(fa[x]); 25 } 26 void init(){ 27 int i,j; 28 for (i=1;i<=n;i++){ 29 for (j=1;j<=n;j++){ 30 scanf("%d",&a[i][j]); 31 } 32 } 33 m=0; 34 for (i=1;i<=n;i++){ 35 for (j=i+1;j<=n;j++){ 36 edge[++m].u=i; 37 edge[m].v=j; 38 edge[m].val=a[i][j]; 39 } 40 } 41 for (i=1;i<=n;i++) 42 fa[i]=i; 43 sort(edge+1,edge+m+1); 44 } 45 int main(){ 46 freopen ("net.in","r",stdin); 47 freopen ("net.out","w",stdout); 48 int i,j,ans; 49 while (~scanf("%d",&n)){ 50 init();ans=0; 51 int tx,ty; 52 for (i=1;i<=m;i++){ 53 tx=getfather(edge[i].u); 54 ty=getfather(edge[i].v); 55 if (tx!=ty){ 56 ans+=edge[i].val; 57 fa[tx]=ty; 58 } 59 } 60 printf("%d\n",ans); 61 } 62 return 0; 63 }
POJ-1258 Agri-Net(kruskal最小生成树)
标签:logic val mem amp accept its sam require freopen
原文地址:http://www.cnblogs.com/keximeiruguo/p/7279413.html