The maze breakout (dijkstra seeks the second shortest path)

Find the next-shortest path in a graph

input format

The first line inputs two integers  n  ( 1 n 2 0 0 ) and  m , indicating that there are a total of  n  points and  m  edges.

Next, enter n  lines, each with two integers xi, andi (500xi, andi5 0 0 ), representing the coordinates of the i  -th point.

Next, enter  m  lines, each with two integers pj,qj (1pj,qjn ), representing the point  pj and point  qj connected between.

output format

Output a line, the output contains a number, indicating the length of the second shortest path (two digits after the decimal point), if there are multiple first short paths, the answer is the length of the first shortest path; if the second shortest path does not exist, Then output  1 .

sample input

3 3
1 1
2 2
3 2
1 2
2 3
1 3

Sample output

2.41

Problem solving instructions: It is not allowed to repeat the second shortest path of the node. First, the dijksta algorithm calculates the shortest path, and records the shortest path of 1—>n.

The crux of the problem is how to record the shortest path of 1->n nodes? We use a fa[maxn] array to continuously save a node by which node is updated. For example, the shortest path is 1->2->3->4, then our dijkstra algorithm is that every update, it will save the shortest path from 1 node to the current search node, how does 1 come to this node, it is directly 1 Come, or it was updated through other points. Sending fa[] is to record who updated this point in the previous step. Here we send fa[3]=2,fa[2]=1;fa[4]=3;

We enumerate and eliminate the edges on the shortest distance, and we can start from the edge (fa[n]-n).

The last memset assignment to double (a larger value such as 0x3f3f3f3f) is not acceptable.

ac code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<string.h>
#include<map>
using namespace std;
const int MAXN=205;
double inf=99999999;
struct po{
	int x, y;
}p[MAXN];
int fa[MAXN];
struct node{
	int no;double cow;
	node(int noo,double coww){
		no=noo;cow=coww;
	}
};
double dis[MAXN];
int n;
bool mark[MAXN];
vector<node>ve[MAXN];
void dij(int left,int right){
	int now=1;dis[1]=0;
	memset(mark,false,sizeof(mark));
	mark[1]=true;
	for(int i=2;i<=n;i++) dis[i]=inf;
	for(int i=1;i<n;i++){
		for(int j=0;j<ve[now].size();++j){
			if(mark[ve[now][j].no]||(now==left&&ve[now][j].no==right)||(now==right&&ve[now][j].no==left))
			      continue;
		   if(dis[ve[now][j].no]>dis[now]+ve[now][j].cow){
		        if(!right&&!left)fa[ve[now][j].no]=now;
				dis[ve[now][j].no]=dis[now]+ve[now][j].cow;
     	   }    
		}
		int mn=inf;
		for(int i=1;i<=n;i++){
			if(mark[i])continue;
			if(dis[i]<mn){
				mn = dis [i];
				now=i;
			}
		}
		mark[now]=true;
	}
}
int main(){
	int m;cin>>n>>m;
	memset(mark,false,sizeof(mark));
	for(int i=1;i<=n;i++){
		int ip1,ip2;cin>>ip1>>ip2;
		p[i].x=ip1;p[i].y=ip2;
	}
	for(int i=1;i<=m;i++){
		int ip1,ip2;cin>>ip1>>ip2;
		double lo=sqrt((p[ip1].x-p[ip2].x)*(p[ip1].x-p[ip2].x)+(p[ip1].y-p[ip2].y)*(p[ip1].y-p[ip2].y));
		ve[ip1].push_back(node(ip2,lo));
		ve[ip2].push_back(node(ip1,lo));
	}
	dij (0,0);
	if(dis[n]==inf){
		cout<<"-1"<<endl;
		return 0;
	}
	double mn=inf;
	int now=n;fa[1]=0;
    while(fa[now]){  
        dij(fa[now],now);  
        mn=min(dis[n],mn);  
        now=fa[now];  
    }  
	if(mn==inf)cout<<"-1"<<endl;
	else printf("%.2lf",mn);
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324684377&siteId=291194637