Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 26547 | Accepted: 10300 |
Descriptionios
Inputthis
Outputspa
Sample Inputcode
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Outputblog
The highest possible quality is 1/3.
Sourceip
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const int maxn=2010; 6 const int inf=maxn*7; 7 int n; 8 int a[maxn],map[maxn][maxn]; 9 char ch[maxn][10]; 10 bool v[maxn]; 11 int bj(int x,int y){ 12 int ret=0; 13 for(int i=0;i<7;i++) 14 if(ch[x][i]!=ch[y][i]) ret++; 15 return ret; 16 } 17 void intn(){ 18 for(int i=1;i<=n;i++) scanf("%s",ch[i]); 19 for(int i=1;i<=n;i++) 20 for(int j=i+1;j<=n;j++) 21 map[j][i]=map[i][j]=bj(i,j); 22 } 23 int prim(){ 24 memset(v,0,sizeof(v)); 25 int ans=0,p,b; 26 v[1]=1; 27 for(int i=1;i<=n;i++) a[i]=map[1][i]; 28 for(int m=1;m<n;m++){ 29 p=inf; 30 for(int i=1;i<=n;i++) if(a[i]<p&&!v[i]){p=a[i];b=i;} 31 ans+=a[b];v[b]=1; 32 for(int i=1;i<=n;i++) a[i]=min(a[i],map[b][i]); 33 } 34 return ans; 35 } 36 int main(){ 37 while(scanf("%d",&n)){ 38 if(!n) break; 39 else intn(); 40 printf("The highest possible quality is 1/%d.\n",prim()); 41 } 42 return 0; 43 }
以前的prim打的醜,poj一直給我TLE(只有TLE),果真poj逼格這麼高的地方不適合我這樣不會英語,又很嬌弱的蒟蒻。ci
題目來源:POJ字符串