样例输出-1,但提交上去后AC?!
查看原帖
样例输出-1,但提交上去后AC?!
684354
chengyixuan1308楼主2024/9/7 11:34

rt

#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
const int N = 1e3 + 10;
struct node { int l,r,h,id; } a[N];
int n,s,t,down[N][2],dst[N][2];
bool flag[N][2];
const int inf = 9e18;
bool cmp(node x,node y)
{
	if(x.h != y.h)	return x.h > y.h;
	return x.l < y.l;
}
signed main()
{
	cin >> n >> s >> t;
	for(int i = 1;i <= n;i++)
		cin >> a[i].l >> a[i].r >> a[i].h,a[i].id = i;
	sort(a + 1,a + n + 1,cmp);
	for(int i = 1;i <= n;i++)
		if(a[i].id == s)
		{
			s = i;
			break;
		}
	for(int i = 1;i <= n;i++)
		if(a[i].id == t)
		{
			t = i;
			break;
		}
	memset(down,0x3f,sizeof(down));
	for(int i = 1;i <= n;i++)
		for(int j = i + 1;j <= n;j++)
		{
			if(a[i].l >= a[j].l && a[i].r <= a[j].r)
			{
				flag[i][0] = flag[i][1] = true;
				down[i][0] = min(down[i][0],j);
				down[i][1] = min(down[i][1],j);
			}
			else if(a[i].l <= a[j].r && a[i].l >= a[j].l)
			{
				flag[i][0] = true;
				down[i][0] = min(down[i][0],j);
			}
			else if(a[i].r >= a[j].l && a[i].r <= a[j].r)
			{
				flag[i][1] = true;
				down[i][1] = min(down[i][1],j);
			}
		}
	memset(dst,0x3f,sizeof(dst));
	queue<pii > q;
	q.push(pii(s,0));
	dst[s][0] = 0;
	while(!q.empty())
	{
		pii _u = q.front();
		q.pop();
		int u = _u.x, f = _u.y;
		if(!flag[u][f])	continue;
		// [u,f] => [down[u][f],f] and [u,!f]
		// [u,!f]
		int d;
		d = abs(a[u].r - a[u].l);
		if(dst[u][f] + d < dst[u][1 - f])	q.push(pii(u,1 - f)),dst[u][1 - f] = dst[u][f] + d;
		// [down[u][f],f]
		if(down[u][f] > n)	continue;
		int v = down[u][f];
		if(a[u].l >= a[v].l && a[u].r <= a[v].r)
		{
			d = (f == 0 ? a[u].l - a[v].l : a[v].r - a[u].r) + a[u].h - a[v].h;
			if(dst[u][f] + d < dst[v][f])	q.push(pii(v,f)),dst[v][f] = dst[u][f] + d;
		}
		else if(a[u].l <= a[v].r && a[u].l >= a[v].l && f == 0)
		{
			d = a[u].h - a[v].h + a[u].l - a[v].l;
			if(dst[u][f] + d < dst[v][0])	q.push(pii(v,0)),dst[v][0] = dst[u][f] + d;
			d = a[u].h - a[v].h + a[v].r - a[u].l;
			if(dst[u][f] + d < dst[v][1])	q.push(pii(v,1)),dst[v][1] = dst[u][f] + d;
		}
		else if(a[u].r >= a[v].l && a[u].r <= a[v].r && f == 1)
		{
			d = a[u].h - a[v].h + a[u].r - a[v].l;
			if(dst[u][f] + d < dst[v][0])	q.push(pii(v,0)),dst[v][0] = dst[u][f] + d;
			d = a[u].h - a[v].h + a[v].r - a[u].r;
			if(dst[u][f] + d < dst[v][1])	q.push(pii(v,1)),dst[v][1] = dst[u][f] + d;
		}
	}
	int ans = inf;
	for (int i = 1;i <= n;i++)
	{
		if(down[i][0] == t)
			ans = min(ans,dst[i][0] + a[i].h - a[t].h);
		if(down[i][1] == t)
			ans = min(ans,dst[i][1] + a[i].h - a[t].h);
//		cout << dst[i][0] << " " << dst[i][1] << endl;
//		cout << down[i][0] << " " << down[i][1] << endl;
	}
	if(ans > (int)1e18)	cout << -1 << endl;
	else	cout << ans << endl;
	return 0;
}

有没有大佬帮忙看一下。

2024/9/7 11:34
加载中...