首页 > ACM题库 > HDU-杭电 > hdu 2482 Transit search-最短路径-[解题报告]C++
2014
01-26

hdu 2482 Transit search-最短路径-[解题报告]C++

Transit search

问题描述 :

Henry decides to develop a web site, which will provide the service of transit search. But he can only get the transit data of Guangzhou, so his web site can only support the transit search of Guangzhou. We suppose Guangzhou is 10240 meters by 10240 meters. The coordinate of the top-left corner is (0,0). The coordinate of the bottom-right corner is (10240,10240). The X�axis is from top to bottom and the Y-axis is from left to right. At the beginning, four pictures of the size 10cm by 10 cm make up of the whole map of Guangzhou. They are numbered from 0 to 3. It is to say at the beginning the scale of the map is 1cm:512 meters. We call the four pictures are at level 1.

When you double-click on the map using the mouse, the map will zoom in. The pictures at next level will be shown on the screen. For example, when you double-click on the above map, picture 0 will be replaced by four pictures 00, 01, 02, 03, all of whose sizes are 10 cm by 10 cm. and the scale of the map change to 1cm:256 meters. (notice that, pictures 00,01,02,03 together describe the same area as picture 0). When you continue double-click, picture 01 will be replaced by pictures 010,011,012,013, and so on.
Now, a position’s coordinate can be given by(str, x,y). str consists of 8 characters each from 0 to 3. It describes the id of the picture which the position is located at. x and y(0cm<=x,y<=10cm) describe the position’s offset relative to the top-left corner on picture str. Notice that the X�axis is from top to bottom and the Y-axis is from left to right.

Now, the start position and end position are given as (start, sx, sy), (end, ex, ey). And some information about the bus line will be also given. First, each bus stop will be described by (name, x, y), its name and its coordinate. Second, each bus line will be described by (name1, name2, name3…namek) which are the bus stops the bus line travels through. If the distance between the start position and end position is no more than 2000 meters, the web site will suggest walking there. Otherwise, the web site will find a bus stop whose distance is no more than 1000 meters from the start position. You can take buses to a bus stop whose distance is no more than 1000 meters from the end position. Along the way, you can change buses at any bus stop. If you can take buses according the above rules, the web site will find a route with fewest number of changing buses. If you can’t take buses according the above rules, the web site will suggest taking a taxi.

输入:

The input begins with a line containing an integer T, the number of test cases.
For each case, the first two lines describe the start position and the end position as followed.
Start sx sy
End ex ey
Start and End both contain 8 characters each from 0 to 3. 0cm<=sx,sy,ex,ey<=10cm. Notice that all the numbers in the input are integers.
The next line contains an integer n(0<n<5001), indicating the total number of bus stops in Guangzhou. The following n lines each describe a bus stop in the format:
Name x y
Name contains no more than 20 characters. 0<=x,y<=10240.
Next comes an integer m(0<m<=100), indicating the number of bus lines in Guangzhou.
Then following is the description of the m bus lines.
Each bus line is described as followed:
K
Name1 Name2 Name3 … Namek
K(0<K<=30) is the number of bus stops along the bus line.
Namei is the ith bus stop along the bus line. Notice that the bus line is bidirectional.

输出:

The input begins with a line containing an integer T, the number of test cases.
For each case, the first two lines describe the start position and the end position as followed.
Start sx sy
End ex ey
Start and End both contain 8 characters each from 0 to 3. 0cm<=sx,sy,ex,ey<=10cm. Notice that all the numbers in the input are integers.
The next line contains an integer n(0<n<5001), indicating the total number of bus stops in Guangzhou. The following n lines each describe a bus stop in the format:
Name x y
Name contains no more than 20 characters. 0<=x,y<=10240.
Next comes an integer m(0<m<=100), indicating the number of bus lines in Guangzhou.
Then following is the description of the m bus lines.
Each bus line is described as followed:
K
Name1 Name2 Name3 … Namek
K(0<K<=30) is the number of bus stops along the bus line.
Namei is the ith bus stop along the bus line. Notice that the bus line is bidirectional.

样例输入:

3
00000000 1 1
00001000 3 3
4
a 1 1
b 20 30
c 40 50
d 100 100
2
3
a b c
3
b c d
00000000 1 1
03231130 5 5
5
a 1 1
b 1000 1000
c 3000 3000
d 3000 4000
e 4500 4000
2
3
a b c
3
c d e
00000000 1 1
03231130 5 5
4
a 1 1
b 1000 1000
c 3000 3000
d 3000 4000
2
3
a b c
3
b c d

样例输出:

walk there
2
take a taxi

http://acm.hdu.edu.cn/showproblem.php?pid=2482

#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#define N 5050
#define NN 105
#define M 10001
#define inf 0x7fffffff
int place[8]={5120,2560,1280,640,320,160,80,40};
int fx,fy;
int ex,ey;
int m,n;

struct bus_stop{
	int x,y;
	char ch[30];
}B[N];
vector<bus_stop> bus_line[NN];

struct node{
	int next,v,w;
	node(){};
	node(int a,int b,int c){
		next=a;v=b;w=c;
	}
}E[M];
int head[NN],dis[NN],NE;
bool h[NN];
void init(){
	NE=0;
	memset(head,-1,sizeof(head));
}
void insert(int u,int v,int w){
	E[NE]=node(head[u],v,w);
	head[u]=NE++;
}
bool update(int u,int v,int w){
	if(dis[u]+w<dis[v]){
		dis[v]=dis[u]+w;
		return true;
	}
	return false;
}
void spfa(int beg,int end){
	queue<int> q;
	memset(h,0,sizeof(h));
	for(int i=0;i<=m+1;i++)
		dis[i]=inf;
	dis[beg]=0;
	q.push(beg);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		h[u]=0;
		for(int i=head[u];i!=-1;i=E[i].next){
			int v=E[i].v;
			if(update(u,v,E[i].w)&&!h[v]){
				h[v]=1;
				q.push(v);
			}
		}
	}
	if(dis[end]==inf){
		printf("take a taxi\n");
		return;
	}
	printf("%d\n",dis[end]);
}

int find(char *p){
	for(int i=0;i<n;i++)
		if(strcmp(p,B[i].ch)==0)
			return i;
}
bool find_bus(vector<bus_stop> p,vector<bus_stop> q){
	for(int i=0;i<p.size();i++)
		for(int j=0;j<q.size();j++)
			if(strcmp(p[i].ch,q[j].ch)==0)
				return true;
	return false;
}
void trans(char *p,int &x,int &y){
	x*=4;y*=4;
	for(int i=0;i<8;i++){
		if(p[i]=='1')
			y+=place[i];
		else if(p[i]=='2')
			x+=place[i];
		else if(p[i]=='3'){
			x+=place[i];
			y+=place[i];
		}
	}
}
double cal_dis(int fx,int fy,int ex,int ey){
	double temp1=(fx*1.0-ex*1.0)*(fx*1.0-ex*1.0);
	double temp2=(fy*1.0-ey*1.0)*(fy*1.0-ey*1.0);
	return sqrt(temp1+temp2);
}
int main(void){
	int t;
	scanf("%d",&t);
	while(t--){
		char s[10],e[10];
		scanf("%s%d%d",s,&fx,&fy);
		scanf("%s%d%d",e,&ex,&ey);
		trans(s,fx,fy);
		trans(e,ex,ey);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%s",B[i].ch);
			scanf("%d%d",&B[i].x,&B[i].y);
		}
		scanf("%d",&m);
		for(int i=1;i<=m;i++){
			char ch[30];
			int k;
			scanf("%d",&k);
			bus_line[i].clear();
			while(k--){
				scanf("%s",ch);
				int id=find(ch);
				bus_line[i].push_back(B[id]);
			}
		}			
		if(cal_dis(fx,fy,ex,ey)<=2000.0){
			printf("walk there\n");
			continue;
		}
		int beg=0;
		int end=m+1;
		bool flag1,flag2;
		flag1=flag2=0;
		init();
		for(int i=1;i<=m;i++)
			for(int j=0;j<bus_line[i].size();j++){
				double k1=cal_dis(fx,fy,bus_line[i][j].x,bus_line[i][j].y);
				double k2=cal_dis(ex,ey,bus_line[i][j].x,bus_line[i][j].y);
				if(k1<=1000.0){
					flag1=true;
					insert(beg,i,1);
				}
				if(k2<=1000.0){
					flag2=true;
					insert(i,end,0);
				}
			}
		if(!flag1||!flag2){
			printf("take a taxi\n");
			continue;
		}
		for(int i=1;i<=m;i++)
			for(int j=i+1;j<=m;j++)
				if(find_bus(bus_line[i],bus_line[j])){
					insert(i,j,1);
					insert(j,i,1);
				}
		spfa(beg,end);
	}
}

解题转自:http://blog.csdn.net/me4546/article/details/6585310


  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。