求助,求错误数据,能把这个代码wa掉就可
  • 板块P1491 集合位置
  • 楼主纯白
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/10/17 22:38
  • 上次更新2023/11/4 03:25:57
查看原帖
求助,求错误数据,能把这个代码wa掉就可
327139
纯白楼主2021/10/17 22:38

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#define mp make_pair
using namespace std;
const double inf =0x3f3f3f3f;
template <typename T>
inline void read(T &x)
{
	x = 0;
	T f=1;
	char c=getchar();
	for (; !isdigit(c); c=getchar())
		if (c=='-')
			f= -1;
	for (; isdigit(c); c=getchar())
		x = (x<<3) + (x<<1) + c -'0';
	x *=f ;
}
const int maxn = 600;

int n, m;
int a, b;
pair <int, int> p[maxn];
struct node{
	int from, to, nex;
	double val;
}e[maxn*maxn];
int hd[maxn],ecnt;
inline void adde(int from, int to, double val)
{
	e[++ecnt] = node{from, to, hd[from], val};
	hd[from] = ecnt;
	e[++ecnt] = node{to, from, hd[to], val};
	hd[to] = ecnt;
}


inline double cal(int ax, int ay, int bx, int by)
{
	return sqrt(pow((ax-bx),2)+pow((ay-by),2));
}

double d1[maxn],d2[maxn];
priority_queue < pair<double, int> > q;
bool vis[maxn];

inline void dijkstra(int x)
{
	q.push(mp(0,x));
	d1[x]=0;
	while (!q.empty())
	{
		int u =q.top().second; q.pop();
		//if (vis[u]) continue;
		//vis[u] = 1;
		for (int i=hd[u]; i; i=e[i].nex)
		{
			int to = e[i].to;
			if (d1[to] > d1[u] + e[i].val)
			{
				d2[to] = min(d1[to], d2[u] + e[i].val);
				d1[to] = d1[u] + e[i].val;
				q.push(mp(-d1[to], to));
			}
			else if (d1[to] == d1[u] + e[i].val)
			{
				if (d2[to] > d2[u] + e[i].val)
				{
					d2[to] = d2[u] + e[i].val;
					q.push(mp(-d1[to], to));
				}
			}
			else if (d2[to] > d1[u] + e[i].val )
			{
				d2[to] = d1[u] + e[i].val;
				q.push(mp(-d1[to], to));
			}
			
		}
	}
}

double dis[maxn];
int cnt[maxn];
inline bool dij(int x)
{
	while (!q.empty()) q.pop();
	fill(dis, dis+n+1, inf);
	dis[x] = 0;
	q.push(mp(0, x));
	cnt[x] = 1;
	while (!q.empty())
	{
		int u=q.top().second;q.pop();
		if (vis[u]) continue;
		vis[u] = 1;
		for (int i=hd[u]; i; i=e[i].nex)
		{
			int to=e[i].to;
			if (dis[to] > dis[u] + e[i].val)
			{
				cnt[to] = cnt[u];
				dis[to] = dis[u] + e[i].val;
				q.push(mp(-dis[to], to));
			}
			else if (dis[to] == dis[u] + e[i].val)
			{
				cnt[to] += cnt[u];
			}
		}
	}
	return cnt[n] > 1;
}



int main ()
{
	read(n),read(m);
	for (int i=1; i<=n; i++)
		read(p[i].first), read(p[i].second);
	for (int i=1; i<=m; i++)
	{
		read(a),read(b);
		adde(a, b, cal(p[a].first, p[a].second, p[b].first, p[b].second));
	}
	fill(d1+1, d1+1+n, inf);
	fill(d2+1, d2+1+n, inf);
	dijkstra(1);
	if (d2[n] == inf)
		printf("-1");
	else if (dij(1))
		printf("%.2lf",d1[n]);
	else 
		printf("%.2lf",d2[n]);
	return 0;
	
}

大意是先求的严格的次短路,然后如果最短路有两条的话就输出最短,否则就是次短

2021/10/17 22:38
加载中...