ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1205. By the Underground or by Foot?

Why Dijkstra TLE#3 ??
Posted by Romko [Lviv NU] 13 Apr 2007 01:33
#include <iostream>
#include <cmath>
using namespace std;

struct point
{
    double x;
    double y;
    static double dist(point &q, point &w)
    {
        return (sqrt((q.x - w.x)*(q.x - w.x)+(q.y - w.y)*(q.y - w.y)));
    }
    point():x(0),y(0){}
};
double A[210][210];

double mini(double a,double b)
{
    if(a < b)
        return a;
    else
        return b;
}
#define oo 10000;
bool S[210];
double B[210];
double C[210];
void Dijkstra(int a,int n)
{
    for(int i = 1; i <= n; i++)
    {
        S[i] = 0;
        C[i] = a;
        B[i] = A[a][i];
    }
    B[a] = 0;S[a] = 1;C[a] = 0;
    while(1)
    {
    int min = 10000;
    int ind;
    for(int i = 1; i <= n; i++)
    {
        if(!S[i])
        {
            if(B[i] < min)
            {
                min = B[i];
                ind = i;
            }
        }
    }
    S[ind] = 1;
    for(int k = 1; k <= n; k++)
    {
        if(!S[k])
        {
            if(B[k] > B[ind] + A[ind][k])
            {
                B[k]  = B[ind] + A[ind][k];
                C[k] = ind;
            }
        }
    }
    bool tf = false;
    for(int i = 1; i <= n; i++)
    {
        if(!S[i])
            tf = true;
    }
    if(!tf)
        return;
    }
}
void output(int n)
{
    int z = n;
    int MT[210];
    int i = 0;
    do
    {
        MT[i] = z;
        z = C[z];
        i++;
    }
    while(z);
    cout << i << ' ';
    for(int j = i-1; j >= 0; j--)
        cout << MT[j] << ' ';
}
int main()
{
    int v1,v2;
    cin >> v1 >> v2;
    int n;
    cin >> n;
    point und[210];
    for(int i = 1; i <= n; i++)
        cin >> und[i].x >> und[i].y;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            A[i][j] = oo;
    }
    int a = -1,b= -1;
    while(a!=0 && b != 0)
    {
        cin >> a >> b;
        A[b][a] = point::dist(und[b],und[a]);
        A[a][b] = point::dist(und[a],und[b]);
    }
    point Q,W;
    cin >> Q.x >> Q.y;
    cin >> W.x >> W.y;
    double min1 = 10000;
    int item1,item2;
    for(int i = 1; i <= n; i++)
    {
        if(point::dist(und[i],Q) < min1)
        {
            min1 = point::dist(und[i],Q);
            item1 = i;
        }
    }
    double min2 = 10000;
    for(int i = 1; i <= n; i++)
    {
        if(point::dist(und[i],W) < min2)
        {
            min2 = point::dist(und[i],W);
            item2 = i;
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if((i != j) && (A[i][j] == 10000))
            {
                A[i][j] = point::dist(und[i],und[j])*v1*v2;
                A[j][i] = point::dist(und[j],und[i])*v1*v2;
            }
        }
    }
    Dijkstra(item1,n);
    double tmp = point::dist(Q,W);
    if(tmp/v1 < B[item2]/v2 + min1/v1 + min2/v1)
    {
        printf("%.7lf\n0\n",tmp/v1);

    }
    else
    {
        double res = B[item2]/v2 + min1/v1 + min2/v1;
        printf("%.7lf\n",res);
        output(item2);
    }

    return 0;
}