Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:用prim,比并查集快.In Reply To:用prim,比并查集快. Posted by:xfxyjwf at 2005-07-14 04:50:57 用了prim的,在build函数中处理,但是还是超时,本地处理700个数据花了3s,大部分时间像是在处理输入输出 #include <iostream> #include <string.h> using namespace std; #define GetDisDB(x0,y0,x1,y1) ((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)) #define Max 751 typedef struct { int x,y; }Point; int HashConn[Max][Max]; int Map[Max][Max]; int SetConn[Max]; int ConnEndIndex; int SetLeft[Max]; int LeftEndIndex; Point Value[Max]; void SetZero(int a,int b) { Map[a][b] = 0; Map[b][a] = 0; } void RemoveIndexFromLeft(int index) { SetLeft[index] = 0; LeftEndIndex--; } void AddIndexToSetConn(int v) { ConnEndIndex++; SetConn[ConnEndIndex] = v; } void Build(int n) { int a,b,i,j,v; int index = 0; int curi,curj; while(LeftEndIndex) { a = b = 0; v = 0x0FFFFFFF; for(i= 1;i<=ConnEndIndex;i++) for(j=1;j<=n;j++) { if(SetLeft[j]) { curi = SetConn[i]; curj = SetLeft[j]; if((Map[curi][curj]<v)) { a = curi; b = curj; v = Map[curi][curj]; index = j; } } } if(!HashConn[a][b]) cout<<a<<" "<<b<<endl; AddIndexToSetConn(b); RemoveIndexFromLeft(index); } } int main() { int i,j,n,p,a,b; int v,x,y; (cin>>n); { memset(Map,0,sizeof(int)*Max*Max); memset(SetConn,0,Max*sizeof(int)); memset(SetLeft,0,Max*sizeof(int)); memset(Value,0,Max*sizeof(Point)); memset(HashConn,0,sizeof(int)*Max*Max); for(i=1;i<=n;i++) { cin>>x>>y; Value[i].x = x; Value[i].y = y; SetLeft[i] = i; } for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { Map[i][j] = GetDisDB(Value[i].x,Value[i].y,Value[j].x,Value[j].y); Map[j][i] = Map[i][j]; } cin>>p; for(i=1;i<=p;i++) { cin>>a>>b; SetZero(a,b); HashConn[a][b] = 1; HashConn[b][a] = 1; } ConnEndIndex = 0; LeftEndIndex = n; RemoveIndexFromLeft(1); AddIndexToSetConn(1); Build(n); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator