zoukankan      html  css  js  c++  java
  • 求最小生成树的第i条边——PKU2349

    //printf("%.2lf\n",sqrt((double)a[s]));//以后用sqrt自己小心点,而且要用c++提交

    //郁闷到了没加double就一直错,不说了,而且g++提交也一直错,c++提交才对

    下次看到题目有说用sqrt的时候要小心了,因为精度而产生的问题是比较郁闷的

    ps:用vs2008它会会提示sqrt重载问题……以后有关精度问题时先在vs上运行试试……

    View Code
    #include <iostream>
    #include
    <algorithm>
    #include
    <math.h>
    using namespace std;
    int w,n;
    struct Point
    {
    int x,y;
    }p[
    505];
    int map[505][505];
    int a[105],s[505];
    int dist[505],k;
    int distence(Point a,Point b)
    {
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    bool cmp(int a,int b)
    {
    return a>b;
    }
    double PRIM(int u)
    {
    int rj,d,i,j;
    for(i=1;i<=n;i++)
    {
    dist[i]
    =map[u][i];
    s[i]
    =0;
    }
    s[u]
    =1;
    for(i=1;i<n;i++)//第i条边
    {
    int min=1000000000;
    for(j=1;j<=n;j++)
    {
    if(dist[j]<min&&!s[j])
    {
    min
    =dist[j];
    rj
    =j;
    }
    }
    a[i]
    =min;
    s[rj]
    =1;
    for(j=1;j<=n;j++)
    {
    if(map[rj][j]<dist[j]&&!s[j])
    dist[j]
    =map[rj][j];
    }
    }

    sort(a
    +1,a+n,cmp);
    return sqrt(double(a[w]));
    }
    int main()
    {
    int t,i,j;
    scanf(
    "%d",&t);
    while(t--)
    {
    k
    =0;
    memset(map,
    0,sizeof(map));
    scanf(
    "%d%d",&w,&n);
    for(i=1;i<=n;i++)
    {
    scanf(
    "%d%d",&p[i].x,&p[i].y);
    }
    for(i=1;i<=n;i++)
    {
    for(j=1;j<i;j++)
    {
    map[i][j]
    =map[j][i]=distence(p[i],p[j]);
    }
    }
    printf(
    "%.2lf\n",PRIM(1));
    }
    return 0;
    }
    #include<stdio.h>
    #include
    <math.h>
    #include
    <algorithm>
    using namespace std;

    int g[505][2];
    int map[505][505];
    bool use[505];
    int a[505];
    bool cmp(int a,int b)
    {
    return a>b;
    }
    int main()
    {
    int n,i,j,k,s,t;
    scanf(
    "%d",&t);
    while(t--)
    {
    scanf(
    "%d %d",&s,&n);
    for(i=1;i<=n;i++)
    {
    scanf(
    "%d%d",&g[i][0],&g[i][1]);
    use[i]
    =0;
    }

    int temp;
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    if(i!=j)
    {
    temp
    =(g[i][0]-g[j][0])*(g[i][0]-g[j][0])+(g[i][1]-g[j][1])*(g[i][1]-g[j][1]);
    map[i][j]
    =temp;
    }
    }
    }

    int add=1,min,rj;
    use[
    1]=1;
    for(k=1;k<n;k++)
    {
    min
    =999999999;
    for(i=1;i<=n;i++)
    {
    if(use[i]==1)
    for(j=1;j<=n;j++)
    {
    if(use[j]==0&&map[i][j]<min)
    {
    rj
    =j;
    min
    =map[i][j];
    }
    }
    }
    use[rj]
    =1;
    a[add]
    =min;
    add
    ++;
    }

    sort(
    &a[1],&a[n+1],cmp);
    printf(
    "%.2lf\n",sqrt((double)a[s]));//郁闷到了没加double就一直错,不说了
    }
    }
  • 相关阅读:
    asp.net 2.0 run
    Regular Expression
    assembly
    asp.net loading..
    session
    asp.net performance
    asp.net page order
    interface
    UVA 562 Dividing coins
    UVA 10003 Cutting Sticks
  • 原文地址:https://www.cnblogs.com/huhuuu/p/1957351.html
Copyright © 2011-2022 走看看