L - Subway(最短路spfa)

L - Subway(最短路spfa)

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. Input Input consists of the x,y coordinates of your home
and your school, followed by specifications of several subway lines.
Each subway line consists of the non-negative integer x,y coordinates
of each stop on the line, in order. You may assume the subway runs in
a straight line between adjacent stops, and the coordinates represent
an integral number of metres. Each line has at least two stops. The
end of each subway line is followed by the dummy coordinate pair
-1,-1. In total there are at most 200 subway stops in the city. Output Output is the number of minutes it will take you to get to school,
rounded to the nearest minute, taking the fastest route.

Sample Input
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1
Sample Output
21

思路

  • 這一題難在輸入和建圖,,,,,
  • 對於建圖,我說一些需要注意的地方:
    1.首先 起點要與所有的火車站 建了一條邊(起點 -> 火車站)權值爲人走過這段距離所用的時間
    2.汽車 所有的火車站與終點 建了一條邊 (火車站-> 終點)權值爲人走過這段距離所用的時間
    3. 這題一條最需要注意: 在任意一組 車站點之間,相鄰的 站點之間 要建立雙向邊(權值爲:火車的行走的時間)
    4. 所有火車站之間應該 也應該建了 雙向邊,這個邊的權值(時間),一定是人走過兩個車站的時間

題解一

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;

#define INF 0x3f3f3f3f
#define Pir pair<double, double>
const int maxm = 100005;

struct Pos
{
    double x,y;
} pos[maxm];
int k = 2;
double Dis(Pos a, Pos b)
{
    return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) );
}

struct Edge
{
    int v;
    double w;
    int next;
} edge[maxm];
int head[maxm];
double dis[maxm];
int cnt = 0;

void Add(int u, int v, double w)
{
    edge[++ cnt] = (Edge){ v, w, head[u]}; head[u] = cnt;
}

double Spfa(int s, int e)
{
    int use[maxm];
    for(int i = 0; i <= k; i ++)
        dis[i] = INF, use[i] = 0;
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    int u,v;
    double w;
    while(! q.empty())
    {
        u = q.front(); q.pop();
        use[u] = 0;

        for(int i = head[u]; i; i = edge[i].next)
        {
            v = edge[i].v;
            w = edge[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(! use[v])
                {
                    q.push(v);
                    use[v] = 1;
                }
            }
        }
    }
    return dis[e];
}



int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    //freopen("T.txt","r",stdin);
    cin >> pos[0].x >> pos[0].y >> pos[1].x >> pos[1].y;
    double x,y;
    while(cin >> x >> y)
    {
        pos[k].x = x; pos[k ++].y = y;
        while(cin >> x >> y && (x!=-1 && y!=-1))
        {
            pos[k].x = x; pos[k].y = y;
            Add(k - 1, k, Dis(pos[k-1], pos[k])/2000*3);
            Add(k, k - 1, Dis(pos[k], pos[k-1])/2000*3);
            k ++;
        }
    }

    for(int i = 0; i < 2; i ++)
    {
        for(int j = 2; j < k-1; j ++)
        {
            if(i == 0)
                Add(i, j, Dis(pos[i], pos[j])/500*3);
            if(i == 1)
                Add(j, i, Dis(pos[i], pos[j])/500*3);
        }
    }

    for(int i = 0; i < k; i ++)
        for(int j = i + 1; j < k; j ++)
        {
            Add(i, j, Dis(pos[i], pos[j])/500*3);
            Add(j, i, Dis(pos[i], pos[j])/500*3);
        }
    Add(0, 1, Dis(pos[0], pos[1])/500*3);
    cout << int( Spfa(0, 1) + 0.5) << endl;

    return 0;
}

題解二(別人的)

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue>
#include<stack>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 205;
const double wsp = 10 * 1000 / 60;
const double ssp = 40 * 1000 / 60;

struct Node{
    double x, y;
}node[MAXN];

struct ff{
    int x, d;
    ff(){}
    ff( int a, double b ){ x = a; d = b; }
    bool operator <( const ff & a )const{
        return d > a.d;
    }
};

int cnt;
double cost[MAXN][MAXN];
double dis[MAXN];

double gdis( int pre, int pos ){
    double dx = node[pre].x - node[pos].x;
    double dy = node[pre].y - node[pos].y;
    return sqrt( dx * dx + dy * dy );
}

void dij(){
    for( int i = 1; i < MAXN; i++ )
        dis[i] = INF;
    dis[1] = 0;

    priority_queue<ff> Q;
    Q.push( ff( 1, dis[1]) );

    while( !Q.empty() ){
        ff temp = Q.top(); Q.pop();
        int x = temp.x;
        if( temp.d > dis[x] ) continue;
        for( int i = 1; i < cnt; i++ ){
            if( dis[i] > dis[x] + cost[x][i] ){
                dis[i] = dis[x] + cost[x][i];
                Q.push( ff( i, dis[i] ) );
            }
        }
    }
}

int main(){
    ios::sync_with_stdio( false );

    for( int i = 0; i < MAXN; i++ )
        for( int j = 0; j < MAXN; j++ )
            cost[i][j] = INF;

    cin >> node[1].x >> node[1].y >> node[2].x >> node[2].y;
    cnt = 3;

    while( cin >> node[cnt].x >> node[cnt].y ){
        cnt++;
        while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == -1 && node[cnt].y == -1 ) ){
            cost[cnt][cnt - 1] = cost[cnt - 1][cnt] = gdis( cnt - 1, cnt ) / ssp;
            cnt++;
        }
    }

    for( int i = 1; i < cnt - 1; i++ ){
        cost[i][i] = 0;
        for( int j = i + 1; j < cnt; j++ ){
            cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp );
        }
    }

    dij();

    cout << int( dis[2] + 0.5 );
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章