poj 2253 Frogger 最小生成树

来源:互联网 发布:中控iface702软件 编辑:程序博客网 时间:2024/06/16 19:09

点这里

模板题

oj崩了就没心情改了。。。

只要找出最后一条边就可以了 

#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#define MIN 0x3f3f3f#define MAX_Point 105using namespace std;const int maxn=200005;const int maxm=200005;int F[maxn],p,q;double max1;struct Edge{    int u,v;    double w;}edge[maxm];struct Edge2{    int i,x,y;}edge1[maxm];int tol;void addedge(int u,int v,double w){    edge[tol].v=v;    edge[tol].u=u;    edge[tol++].w=w;}bool cmp(Edge a,Edge b){    return a.w<b.w;}int fnd(int x){    return x==F[x]?x:F[x]=fnd(F[x]);}double kruskal(int n){    for(int i=0;i<n;i++) F[i]=i;   // sort(edge,edge+tol,cmp);    int cnt=0;    double ans=0;    for(int i=0;i<tol;i++)    {        int u=edge[i].u;        int v=edge[i].v;        double w=edge[i].w;        int a=fnd(u);        int b=fnd(v);        if(a!=b)        {            ans+=w;            F[b]=a;            cnt++;        }        if(fnd(0)==fnd(1))        {            max1=w; return 0;        }        if(cnt==n-1)break;    }    if(cnt<n-1)return -1;}int main(){    int n;double x;int d=1;    while(~scanf("%d",&n))    {  if(n==0)  break;           p=0,q=1;       for(int i=0;i<n;i++)       {  int x,y;           edge1[i].i=i;          scanf("%d %d",&edge1[i].x,&edge1[i].y);}          tol=0;        for(int i=0;i<n;i++)       {           for(int j=i+1;j<n;j++)           {             x=sqrt((edge1[i].x-edge1[j].x)*(edge1[i].x-edge1[j].x)+(edge1[i].y-edge1[j].y)*(edge1[i].y-edge1[j].y));           // printf("%.2lf\n",x);             addedge(i,j,x);             addedge(j,i,x);           }       }         sort(edge,edge+tol,cmp);               kruskal(n);        printf("Scenario #%d\n",d++);        printf("Frog Distance = %.3f\n\n",max1);    }    return 0;}