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

求大神帮看看,为嘛prim会超时!!!???

Posted by 1310618 at 2016-07-09 12:49:21 on Problem 1789
//MST 倒数最大
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define INF 10//最大权为7
int graph[2001][2001];
char str[2001][8];
int weight(int i,int j){
	int w=0;
	for(int k=0;k<7;k++)
		if(str[i][k]!=str[j][k])
			w++;
	return w;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF&&n!=0){
		/*for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				graph[i][j]=INF;*/
		memset(graph,INF,sizeof(graph));
		for(int i=1;i<=n;i++)
		{
			cin>>str[i];
		}
		for(int i=1;i<=n-1;i++)
		{
			for(int j=i+1;j<=n;j++){
				graph[i][j]=graph[j][i]=weight(i,j);
			}
		}
		//begin prim
		int set[2001];int nset=1;set[1]=1;
		int edge=1;int sum=0;
		while(edge<=n-1){
			int min=INF;int l,r;
			for(int i=1;i<=nset;i++){
				for(int j=1;j<=n;j++)
				{
					if(min>graph[set[i]][j])
					{
						min=graph[set[i]][j];
						l=set[i];
						r=j;
					}
				}
			}
			sum+=min;
			set[++nset]=r;
			edge++;
			for(int i=1;i<=nset;i++){
				for(int j=1;j<=nset;j++){
					graph[set[i]][set[j]]=graph[set[j]][set[i]]=INF;
				}
			}
		}
		cout<<"The highest possible quality is 1/"<<sum<<'.'<<endl;
	}
	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