HDU 4179 Difficult Routes
Difficult Routes
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1030 Accepted Submission(s): 207
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 is
100*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.
![](http://acm.hdu.edu.cn/data/images/C388-1002-5.jpg)
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.
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 is
![](http://acm.hdu.edu.cn/data/images/C388-1002-1.jpg)
![](http://acm.hdu.edu.cn/data/images/C388-1002-2.jpg)
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
![](http://acm.hdu.edu.cn/data/images/C388-1002-3.jpg)
![](http://acm.hdu.edu.cn/data/images/C388-1002-4.jpg)
The figure shows a road map with three intersections for the three sample inputs.
![](http://acm.hdu.edu.cn/data/images/C388-1002-5.jpg)
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.
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.
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
PS:智障了3天啦,每天都在重写,今天终于ac了,发现了这题我犯的错误,还是看了别人的的博客才发现的
题意:一个三维坐标系,给出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的边,建立正图和反图,分别求出起点到每个点的最短距离和每个点到终点的最短距离。
一定要有一个难度值为d的。
1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 #include <cstdio> 7 #define N 50010 8 using namespace std; 9 const double inf = 1e16; 10 struct nope{ 11 int x,y,z; 12 }point[N]; 13 struct node{ 14 int next; 15 double len; 16 }; 17 int n,m; 18 double len1[N],len2[N]; 19 bool vis[N]; 20 vector<node> mapp1[N]; 21 vector<node> mapp2[N]; 22 vector<int> edge; 23 double dis(nope a,nope b){ 24 double xx=1.0*(a.x-b.x)*(a.x-b.x); 25 double yy=1.0*(a.y-b.y)*(a.y-b.y); 26 double zz=1.0*(a.z-b.z)*(a.z-b.z); 27 return sqrt(xx+yy+zz); 28 } 29 int dif(nope a,nope b){ 30 if(a.z>=b.z){ 31 return 0; 32 } 33 double xx=1.0*(a.x-b.x)*(a.x-b.x); 34 double yy=1.0*(a.y-b.y)*(a.y-b.y); 35 return (int)(100*(b.z-a.z)/sqrt(xx+yy)); 36 } 37 void add(int a,int b,double len,vector<node> mapp[]){ 38 node aa; 39 aa.next=b; 40 aa.len=len; 41 mapp[a].push_back(aa); 42 } 43 44 void spfa(int start,double *len,vector<node> mapp[]){ 45 queue<int> Q; 46 for(int i=1;i<=n;i++){ 47 len[i]=inf; 48 vis[i]=false; 49 } 50 len[start]=0; 51 vis[start]=true; 52 Q.push(start); 53 while(!Q.empty()){ 54 int u=Q.front(); 55 Q.pop(); 56 vis[u]=false; 57 for(int i=0;i<mapp[u].size();i++){ 58 int v=mapp[u][i].next; 59 double w=mapp[u][i].len; 60 if(len[v]>len[u]+w){ 61 len[v]=len[u]+w; 62 if(!vis[v]){ 63 vis[v]=true; 64 Q.push(v); 65 } 66 } 67 } 68 } 69 } 70 71 int main(){ 72 int a[N],b[N],s,t,d; 73 while(scanf("%d %d",&n,&m),n||m){ 74 for(int i=0;i<=n;i++){ 75 mapp1[i].clear(); 76 mapp2[i].clear(); 77 } 78 edge.clear(); 79 for(int i=1;i<=n;i++){ 80 scanf("%d %d %d",&point[i].x,&point[i].y,&point[i].z); 81 } 82 for(int i=0;i<m;i++){ 83 scanf("%d %d",&a[i],&b[i]); 84 } 85 scanf("%d %d %d",&s,&t,&d); 86 for(int i=0;i<m;i++){ 87 int dif1=dif(point[a[i]],point[b[i]]); 88 int dif2=dif(point[b[i]],point[a[i]]); 89 double dd=dis(point[a[i]],point[b[i]]); 90 if(dif1<=d){ 91 add(a[i],b[i],dd,mapp1); 92 add(b[i],a[i],dd,mapp2); 93 } 94 if(dif2<=d){ 95 add(b[i],a[i],dd,mapp1); 96 add(a[i],b[i],dd,mapp2); 97 } 98 if(dif1==d){ 99 edge.push_back(a[i]); 100 edge.push_back(b[i]); 101 } 102 if(dif2==d){ 103 edge.push_back(b[i]); 104 edge.push_back(a[i]); 105 } 106 } 107 spfa(s,len1,mapp1); 108 spfa(t,len2,mapp2); 109 double ans=inf; 110 for(int i=0;i<edge.size();i+=2){ 111 int u=edge[i]; 112 int v=edge[i+1]; 113 double tmp=len1[u]+dis(point[u],point[v])+len2[v]; 114 if(ans>tmp){ 115 ans=tmp; 116 } 117 } 118 if(ans == inf){ 119 printf("None\n"); 120 } 121 else{ 122 printf("%.1lf\n",ans); 123 } 124 } 125 return 0; 126 }
借鉴别人的代码写的
1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 #include <cstdio> 7 using namespace std; 8 const int N = 50005; 9 const double inf = 1e16; 10 struct node{ 11 int x,y,z; 12 }p[N]; 13 struct Edge{ 14 int v; 15 double w; 16 int next; 17 }et[N*100]; 18 int n,m,num; 19 int s,t,d; 20 double dis1[N],dis2[N]; 21 int eh1[N],eh2[N],aa[N],bb[N]; 22 bool vis[N]; 23 vector<int> edge; 24 double dis(node a,node b){ 25 double xx=1.0*(a.x-b.x)*(a.x-b.x); 26 double yy=1.0*(a.y-b.y)*(a.y-b.y); 27 double zz=1.0*(a.z-b.z)*(a.z-b.z); 28 return sqrt(xx+yy+zz); 29 } 30 int dif(node a,node b){ 31 if(b.z<=a.z){ 32 return 0; 33 } 34 double xx=1.0*(a.x-b.x)*(a.x-b.x); 35 double yy=1.0*(a.y-b.y)*(a.y-b.y); 36 return (int)(100*(b.z-a.z)/ sqrt(xx+yy)); 37 } 38 void add(int u,int v,double w,int *eh){ 39 Edge e = {v,w,eh[u]}; 40 et[num]=e; 41 eh[u]=num++; 42 } 43 44 void spfa(int start,int *eh,double *dis){ 45 queue<int> Q; 46 for(int i=1;i<=n;i++){ 47 dis[i]=inf; 48 vis[i]=false; 49 } 50 dis[start]=0; 51 vis[start]=true; 52 Q.push(start); 53 while(!Q.empty()){ 54 int u=Q.front(); 55 Q.pop(); 56 vis[u]=false; 57 for(int i=eh[u];i!=-1;i=et[i].next){ 58 int v=et[i].v; 59 double w=et[i].w; 60 if(dis[v]>dis[u]+w){ 61 dis[v]=dis[u]+w; 62 if(!vis[v]){ 63 vis[v]=true; 64 Q.push(v); 65 } 66 } 67 } 68 } 69 } 70 71 int main(){ 72 while(scanf("%d %d",&n,&m),n||m){ 73 memset(eh1,-1,sizeof(eh1)); 74 memset(eh2,-1,sizeof(eh2)); 75 num=0; 76 edge.clear(); 77 for(int i=1;i<=n;i++){ 78 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); 79 } 80 for(int i=0;i<m;i++){ 81 scanf("%d%d",&aa[i],&bb[i]); 82 } 83 scanf("%d%d%d",&s,&t,&d); 84 for(int i=0;i<m;i++){ 85 int a=aa[i]; 86 int b=bb[i]; 87 int dif1=dif(p[a],p[b]); 88 int dif2=dif(p[b],p[a]); 89 double dd=dis(p[a],p[b]); 90 if(dif1<=d){ 91 add(a,b,dd,eh1); 92 add(b,a,dd,eh2); 93 } 94 if(dif2<=d){ 95 add(b,a,dd,eh1); 96 add(a,b,dd,eh2); 97 } 98 if(dif1==d){ 99 edge.push_back(a); 100 edge.push_back(b); 101 } 102 if(dif2==d){ 103 edge.push_back(b); 104 edge.push_back(a); 105 } 106 } 107 spfa(s,eh1,dis1); 108 spfa(t,eh2,dis2); 109 double ans=inf; 110 for(int i=0;i<edge.size();i+=2){ 111 int u=edge[i]; 112 int v=edge[i+1]; 113 double tmp=dis1[u]+dis(p[u],p[v])+dis2[v]; 114 if(ans>tmp){ 115 ans=tmp; 116 } 117 } 118 if(ans == inf){ 119 printf("None\n"); 120 } 121 else{ 122 printf("%.1lf\n",ans); 123 } 124 } 125 return 0; 126 }
2016-12-22 17:48:26