POJ -- 3037 Skiing

来源:互联网 发布:少女前线机枪数据 编辑:程序博客网 时间:2024/06/15 10:18

代码实现:

Dijkstra:

#include<stdio.h>#include<string.h>#include<math.h>#include<queue>using namespace std;const int maxn=10010;int r,c,top,visit[maxn];double v,dis_t[maxn],w[maxn][maxn];struct Edge{    int num;    double time;    Edge *next;    Edge(int n=0,double t=0,Edge *p=0):num(n),time(t),next(p){}}*h[maxn],e[4*maxn];bool operator<(Edge a,Edge b){    return a.time>b.time;}void Addedge(int from,int to,double t){    Edge *p=&e[top++];    p->num=to;    p->time=t;    p->next=h[from];    h[from]=p;}void Dijkstra(){    priority_queue<Edge> q;    memset(visit,0,sizeof(visit));    for(int i=1;i<=r*c;i++) dis_t[i]=1e13;    Edge next;    dis_t[1]=0;    q.push(Edge(1,0));    while(!q.empty()){        int x=q.top().num;        q.pop();        //printf("*%d %.2lf %.2lf\n",head.num,head.time,dis_t[x]);        if(visit[x]) continue;        visit[x]=1;        if(x==r*c) break;        for(Edge *p=h[x];p;p=p->next){            next=*p;            if(!visit[next.num]&&dis_t[next.num]>dis_t[x]+next.time){                dis_t[next.num]=dis_t[x]+next.time;                next.time=dis_t[next.num];                q.push(next);            }        }    }}int main(){    while(~scanf("%lf%d%d",&v,&r,&c)){        memset(h,0,sizeof(h));        top=0;        double e;        for(int i=1;i<=r;i++){            for(int j=1;j<=c;j++)                scanf("%lf",&w[i][j]);        }        for(int i=1;i<=r;i++){            for(int j=1;j<=c;j++){                int x=(i-1)*c+j;                double t=1.0/(v*pow(2.0,w[1][1]-w[i][j]));                //printf("*%.2lf ",t);                if(i-1>=1) Addedge(x,x-c,t);                if(i+1<=r) Addedge(x,x+c,t);                if(j-1>=1) Addedge(x,x-1,t);                if(j+1<=c) Addedge(x,x+1,t);            }        }        /*for(int i=1;i<=9;i++){            for(Edge *p=h[i];p;p=p->next)                printf("%d %.2lf ",p->num,p->time);            printf("\n");        }*/        Dijkstra();        //printf("%.2lf %.2lf %.2lf %.2lf\n",dis_t[1],dis_t[2],dis_t[5],dis_t[8]);        printf("%.2lf\n",dis_t[r*c]);    }}


SPFA:

#include<stdio.h>#include<string.h>#include<math.h>#include<queue>using namespace std;const int maxn=10010;int r,c,top,visit[maxn];double v,dis_t[maxn],w[maxn][maxn];struct Edge{    int num;    double time;    Edge *next;    Edge(int n=0,double t=0,Edge *p=0):num(n),time(t),next(p){}}*h[maxn],e[4*maxn];void Addedge(int from,int to,double t){    Edge *p=&e[top++];    p->num=to;    p->time=t;    p->next=h[from];    h[from]=p;}void SPFA(){    queue<Edge> q;    memset(visit,0,sizeof(visit));    for(int i=1;i<=r*c;i++) dis_t[i]=1e13;    Edge next;    dis_t[1]=0;    visit[1]=1;    q.push(Edge(1,0));    while(!q.empty()){        int x=q.front().num;        q.pop();        visit[x]=0;        for(Edge *p=h[x];p;p=p->next){            next=*p;            if(dis_t[next.num]>dis_t[x]+next.time){                dis_t[next.num]=dis_t[x]+next.time;                if(!visit[next.num]){                    visit[next.num]=1;                    q.push(next);                }            }        }    }}int main(){    while(~scanf("%lf%d%d",&v,&r,&c)){        memset(h,0,sizeof(h));        top=0;        double e;        for(int i=1;i<=r;i++){            for(int j=1;j<=c;j++)                scanf("%lf",&w[i][j]);        }        for(int i=1;i<=r;i++){            for(int j=1;j<=c;j++){                int x=(i-1)*c+j;                double t=1.0/(v*pow(2.0,w[1][1]-w[i][j]));                //printf("*%.2lf ",t);                if(i-1>=1) Addedge(x,x-c,t);                if(i+1<=r) Addedge(x,x+c,t);                if(j-1>=1) Addedge(x,x-1,t);                if(j+1<=c) Addedge(x,x+1,t);            }        }        SPFA();        //printf("%.2lf %.2lf %.2lf %.2lf\n",dis_t[1],dis_t[2],dis_t[5],dis_t[8]);        printf("%.2lf\n",dis_t[r*c]);    }}


0 0
原创粉丝点击