标签:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define debug #if defined debug #define CDBG(format,...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, ##__VA_ARGS__) int open_file(){ FILE *fp ; if((fp = freopen("input3.txt","r",stdin)) == NULL){ CDBG("error in freopen"); return -1; } CDBG("success in freopen"); return 0; } #else #define CDBG(format,...) do{}while(0) #endif #define N 510 int n,k,pre[N]; struct point { int x; int y; int len; }p[N*N]; /*int cmp(const void * x,const void * y) { return ((point*)x)->len>((point*)y)->len?1:-1; }*/ void swap(point &x,point &y) { point temp; temp = x; x = y; y = temp; // 从指针改成数组就行了 } int choose_pivot(int i,int j ) { return((i+j) /2); } //会超时 void quicksort(point list[],int m,int n) { int key,i,j,k; if( m < n) { k = choose_pivot(m,n); if(m != k)
swap(list[m],list[k]); key = list[m].len; i = m+1; j = n; while(i <= j) { while((i <= n) && (list[i].len <= key)) i++; while((j >= m) && (list[j].len > key)) j--; if( i < j) swap(list[i],list[j]); } // 交换两个元素的位置 if(m != j)
swap(list[m],list[j]); // 递归地对较小的数据序列进行排序 quicksort(list,m,j-1); quicksort(list,j+1,n); } } int cmp(point x,point y) { return x.len<y.len; } int find(int x) { while(x!=pre[x]) x=pre[x]; return x; } void kruskal() { int i,mix,a,b; mix=0; //sort(p,p+k,cmp); for(i = 0; i < k ;i++) CDBG("%d ",p[i].len); quicksort(p,0,k-1); //跟 sort 结果完全不一样 for(i = 0; i < k ;i++) CDBG("%d ",p[i].len); for(i=1;i<=n;i++) pre[i]=i; for(i=0;i<k;i++) { a=find(p[i].x); b=find(p[i].y); if(a!=b) { if(p[i].len>mix) mix=p[i].len; pre[b]=a; } } cout<<mix<<endl; } void input() { scanf("%d",&n); k=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { scanf("%d",&p[k].len); if(i != j && p[k].len != 0){ p[k].x=i; p[k].y=j; k++; //过滤掉ii 点 } } } int main() { int T; #ifdef debug if(open_file() != 0) return -1; #endif scanf("%d",&T); while(T--) { input(); kruskal(); } return 0; }
Kruskal:
#include<stdio.h> #include<string.h> #define debug #if defined debug #define CDBG(format,...) printf("File:"__FILE__", Line:%05d: "format"\n", __LINE__, ##__VA_ARGS__) #else #define CDBG(format,...) do{}while(0) #endif #define N 511 // n -> [3,500] #define inf 0x3f3f3f3f //distance -> [1,65536] int villages[N][N]; int visited[N]; int lowcost[N]; int open_file(){ FILE *fp ; if((fp = freopen("input3.txt","r",stdin)) == NULL){ CDBG("error in freopen"); return -1; } CDBG("success in freopen"); return 0; } void reset_visited(int n){ int i = 1; for( i = 1; i<= n; i++){ visited[i] = 0; lowcost[i] = 0;} } //下面2个方法都可以 int prim(int start_p,int n){ int i,j,pos,min = 0,result = 0; reset_visited(n); //start at point 1 visited[start_p] = 1; pos = start_p; //set low array from point 1 to other point for(i = 1; i <= n; i++) if(i != pos) { lowcost[i] = villages[pos][i]; CDBG("set lowcost[%d] = %d",i,lowcost[i]); } //check n-1 times again for(i = 1; i < n; i++){ // 使用 while循环代替 for 容易有一些问题 //find min distance 寻找下一个最短距离 min = inf; for(j = 1; j <= n; j++){ if(visited[j] == 0 && lowcost[j] < min) { min = lowcost[j];pos = j; CDBG("min = %d pos = %d j = %d",min,pos,j); } } CDBG("min = %d pos = %d ",min,pos); //add min distance; if(result < min) result = min; visited[pos] = 1; //update for(j = 1; j <= n; j++){ if(visited[j] == 0 && lowcost[j] > villages[pos][j]) { lowcost[j] = villages[pos][j]; CDBG("update lowcost[%d] = %d",j ,lowcost[j]); } } } return result; } int prim(int n) { int u = 1; int i,j,k,start,min,max; //memset(vis,0,sizeof(vis)); reset_visited(n); for(i=1;i<=n;i++) if(i!=u) lowcost[i]=villages[1][i]; visited[1]=true; k=0; min=0;max=0; for(i=1;i<n;i++) //n-1条边 { min=inf; for(j=1;j<=n;j++) if(lowcost[j]<min&&!visited[j]) { min=lowcost[j]; k=j; } if(min>max) max=min; visited[k]=true; for(j=1;j<=n;j++) if(lowcost[j]>villages[k][j]&&!visited[j]) { lowcost[j]=villages[k][j]; } } return max; //cout<<max<<endl; } void read_villages(int n){ for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { villages[i][j] = 0; scanf("%d",&villages[i][j]); } } void check_villages(int n){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++) printf("%d ",villages[i][j]); // 应该是这个导致了 Output Limit Exceeded printf("\n"); } } int main(){ int T,n; #ifdef debug if(open_file() != 0) return -1; #endif scanf("%d",&T); while(T--){ scanf("%d",&n); read_villages(n); //check_villages(n); int ans=0; ans = prim(2,n); // 可以从任意一点开始 //ans = prim(n); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/Pitter99/p/4795668.html