Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

堆优化Dijkstra

Posted by 1340502116 at 2016-07-08 11:18:54 on Problem 2253
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std;
const int MAXN=205;
const int INF=0x3f3f3f3f;
struct Edge{
	int to;
	double w;
	Edge(){}
	Edge(int cto,double cw):to(cto),w(cw){}
	bool operator<(const Edge& e) const
	{
		return w > e.w;
	}
};
struct Point{
	int y,x;
}points[MAXN];
vector<Edge> arc[MAXN];
int n;
double dist(int x1,int y1,int x2,int y2)
{
	return sqrt(double((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
}
double d[MAXN];
int vis[MAXN];
double dijkstra(int s)
{
	for(int i=1;i<=n;i++)
	{
		d[i]=INF;
		vis[i]=0;
	}
	priority_queue<Edge> que;
	que.push(Edge(s,0));
	d[s]=0;
	while(!que.empty())
	{
		Edge now=que.top();que.pop();
		int u=now.to;
		if(vis[u])	continue;
		vis[u]=1;
		for(int i=0;i<arc[u].size();i++)
		{
			Edge e=arc[u][i];
			double mx=max(d[u],e.w);
			if(d[e.to]>mx)
			{
				d[e.to]=mx;
				que.push(Edge(e.to,mx));
			}
		}
	}
	return d[2];
}
int main()
{
	int cas=1;
	while(cin>>n&&n!=0)
	{
		for(int i=1;i<=n;i++)	arc[i].clear();
		for(int i=0;i<n;i++)
		{
			cin>>points[i].y>>points[i].x;
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<i;j++)
			{
				double d=dist(points[i].x,points[i].y,points[j].x,points[j].y);
				arc[i+1].push_back(Edge(j+1,d));
				arc[j+1].push_back(Edge(i+1,d));
			}
		}
		double res=dijkstra(1);
		cout<<"Scenario #"<<cas++<<endl;
		cout<<"Frog Distance = "<<showpoint<<setprecision(3)<<fixed<<res<<endl;
		cout<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator