最短路 WA on test 30 求助!
  • 板块CF95C Volleyball
  • 楼主Moyou
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/11/20 17:12
  • 上次更新2023/10/27 02:11:58
查看原帖
最短路 WA on test 30 求助!
597798
Moyou楼主2022/11/20 17:12

求助!Wrong answer on test 30

思路:跑一遍堆优化dij求出每个点与别的点的最短距离,给每个最短距离 <= t的点连一条长度为c的边,最后再在新的图上跑一遍dij求出起点到终点的最短路

// Problem: Volleyball
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF95C
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Author: Moyou
// Copyright (c) 2022 Moyou All rights reserved.
// Date: 2022-11-20 15:48:32

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#define x first
#define y second
#define int long long
#define speedup (ios::sync_with_stdio(0),cin.tie(0),cout.tie(0))
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int N = 2e3 + 10, M = 3e3 + 10, M2 = 3e6 + 10;

int h[N], ne[M], e[M], w[M], idx;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; 
}

int h2[N], ne2[M2], e2[M2], w2[M2], idx2;
void add2(int a, int b, int c)
{
	e2[idx2] = b, w2[idx2] = c, ne2[idx2] = h2[a], h2[a] = idx2 ++; 
}

int dist[N];

int t[N], c[N];
bool st[N];

void dijkstra(int s)
{
	memset(st, 0, sizeof st);
	memset(dist, 0x7f, sizeof dist);
	priority_queue<PII> heap;
	heap.push({0, s});
	dist[s] = 0;
	while(heap.size())
	{
		auto t = heap.top();
		heap.pop();
		if(st[t.y]) continue;
		st[t.y] = 1;
		for(int i = h[t.y]; ~i; i = ne[i])
		{
			int j = e[i];
			if(dist[j] > dist[t.y] + w[i])
			{
				dist[j] = dist[t.y] + w[i];
				heap.push({dist[j], j});
			}
		}
	}
}

void dijkstra2(int s)
{
	memset(st, 0, sizeof st);
	memset(dist, 0x7f, sizeof dist);
	priority_queue<PII> heap;
	heap.push({0, s});
	dist[s] = 0;
	while(heap.size())
	{
		auto t = heap.top();
		heap.pop();
		if(st[t.y]) continue;
		st[t.y] = 1;
		for(int i = h2[t.y]; ~i; i = ne2[i])
		{
			int j = e2[i];
			if(dist[j] > dist[t.y] + w2[i])
			{
				dist[j] = dist[t.y] + w2[i];
				heap.push({dist[j], j});
			}
		}
	}
}

signed main()
{
	memset(h, -1, sizeof h);
	memset(h2, -1, sizeof h2);

	int n, m, start, end;
	cin >> n >> m >> start >> end;
    
	for(int i = 1; i <= m; i ++)
	{
		int a, b, ww;
		cin >> a >> b >> ww;
		add(a, b, ww); add(b, a, ww);
	}
    
	
	for(int i = 1; i <= n; i ++)
		cin >> t[i] >> c[i];
		
	for(int i = 1; i <= n; i ++)
	{
		dijkstra(i);
		for(int j = 1; j <= n; j ++)
			if(dist[j] <= t[i])
				add2(i, j, c[i]);
	}
	dijkstra2(start);
	if(dist[end] >= 0x7f7f7f7f) cout << -1 << endl;
	else cout << dist[end] << endl;
	
	return 0;
}

2022/11/20 17:12
加载中...