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算法,但是memset清零时出错了,大牛能告诉下原因么?

Posted by huanglj at 2008-09-29 21:30:04 on Problem 2421
在这个函数中,我定义了一个flag[100]的标志,用memset清0;结果用prim做的时候wa的无数次。后来我把这个用for循环改了下,马上就通过了。。。
大家能够帮忙解释下为什么么?谢谢大牛了

#include <iostream>

using namespace std;
int n;
int q;
struct point
{
    int line;
    int row;
};
//point a[5050];
int b[100][100];
int flag[100];
int opt[100];//已经选择了的点。
int main()
{
    int i,j;
    while(cin>>n)
    {
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin>>b[i][j];
			}
		}
		cin>>q;
		if(q!=0)
		{
			int x,y;
			for(i=0;i<q;i++)
			{
				//      cin>>a[i].line>>a[i].row;
				cin>>x>>y;
				b[x-1][y-1]=0;
				b[y-1][x-1]=0;
			}
		}     
	 //	memset(flag,0,100);
        flag[0]=1;
        for(i=1;i<n;i++)
        {
            flag[i]=0;
        }
		memset(opt,0,100);
		opt[0]=0;
	//	flag[0]=1;
		int p=0;
		int sum=0;
		
		int ver=0;
		while(p!=n-1)
		{
  			int min=1000001;
            for(i=0;i<=p;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(!flag[j]&&b[opt[i]][j]<min)
                    {
                        min=b[opt[i]][j];
                        ver=j;
                    }
                }
            }

			sum+=min;
	  		opt[++p]=ver;
			flag[ver]=1;
		}
		cout<<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