求助!2,3,4MLE
查看原帖
求助!2,3,4MLE
772592
shuangmu楼主2023/5/14 09:36

是因为溢出吗?

#include<bits/stdc++.h>
using namespace std;
const int N = 205, M = 3050, P = 15, F = 9000;
const int INF = 0x3f3f3f3f;

inline int read()
{
	int x = 0; char ch = getchar();
	while(ch<'0'||ch>'9'){ ch = getchar();}
	while(ch>='0'&&ch<='9'){x = x*10+ch-48;ch = getchar();}
	return x;
}
int head[N*F], tot;
struct node
{
	int nxt, to, w;
}edge[60*N*F+M*2*F];
void add(int u, int v, int w)
{
	edge[++tot].nxt = head[u];
	edge[tot].to = v;
	edge[tot].w = w;
	head[u] = tot;
}
int n, m, p, K;//点, 边, 怪物数, 铁匠
int g[N]; 
struct xwx
{
	int p, w;
	bool operator <(const xwx &b)const
	{
		return w>b.w;
	}
};
priority_queue<xwx> q;
int dst[N*F];
bool vis[N*F];
void dij(int s)
{
	memset(dst, 0x3f, sizeof(dst));
	dst[s] = 0;
	q.push((xwx){s, dst[s]});
	while(!q.empty())
	{
		int u = q.top().p;
		q.pop();
		vis[u] = 1;
		for(int i = head[u]; i; i = edge[i].nxt)
		{
			int v = edge[i].to;
			if(!vis[v]&&dst[u]+edge[i].w<dst[v])
			{
				dst[v] = dst[u]+edge[i].w;
				q.push((xwx){v, dst[v]});
			}
		}
	}
}
int main()
{
	n = read(), m = read(), p = read(), K = read();
	for(int i = 1; i<=K; i++)
	{
		int u = read(), qi = read();
		for(int i = 1; i<=qi; i++)
		{
			int tmp = read();
			g[u]|=(1<<(tmp-1));
		}
	}
	for(int i = 1; i<=m; i++)
	{
		int u = read(), v = read(), w = read();
		int kin = read(), tmp = 0;
		for(int j = 1; j<=kin; j++)
		{
			int x = read();
			tmp|=(1<<(x-1));
		}
		for(int j = (1<<p)-1; j>=tmp; j--)
		{
			if((j&tmp)==tmp)
				add(u+j*n, v+j*n, w);
				add(v+j*n, u+j*n, w);
		}
	}
	for(int i = 0; i<(1<<p)-1; i++)
	{
		for(int u = 1; u<=n; u++)
		{
			if((g[u]&i)!=g[u])
			{
				int flo = (g[u]|i);
				add(u+i*n, u+flo*n, 0);
			}
		}
	}
	dij(1+g[1]*n);
	int ans = INF;
	for(int i = 0; i<=(1<<p)-1; i++)
	{
		ans = min(ans, dst[n+i*n]);
	}
	if(ans == INF)
		puts("-1");
	else
		printf("%d\n", ans);
	return 0;
}
2023/5/14 09:36
加载中...