UVA 1001 Say Cheese(dijkstra算法)

题意:

在实体空间中给出若干个空心的球,在球中可以瞬间移动, 在实体中移动花费与距离成正比的的时间,给出两点求到达的最短时间。

解题思路:

花样建图, 把起点和终点作为半径为0的球加入图中,使用dijkstra算法一搞就好啦!


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3442


Memory: 0 KB   Time: 3 MS
Language: C++ 4.8.2   Result: Accepted
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 110
const int INF = 0x3f3f3f3f;
int n;
typedef pair PP;
priority_queue, greater > que;
double d[MAX_N];
struct point
{
    int x, y, z, r;
}P[MAX_N];
struct node
{
    int to, next;
    double w;
} edge[MAX_N * MAX_N];
int head[MAX_N];
int top = 0;
inline void add_edge(int u, int v, double w)
{
    edge[top].to = v;
    edge[top].w = w;
    edge[top].next = head[u];
    head[u] = top++;
}

double dist(point a, point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)) - a.r - b.r;
}

void dijkstra(int s)
{
    d[s] = 0.0;
    que.push(PP(0.0, s));
    while(!que.empty())
    {
        PP p = que.top();
        que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(int k = head[v] ; k != -1 ; k = edge[k].next)
        {
//            cout< d[v] + edge[k].w)
            {
                d[edge[k].to] = d[v] + edge[k].w;
                que.push(PP(d[edge[k].to], edge[k].to));
            }
        }

    }
}


inline void init()
{
    for(int i = 0 ; i < MAX_N ; i ++)
    {
        d[i] = 1e10;
    }
    memset(head, -1, sizeof(head));
    while(!que.empty()) que.pop();
    top = 0;
}
int main()
{
    int cas = 1;
    while(scanf("%d",&n) && n != -1)
    {
        init();
        for(int i = 1 ; i <= n ; i++)
        {
            P[i].x = read();
            P[i].y = read();
            P[i].z = read();
            P[i].r = read();
        }

        P[0].x = read();
        P[0].y = read();
        P[0].z = read();
        P[0].r = 0;

        P[n + 1].x = read();
        P[n + 1].y = read();
        P[n + 1].z = read();
        P[n + 1].r = 0;
//        cout<


你可能感兴趣的:(图论,UVA,uva,算法,dijkstra)