Poj 2253 Frogger

Source: Internet
Author: User

Multiple methods can be understood as the shortest path deformation.

It can also be used as the Minimum Spanning Tree.

It is easy to understand the meaning of the question.


The maximum Hop Distance between frog a and frog B.

Two shortest paths are written. Spfa and Dijkstra.

By the way, you should be familiar with the minimal spanning tree and write a Kruskal.


Spfa:

#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6using namespace std;int n,m;struct node{    double x,y;}l[201];struct lx{    int v;    double d;};vector<lx> g[201];void SPFA(int start){    double dis[201];    bool vis[201];    for(int i=1;i<=n;i++)        dis[i]=INF,vis[i]=0;    queue<int>q;    dis[start]=0,vis[start]=1;    q.push(start);    while(!q.empty())    {        int u=q.front();q.pop();        vis[u]=0;        for(int j=0;j<g[u].size();j++)        {            int v=g[u][j].v;            double d=g[u][j].d;            if(dis[v]>max(dis[u],d))            {                dis[v]=max(dis[u],d);                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    printf("Scenario #%d\nFrog Distance = %.3f\n\n",m++,dis[2]);}int main(){    m=1;    while(scanf("%d",&n),n)    {        for(int i=1;i<=n;i++)        {            scanf("%lf%lf",&l[i].x,&l[i].y);            g[i].clear();        }        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                double d=sqrt(pow(l[i].x-l[j].x,2)+pow(l[i].y-l[j].y,2));                lx now;                now.d=d;                now.v=j;g[i].push_back(now);                now.v=i;g[j].push_back(now);            }        }        SPFA(1);    }}




Dijkstra:


#include <cstdio>#include <cmath>#include <algorithm>#define inf 0x7ffffff#define MAXV 210using namespace std;struct lx{    double x,y;} point[MAXV];double d[MAXV];int n;void dijkstra(){    int i,j,vis[MAXV],v=1;    double min;    for(i=1; i<=n; i++)    {        d[i]=inf;        vis[i]=0;    }    d[1]=0;    for(i=1; i<=n; i++)    {        min=inf;        for(j=1; j<=n; j++)            if(!vis[j] && d[j]<min)            {                min=d[j];                v=j;            }        vis[v]=1;        if(v==2) break;        for(j=1; j<=n; j++)        {            double len=sqrt(pow(point[v].x-point[j].x,2)+pow(point[v].y-point[j].y,2));            if(!vis[j] && d[j]>max(d[v],len))            {                d[j]=max(d[v],len);            }        }    }}int main(){    int i,cnt=1;    while(scanf("%d",&n) ,n)    {        for(i=1; i<=n; i++) scanf("%lf%lf",&point[i].x,&point[i].y);        dijkstra();        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",cnt++,d[2]);    }    return 0;}



Kruskal:


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6using namespace std;int n,m;int fa[201];struct lx{    double x,y;}point[201];struct edge{    int u,v;    double len;}l[20101];int father(int x){    if(x!=fa[x])        fa[x]=father(fa[x]);    return fa[x];}bool cmp(edge a,edge b){    return a.len<b.len;}int main(){    int m=1;    while(scanf("%d",&n),n)    {        for(int i=1;i<=n;i++)            scanf("%lf%lf",&point[i].x,&point[i].y);        for(int i=1;i<=n;i++)            fa[i]=i;        int edgecot=0;        for(int i=1;i<=n;i++)            for(int j=i+1;j<=n;j++)        {            l[edgecot].u=i;            l[edgecot].v=j;            l[edgecot++].len=sqrt(pow(point[i].x-point[j].x,2)+pow(point[i].y-point[j].y,2));        }        sort(l,l+edgecot,cmp);        double ans=0;        for(int i=0;i<edgecot;i++)        {            int x=l[i].u;            int y=l[i].v;            double len=l[i].len;            int fx=father(x);            int fy=father(y);            if(fx==fy)continue;            fa[fy]=fx;            if(father(1)==father(2))            {                ans=len;break;            }        }        printf("Scenario #%d\nFrog Distance = %.3f\n\n",m++,ans);    }}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.