1. 问题描述

给定一个图结构,包含n个点,e条边,给定搜索半径100000,求解距离源点 s距离为100000的所有点。这里的距离应该还是指的权值。

数据文件"union.txt",格式为:

start_id,end_id,weight,start_x,start_y,end_x,end_y
1,101043,540,567.988263,99.564119,567.988263,100.104119
......

2. 算法策略

对单源最短路径的求解稍加改动即可,实现思路完全一样。搜索过程中,发现某结点权值已经小于等于100000,则输出并记录该结点,因为每个结点可能被松弛多次,需置标记为1避免重复记录。也就是说,如果一个结点最终未被记录,要么不可达,要么在所有松弛过后权值仍大于100000。

结点是按搜索先后顺序记录的。为了验证正确性,将记录的结点ID进行排序并输出,同时直接遍历dist数组,输出权值小于等于100000的ID,比较二者发现结果相等。由此验证结果正确。

3. 代码实现

#include "stdio.h"
#include "windows.h"
#include <vector>
#include <queue>
#include <stack>
#include <ctime>
using namespace std;

#define MAXN 195234 //1 ... 195233
#define MAXW 0x3f3f3f3f //最大权重

typedef struct WNode{
	int id;
	int cur_w;
	bool operator<(const WNode& n)const{
		return cur_w>n.cur_w;
	}
	WNode(int id,int cur_w):id(id),cur_w(cur_w){}
}WNode;

//Graph[i]中存放的是 与i号结点相邻的 结点们的编号j以及对应权重w, Graph[i]={pair<j,w>,...}
vector<pair<int,int> > Graph[MAXN];
bool visit[MAXN];
int path[MAXN];
int dist[MAXN];
int searched[MAXN];
vector<int> ans;

int s; //源点
int searchRadius; //搜索半径
clock_t start,finish;

//读入文件进行初始化
bool Initialize(){
	printf("Input the starting point: ");
	scanf("%d",&s);
	printf("Input the searching radius: ");
	scanf("%d",&searchRadius);

	printf("\nInputing data...\n");
	start=clock();
	FILE* fp=fopen("union.txt","r");
	if(fp==NULL){
		printf("File open error./n");
		return -1;
	}
	int sid,tid,w;
	double sx,sy,tx,ty;
	fscanf(fp,"%*[^\n]%*c");//跳过第一行
	while(true){
		fscanf(fp,"%d,%d,%d,%lf,%lf,%lf,%lf",&sid,&tid,&w,&sx,&sy,&tx,&ty);
		Graph[sid].push_back(make_pair(tid,w));
		if(sid%1000==0){
			printf("\r[%.2f%%]",sid/195233.0*100);
			fflush(stdout);
		}
		if(sid==195233){
			printf("\r[%.2f%%]",sid/195233.0*100);
			finish=clock();
			printf("\nData input successfully.\n");
			printf("Total time is %.2fs.\n",(double)(finish-start)/CLOCKS_PER_SEC);
			break;
		}
	}
	fclose(fp);
	return true;
}

//求解最短路径,将范围内的点输出并保存至文件
void ShortestPath(int s){
	printf("\nPoints in range %d are:\n",searchRadius);
	start=clock();
	FILE* fp=fopen("101.txt","w");

	memset(path,0,sizeof(path));
	memset(visit,0,sizeof(visit));
	memset(dist,MAXW,sizeof(dist));	
	memset(searched,0,sizeof(searched));
	priority_queue<WNode> qu;
	qu.push(WNode(s,0));
	path[s]=0;
	dist[s]=0;

	while(!qu.empty()){
		WNode cur=qu.top();
		qu.pop();
		if(visit[cur.id])continue;
		visit[cur.id]=1;

		for(int i=0;i<Graph[cur.id].size();i++){
			pair<int,int> tmp=Graph[cur.id][i];
			if(!visit[tmp.first]){	//将未访问过的入队
				qu.push(WNode(tmp.first,tmp.second+cur.cur_w));
			}
			if(tmp.second+cur.cur_w < dist[tmp.first]){	//松弛
				dist[tmp.first]=tmp.second+cur.cur_w;
				path[tmp.first]=cur.id;
				//在搜索半径内,且未被搜索过
				if(dist[tmp.first]<searchRadius && searched[tmp.first]==0){
					searched[tmp.first]=1;
					printf("%d ",tmp.first);//输出到屏幕
					fprintf(fp,"%d ",cur.id);//输出到文件
					ans.push_back(tmp.first);//结果记录在ans中
				}
			}
		}
	}
	fclose(fp);
	printf("\n\nPath solved successfully.\n");
	finish=clock();
	printf("Total time is %.2fs.\n\n",(double)(finish-start)/CLOCKS_PER_SEC);
}

int _tmain(int argc, _TCHAR* argv[])
{
	Initialize();
	ShortestPath(s);
	printf("-----To verify the correctness, sort the ID array and print it:-----\n");
	sort(ans.begin(),ans.end());
	for(int i=0;i<ans.size();i++){
		printf("%d ",ans[i]);
	}
	printf("\n-----Compared with the IDs directly from the dist array:-----\n");
	for(int i=1;i<MAXN;i++){
		if(dist[i]<100000 && dist[i]!=0/*去除源点本身*/){
			printf("%d ",i);
		}
	}
	printf("\n");
	return 0;
}

4. 运行结果

设定源点101,搜索半径100000,得到范围内结点ID,并验证正确性。

标号法进行可达性分析

相关文章: