首页 > ACM题库 > HDU-杭电 > hdu 4179-difficult routes-最短路径-[解题报告]hoj
2015
05-23

hdu 4179-difficult routes-最短路径-[解题报告]hoj

Difficult Routes

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 748    Accepted Submission(s): 125

Problem Description
In preparation for the coming Olympics, you have been asked to propose bicycle training routes for your country’s team. The training committee wants to identify routes for traveling between pairs of locations in multiple sites around
the country. Each route must have a desired level of difficulty based on the steepness of its hills.
You will be given a road map with the elevation data superimposed upon it. Each intersection, where two or more roads meet, is identified by its x- , y- , and z-coordinates. Each road starts and ends at an intersection, is straight, and does not contain bridges
over or tunnels under other roads. The difficulty level, d, of cycling a road is 0 if the road is level or travelled in the downhill direction. The difficulty of a non-level road when travelled in the uphill direction is100*rise
/ run . Here rise is the absolute value of change in elevation and run is the distance between its two intersection points in its horizontal projection to the 2D-plane at elevation zero. Note
that the level of difficulty for cycling a descending road is zero.
A route, which is a sequence of roads such that a successor road continues from the same intersection where its predecessor road finishes, has a level of difficulty d if the maximum level of difficulty for cycling among all its roads equals d. The committee
is also interested in the chosen route between two selected locations, if such a route with the desired difficulty level exists, being the one with the shortest possible distance to travel.
Reminder: The floor function X means X truncated to an integer.
The figure shows a road map with three intersections for the three sample inputs.


The edge labels of the darker shaded surface give the level of difficulty of going up hill. The lighter shaded surface is the horizontal projection to the 2D-plane at elevation zero.
 

Input
Input consists of many road maps. Each map description begins with two non-negative integers N and M, separated by a space on a line by themselves, that represent the number of intersections and the number of roads, respectively.
0 < N, M <= 10000. A value of both N and M equal to zero denotes the end of input data.
Each of the next N lines contains three integers, separated by single spaces, which represent the x-, y- and z-coordinates of an intersection. The integers have values between 0 and 10000, inclusive. Intersections are numbered in order of their appearance starting
with the value one. Each of the following M lines contains two integers that represent start and end intersections of a road.
Finally, three integers s, t and d that represents the desired starting intersection number s, the finishing intersection number t and the level of difficulty d for a training route are given on line by themselves. A valid training route must have at least
one road with a difficulty level of d, and no road with a difficulty level greater than d. 0 <= d <= 10. If the training route is meant to form a closed circuit, then s and t are the same intersection numbers.
 

Output
For each road map and desired route, the output consists of a single line that contains:
1. number denoting the shortest length of a training route rounded to one decimal places, or
2. the single word “None” if no feasible route exists.
 

Sample Input
3 3 0 0 0 100 100 6 200 0 7 1 2 2 3 3 1 1 2 3 3 3 0 0 0 100 100 6 200 0 7 1 2 2 3 3 1 1 1 4 3 3 0 0 0 100 100 6 200 0 7 1 2 2 3 3 1 2 1 5 0 0
 

Sample Output
341.5 283.1 None
题意:一个三维坐标系,给出n个点的坐标,再给出m条无向边,每条边(u,v)都有一个难度值,若v的z坐标小于或等于u的z坐标,则难度值为0,否则,难度值为floor(100 *两点z坐标的差/两点在二维坐标系上的距离)。给出s,t,d,问是否存在一条s到t的路径,路径上边的难度值最大为d,若存在,输出长度最短的路径。
思路:枚举每条难度值为d的边(u,v),那么每条符合要求的s到t的路径长度为s到u的最短距离+w(u,v)+t到v的最短距离。所以对于每条难度值小于或等于d的边,建立正图和反图,分别求出起点到每个点的最短距离和每个点到终点的最短距离。
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const double INF = 1e16;
const int maxn = 50005;

struct Edge
{
    int v;
    double w;
    int next;
} et[maxn * 100];
struct node
{
    int x, y, z;
} p[maxn];
int n, m, d, num, s, t;
int eh1[maxn], eh2[maxn], aa[maxn], bb[maxn];
double dis1[maxn], dis2[maxn];
bool vis[maxn];
vector<int>edg;
double dis(node a, node b)
{
    double xx = 1.0 * (a.x - b.x) * (a.x - b.x);
    double yy = 1.0 * (a.y - b.y) * (a.y - b.y);
    double zz = 1.0 * (a.z - b.z) * (a.z - b.z);
    return sqrt(xx + yy + zz);
}
int dif(node a, node b)
{
    if(b.z <= a.z) return 0;
    double xx = 1.0 * (a.x - b.x) * (a.x - b.x);
    double yy = 1.0 * (a.y - b.y) * (a.y - b.y);
    return (int)(100 * (b.z - a.z) / sqrt(xx + yy));
}
void init()
{
    memset(eh1, -1, sizeof(eh1));
    memset(eh2, -1, sizeof(eh2));
    edg.clear();
    num = 0;
}
void add(int u, int v, double w, int *eh)
{
    Edge e = {v, w, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void spfa(int x, int *eh, double *dis)
{
    queue<int> Q;
    for(int i = 1; i <= n; i++)
    {
        dis[i] = INF;
        vis[i] = false;
    }
    dis[x] = 0;
    vis[x] = true;
    Q.push(x);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = eh[u]; i != -1; i = et[i].next)
        {
            int v = et[i].v;
            double w = et[i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!vis[v])
                {
                    vis[v] = true;
                    Q.push(v);
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d%d", &n, &m), n || m)
    {
        init();
        for(int i = 1; i <= n; i++)
            scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].z);
        for(int i = 0; i < m; i++)
        scanf("%d%d", &aa[i], &bb[i]);
        scanf("%d%d%d", &s, &t, &d);
        for(int i = 0; i < m; i++)
        {
            int a = aa[i], b = bb[i];
            int df1 = dif(p[a], p[b]);
            int df2 = dif(p[b], p[a]);
            double dd = dis(p[a], p[b]);
            if(df1 <= d)
            {
                add(a, b, dd, eh1);
                add(b, a, dd, eh2);
            }
            if(df2 <= d)
            {
                add(b, a, dd, eh1);
                add(a, b, dd, eh2);
            }
            if(df1 == d)
            {
                edg.push_back(a);
                edg.push_back(b);
            }
            if(df2 == d)
            {
                edg.push_back(b);
                edg.push_back(a);
            }
        }
        spfa(s, eh1, dis1);
        spfa(t, eh2, dis2);
        double ans = INF;
        for(int i = 0; i < (int)edg.size(); i += 2)
        {
            int u = edg[i], v = edg[i + 1];
            double tmp = dis1[u] + dis(p[u], p[v]) + dis2[v];
            if(ans > tmp) ans = tmp;
        }
        if(ans == INF) printf("None\n");
        else printf("%.1lf\n", ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。