欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU - 4179 Difficult Routes(带有限制的最短路)

程序员文章站 2022-06-03 18:29:32
...

Difficult Routes

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

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 isHDU - 4179 Difficult Routes(带有限制的最短路)100*rise / runHDU - 4179 Difficult Routes(带有限制的最短路) . 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 HDU - 4179 Difficult Routes(带有限制的最短路)XHDU - 4179 Difficult Routes(带有限制的最短路) means X truncated to an integer.
The figure shows a road map with three intersections for the three sample inputs.

HDU - 4179 Difficult Routes(带有限制的最短路)

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. N <= 10000, M <= 30000. 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

Source

The 2011 South Pacific Programming Contest

 

找出每一条困难度小于等于d的边,分别建立正向图和反向图,然后枚举困难度等于d的边(u,v),求d1[u] + d2[v] + w(u,v)的最小值

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 30005;
const double INF = 1e16;
int n,m;
struct Edge
{
	int from,to;
	double dist;
	Edge(int u,int v,double d):from(u),to(v),dist(d){}
};
struct SPFA
{
	int n,m;
	vector<Edge> edges;
	vector<int> G[MAXN];
	bool inq[MAXN];
	double d[MAXN];
	void init(int n)
	{
		this -> n = n;
		for(int i = 0; i <= n; i++) {
			G[i].clear();
		}
		edges.clear();
	}
	void AddEdge(int from,int to,double dist) {
		edges.push_back(Edge(from,to,dist));
		m = edges.size();
		G[from].push_back(m - 1);
	}
	bool spfa(int s)
	{
		queue<int> Q;
		for(int i = 0; i <= n; i++) {
			d[i] = INF;
			inq[i] = 0;
		}
		d[s] = 0;
		Q.push(s);
		inq[s] = true;
		while(!Q.empty()) {
			int u = Q.front();
			Q.pop();
			inq[u] = false;
			for(int i = 0; i < G[u].size(); i++) {
				Edge& e = edges[G[u][i]];
				if(d[e.to] > d[u] + e.dist) {
					d[e.to] = d[u] + e.dist;
					if(!inq[e.to]) {
						Q.push(e.to);
						inq[e.to] = true;
					}
				}
			}
		}
		return true;
	}
}sp1,sp2;
struct node
{
    double x,y,z;
}no[MAXN];
struct edge
{
    int u,v,d;
    double w;
}e[MAXN * 5],e_[MAXN * 5];

double get_dis(node a, node b)
{
    double x = (a.x - b.x) * (a.x - b.x);
    double y = (a.y - b.y) * (a.y - b.y);
    double z = (a.z - b.z) * (a.z - b.z);
    return sqrt(x + y + z);
}
int get_d(node a, node b)
{
    if (a.z >= b.z)
    {
        return 0;
    }
    double x = (a.x - b.x) * (a.x - b.x);
    double y = (a.y - b.y) * (a.y - b.y);
    double z = b.z - a.z;
    return int(z * 100 / sqrt(x + y));
}
int main(void)
{
    int s,t,d;
    double ans;
    while( scanf("%d %d",&n,&m) != EOF && (n + m) ) {
        sp1.init(n);
        sp2.init(n);
        for(int i = 1; i <= n; i++) {
            scanf("%lf %lf %lf",&no[i].x,&no[i].y,&no[i].z);
        }
        for(int i = 1; i <= m; i++) {
            scanf("%d %d",&e[i].u,&e[i].v);
        }
        scanf("%d %d %d",&s,&t,&d);
        int cnt = 0;
        for(int i = 1; i <= m; i++) {
            int u = e[i].u;
            int v = e[i].v;
            int d1 = get_d(no[u],no[v]);
            int d2 = get_d(no[v],no[u]);
            double w = get_dis(no[u],no[v]);
            if(d1 <= d) {
                sp1.AddEdge(u,v,w);
                sp2.AddEdge(v,u,w);
            }
            if(d2 <= d) {
                sp1.AddEdge(v,u,w);
                sp2.AddEdge(u,v,w);
            }
            if(d1 == d) {
                e_[cnt].u = u;
                e_[cnt].v = v;
                e_[cnt++].w = w;
            }
            if(d2 == d) {
                e_[cnt].u = v;
                e_[cnt].v = u;
                e_[cnt++].w = w;
            }
        }
        sp1.spfa(s);
        sp2.spfa(t);
        ans = INF;
        for(int i = 0; i < cnt; i++) {
            ans = min(ans,sp1.d[e_[i].u] + sp2.d[e_[i].v] + e_[i].w);
        }
        if(ans == INF) printf("None\n");
        else printf("%.1lf\n",ans);
    }
    return 0;
}

 

相关标签: 最短路