hdu 4179(限制最短路)

来源:互联网 发布:广告宣传单设计软件 编辑:程序博客网 时间:2024/06/03 16:41

Difficult Routes

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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. 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 30 0 0100 100 6200 0 71 22 33 11 2 33 30 0 0100 100 6200 0 71 22 33 11 1 43 30 0 0100 100 6200 0 71 22 33 12 1 50 0
 

Sample Output
341.5283.1None
题意:一个三维坐标系,给出n个点的坐标,再给出m条无向边,每条边(u,v)都有一个难度值,若v的z坐标小于或等于u的z坐标,则难度值为0,否则,难度值为floor(100 *两点z坐标的差/两点在二维坐标系上的距离)。给出s,t,d,问是否存在一条s到t的路径,路径上边的难度值最大为d,若存在,输出长度最短的路径。
先讲讲我的思路:dis[i][0]表示从起点到i点时,未经过难度值为d的路,dis[i][1]表示从起点到i点时,经过了难度值为d的路。接下来就是用spfa算法,对其进行松弛操作,除此之外,我们需要多记录一个信息--表示到达该点时的状态是否经过了难度值为d的路。不知道是状态表示得不对还是什么原因,一直WA...
PS:找到原因了,由于有一行代码打错变量了,写代码还是不够细心啊。。。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>using namespace std;const int maxn = 10005;const int inf = 0x3f3f3f3f;struct Edge{int to,next,diff;}edge[300005];struct Point{int x,y,z;}p[maxn];struct Node{int u,sta;Node(int _u,int _sta){u = _u;sta = _sta;}};int n,m,cnt,pre[maxn];double dis[maxn][2];bool inq[maxn][2];void addedge(int u,int v,int diff){edge[cnt].to = v;edge[cnt].diff = diff;edge[cnt].next = pre[u];pre[u] = cnt++;}int compute_diff(Point a, Point 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));  }double dist(Point a,Point 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);  }double spfa(int src,int des,int limit){memset(inq,false,sizeof(inq));for(int i = 1; i <= n; i++) dis[i][0] = dis[i][1] = inf;queue<Node> q;for(int i = pre[src]; i != -1; i = edge[i].next){int v = edge[i].to;if(edge[i].diff == limit){dis[v][1] = dist(p[src],p[v]);inq[v][1] = true;q.push(Node(v,1));}else if(edge[i].diff < limit) {dis[v][0] = dist(p[src],p[v]);inq[v][0] = true;q.push(Node(v,0));}}while(!q.empty()){Node cur = q.front();q.pop();int u = cur.u,sta = cur.sta;inq[u][sta] = false;for(int i = pre[u]; i != -1; i = edge[i].next){int v = edge[i].to;if(edge[i].diff == limit){if(dis[v][1] > dis[u][0] + dist(p[u],p[v])){dis[v][1] = dis[u][0] + dist(p[u],p[v]);if(inq[v][1] == false){inq[v][1] = true;q.push(Node(v,1));}}if(dis[v][1] > dis[u][1] + dist(p[u],p[v])){dis[v][1] = dis[u][1] + dist(p[u],p[v]);if(inq[v][1] == false){inq[v][1] = true;q.push(Node(v,1));}}}else if(edge[i].diff < limit) {if(sta == 1){if(dis[v][1] > dis[u][1] + dist(p[u],p[v])){dis[v][1] = dis[u][1] + dist(p[u],p[v]);if(inq[v][1] == false){inq[v][1] = true;q.push(Node(v,1));}}}if(dis[v][0] > dis[u][0] + dist(p[u],p[v])){dis[v][0] = dis[u][0] + dist(p[u],p[v]);if(inq[v][0] == false){inq[v][0] = true;q.push(Node(v,0));}}}}}if(dis[des][1] == inf) return -1;return dis[des][1];}int main(){int u,v,s,t,d,diff1,diff2;while(scanf("%d%d",&n,&m),n+m){for(int i = 1; i <= n; i++)scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);memset(pre,-1,sizeof(pre));cnt = 0;for(int i = 1; i <= m; i++){scanf("%d%d",&u,&v);diff1 = compute_diff(p[u],p[v]);diff2 = compute_diff(p[v],p[u]);addedge(u,v,diff1);addedge(v,u,diff2);}scanf("%d%d%d",&s,&t,&d);double ans = spfa(s,t,d);if(ans < 0) printf("None\n");else printf("%.1f\n",ans);}return 0;}


参考了别人的思路:感觉图论的问题确实有很多巧妙的方法,这里采用是一种类似于“拆边”的方式,枚举每条难度值为d的边那么每条符合要求的s到t的路径长度为s到u的最短距离+w(u,v)+t到v的最短距离。所以对于每条难度值小于或等于d的边,建立正图和反图,分别求出起点到每个点的最短距离和每个点到终点的最短距离。
http://www.acmerblog.com/hdu-4179-difficult-routes-7189.html

0 0
原创粉丝点击