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

求大神,无限WA!

Posted by 2410892305 at 2015-08-27 13:01:02 on Problem 2349 and last updated at 2015-08-27 13:02:11
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int infinity=0x3f3f3f3f;
const int maxnum=505;
double map1[maxnum][maxnum];
bool visited[maxnum];
double low[maxnum];
int nodenum,num;
double dist[maxnum];
struct node
{
    double x;
    double y;
}N[505];
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int compare(double a,double b)
{
    return a>b;
}
void prim()
{
    int i,j,pos=1;
    double Min;
    memset(visited,0,sizeof(visited));//初始化都未标记
    for(i=1;i<=nodenum;i++)
    {
        low[i]=map1[pos][i];
    }
    visited[pos]=1;//把1号作为起点
    for(i=2;i<=nodenum;i++)//这个i没有其他的意思就是一个循环次数
    {
        Min=infinity;pos=-1;//从1号开始找最小的边
        for(j=1;j<=nodenum;j++)
        {
            if(!visited[j]&&Min>low[j])
            {
                Min=low[j];
                pos=j;
            }
        }
        visited[pos]=1;//做到与1os号最近的边
        dist[num++]=Min;
       // cout<<dist[num-1]<<endl;
        for(j=1;j<=nodenum;j++)
        {
            if(!visited[j]&&low[j]>map1[pos][j])
            {
                low[j]=map1[pos][j];//这个就是替换未被标记的最小权值!
            }
        }
    }
}
int main()
{
    int times,s,i,j;
    double ans,ans1;
    cin>>times;
    while(times--)
    {
        num=0;
        cin>>s>>nodenum;
        memset(map1,0,sizeof(map1));
        for(i=1;i<=nodenum;i++)
        {
            cin>>N[i].x>>N[i].y;
        }
        for(i=1;i<nodenum;i++)
        {
            for(j=i+1;j<=nodenum;j++)
            {
                map1[i][j]=map1[j][i]=dis(N[i],N[j]);
            }
        }
        prim();
        sort(dist,dist+num,compare);
        printf("%.2lf\n",dist[nodenum-s-1]);
    }
}

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