Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

从一开始kruskal就T学了prim还T就是因为用了map!!!T的同志们注意了

Posted by WNSGB at 2016-10-26 18:52:24 on Problem 1789
//prim(稠密图)AC! 
//code by WNSGB 
//#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=2000+10;

int n,w,ans=0;
int a[maxn][maxn],d[maxn],vis[maxn];

char s[maxn][10];
//map<int,string>type;

void prim(){
	int p;
	for(int i=2;i<=n;i++){
		int INF=1e9;
		for(int j=2;j<=n;j++)
			if(!vis[j]&&d[j]<INF){
				p=j;
				INF=d[j];
			}
		vis[p]=1;
		ans+=d[p];	
		for(int j=2;j<=n;j++){
			if(!vis[j]){
				d[j]=min(d[j],a[p][j]);
			}
		}
	}
}

int main(){
	while(scanf("%d",&n)&&n!=0){
		//memset(d,0,sizeof(d));
		memset(vis,0,sizeof(vis));
		memset(a,127,sizeof(a));
		//memset(type,0,sizeof(type));
		//type.clear();
		//scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%s",s[i]);
			//type[i]=s;
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				w=0;
				for(int k=0;k<=6;k++){
					if(s[i][k]!=s[j][k])
					w++;
				}
				a[i][j]=w;
				a[j][i]=w;
			}
		}
		
		for(int i=1;i<=n;i++)d[i]=a[1][i];
		ans=0;
		d[1]=0,vis[1]=1;
	
		prim();
		
		printf("The highest possible quality is 1/%d.\n",ans);
	}
	return 0;
} 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator