POJ 2253 Frogger 解题报告

Question Link

ACcode(Dijkstra)

Solution 1

#include
#include
#include
#include
#define N 205
#define inf 0x3f3f3f
using namespace std;
double e[N][N],dis[N];
int vis[N],x[N],y[N],n;
double edis(int i, int j){
	return 	sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
}
void dij(int s,int l)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dis[i]=edis(s,i);        
    vis[s]=1;
    for(int i=1;idis[j])
	        {
	            k=j;
	            minn=dis[j];
	        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
            dis[j]=min(dis[j],max(dis[k],edis(k,j)));
    }
    return ;
}
int main()
{
//	freopen("1.txt","r",stdin);	
    int t=1;
    while(~scanf("%d",&n)&&n)
    {
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);
        dij(1,2);
        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",t++,dis[2]);
    }
    return 0;
}

Solution 2

#include
#include
#include
#include
#define N 205
#define inf 0x3f3f3f
using namespace std;
double e[N][N],dis[N];
int vis[N],x[N],y[N],n;
void dij(int s,int l)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dis[i]=e[s][i];
    vis[s]=1;
    for(int i=1;idis[j])
	        {
	            k=j;
	            minn=dis[j];
	        }
        vis[k]=1;
        for(int j=1;j<=n;j++)
            dis[j]=min(dis[j],max(dis[k],e[k][j]));
    }
    return ;
}
int main()
{
//	freopen("1.txt","r",stdin);	
    int t=1;
    while(~scanf("%d",&n)&&n)
    {
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);
        for(int i=1;i

Solution 3

Bellman-Ford 写法

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define douinf 987654321
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
#define N 205
using namespace std;
double dis[N];
int vis[N],x[N],y[N],n;
struct node {
	int u,v; 
	double w; 
}e[N*N];int tot; 
double edis(int i, int j){
	return 	sqrt(double(x[i]-x[j])*(x[i]-x[j])+double(y[i]-y[j])*(y[i]-y[j]));
}
void bel(int s,int l)
{
	rep(i,1,n)		dis[i] = douinf; 
	dis[ s ] = 0; 
	for (int k = 1; k <= n-1; k++){
		for (int i = 1; i <= tot; i++){
			if ( dis[ e[i].v ] > max( dis[ e[i].u ] ,  e[i].w ) )
				dis[ e[i].v ]  = max( dis[ e[i].u ] ,  e[i].w ) ;
		}
	}
}

int main()
{
//	freopen("1.txt","r",stdin);	
    int t=1;
    while(~scanf("%d",&n)&&n)
    {
        memset(e,0,sizeof(e));
        for(int i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);
		tot = 0 ;             
        for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++){
//        	if ( i == j )	continue; 
			e[++tot].u = i; 
			e[tot].v = j; 
			e[tot].w = edis(i,j);
        }
        bel(1,2);
        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",t++,dis[2]);
    }
    return 0;
}

你可能感兴趣的:(图论,Bell,Dijkstra)