zoukankan      html  css  js  c++  java
  • (简单) POJ 2502 Subway,Dijkstra。

      Description

      You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
      You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
     
      这个题目是最短路问题,建边的话就是地铁线上的是直线距离除以车的速度,其他的话是除以走的速度,是一个完全图。。。
     
    代码如下:
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
        
    using namespace std;
    
    const int INF=10e8;
    
    bool vis[300];
    
    void Dijkstra(double lowcost[],double cost[][300],int N,int start)
    {
        for(int i=1;i<=N;++i)
        {
            lowcost[i]=INF;
            vis[i]=0;
        }
        lowcost[start]=0;
    
        int k;
        double minn;
    
        for(int cas=1;cas<=N;++cas)
        {
            minn=INF;
            k=-1;
    
            for(int i=1;i<=N;++i)
                if(!vis[i] && minn>lowcost[i])
                {
                    minn=lowcost[i];
                    k=i;
                }
    
            if(k==-1)
                return;
    
            vis[k]=1;
    
            for(int i=1;i<=N;++i)
                if(!vis[i] && cost[k][i]>=0 && lowcost[i]>lowcost[k]+cost[k][i])
                    lowcost[i]=lowcost[k]+cost[k][i];
        }
    }
    
    int X[300],Y[300];
    int N;
    double map1[300][300];
    double ans[300];
    
    double getdis(int a,int b)
    {
        return sqrt((double(X[a]-X[b]))*(X[a]-X[b])+(double(Y[a]-Y[b]))*(Y[a]-Y[b]));
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        for(int i=1;i<=250;++i)
            for(int j=1;j<=250;++j)
                map1[i][j]=-1;
    
        scanf("%d %d %d %d",&X[1],&Y[1],&X[2],&Y[2]);
        
        N=3;
        int base;
    
        while(~scanf("%d %d",&X[N],&Y[N]))
        {
            base=N;
            while(X[N]!=-1 || Y[N]!=-1)
            {
                ++N;
                scanf("%d %d",&X[N],&Y[N]);
            }
    
            for(int i=base;i<N-1;++i)
                map1[i][i+1]=map1[i+1][i]=3.0*getdis(i,i+1)/2000.0;
        }
        --N;
    
        for(int i=1;i<=N;++i)
            for(int j=1;j<=i;++j)
                if(map1[i][j]<0)
                    map1[i][j]=map1[j][i]=3.0*getdis(i,j)/500.0;
    
        Dijkstra(ans,map1,N,1);
    
        printf("%d
    ",(int)(ans[2]+0.5));
        
        return 0;
    }
    View Code
  • 相关阅读:
    九度oj 题目1525:子串逆序打印
    九度oj 题目1516:调整数组顺序使奇数位于偶数前面
    九度oj 题目1490:字符串链接
    九度oj 题目1438:最小公倍数
    九度oj 题目1181:遍历链表
    九度oj 题目1179:阶乘
    九度oj 题目1077:最大序列和
    九度oj 题目1075:斐波那契数列
    九度oj 题目1074:对称平方数
    九度oj 题目1073:杨辉三角形
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338499.html
Copyright © 2011-2022 走看看