最莽的做法,求助为啥时间这么长
查看原帖
最莽的做法,求助为啥时间这么长
237893
donkeys楼主2020/9/24 20:32

先上图,给你们看看我是多么的精准

卡得真准,对吧

但是我也是用dij三遍写的(不是抄的题解,自己瞎写的,莫非常数太大) 有大佬能帮忙看看为啥复杂度这么大吗

#define ll long long
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1003, NODE = 1000006;
ll dis[3][NODE];
int n, m, a, b, c;
int nxt[NODE << 2], to[NODE << 2], edge[NODE << 2], head[NODE], tot;
int map[N][N];
void add(int f, int t, int c)
{
	to[++tot] = t, edge[tot] = c, nxt[tot] = head[f], head[f] = tot;
	return;
}
void dij(int rt, int num)
{
	bool v[NODE] = { 0 };
	memset(dis[num], 0x10, sizeof(dis[num]));
	priority_queue<pair<ll, int> >q;
	q.push({ 0,rt });
	while (!q.empty())
	{
		int p = q.top().second;
		ll cost = -q.top().first;
		q.pop();
		if (v[p])
			continue;
		v[p] = 1, dis[num][p] = cost;
		for (int i = head[p]; i; i = nxt[i])
		{
			if (v[to[i]])
				continue;
			else if (cost + edge[i] >= dis[num][to[i]])
				continue;
			q.push({ -(cost + edge[i]),to[i] });
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m >> a >> b >> c;
	for (int i = 1, base = 0; i <= n; i++, base += m)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> map[i][j];
			if (i != 1)
				add(base + j, base + j - m, map[i - 1][j]), add(base + j - m, base + j, map[i][j]);
			if (j != 1)
				add(base + j, base + j - 1, map[i][j - 1]), add(base + j - 1, base + j, map[i][j]);
		}
	}
	int ta = map[1][a], tb = map[n][b], tc = map[n][c];
	b += n * m - m, c += n * m - m;
	dij(a, 0), dij(b, 1), dij(c, 2);
	int sumpt = n * m;
	ll min = 0x7f7f7f7f7f7f7f7f;
	/*for (int i = 1; i <= sumpt; i++)
	{
		ll temp = dis[0][i] + dis[1][i] + dis[2][i] - (ll)map[(i - 1) / m + 1][(i - 1) % m + 1] * 2;
		if (temp < min)
			min = temp;
	}*/
	for (int i = 1, base = 0; i <= n; i++, base += m)
	{
		for (int j = 1; j <= m; j++)
		{
			ll temp = dis[0][base + j] + dis[1][base + j] + dis[2][base + j] - (ll)map[i][j] * 2;
			if (temp < min)
				min = temp;
		}
	}
	cout << min + ta + tb + tc;
	return 0;
}
2020/9/24 20:32
加载中...