50分求助,TLE#6#10~14#17~20
查看原帖
50分求助,TLE#6#10~14#17~20
463602
l1247396180楼主2022/11/12 17:49

RT

#include<bits/stdc++.h>
#define MAXN 200010
#define MAXM 400010
using namespace std;
int n,m,q,k,s,t,lastans,v,p;//t为组数,n为顶点个数,m为边的个数,q为天数(即询问次数),k为系数,s为最高水位,v为起点,p为水位

//快读
inline int read()
{
	int ans=0;
	char c=getchar();
	while(!isdigit(c))
		c=getchar();
	while(isdigit(c))
		ans=(ans<<1)+(ans<<3)+(c^'0'),c=getchar();
	return ans;
}
inline void write(int x)
{
	if(x>9)
		write(x/10);
	putchar(x%10+'0');
}
//dijkstra
struct Edge
{
	int to,next,dis,height;
}edge[MAXM<<1];
struct Node
{
	int dis,id;
	bool operator < (const Node &x) const
	{
		return x.dis<dis;
	}
};
int head[MAXN],dis[MAXN<<1],cnt;
bool vis[MAXN];

inline void add_edge(int u,int v,int w,int h)
{
	edge[++cnt].dis=w;
	edge[cnt].height=h;
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt;
}
void dijkstra()
{
	priority_queue <Node> q;
	Node tmp;
	tmp.id=1;
	tmp.dis=0;
	dis[1]=0;
	q.push(tmp);
	while(!q.empty())
	{
		tmp=q.top();
		q.pop();
		if(vis[tmp.id])
			continue;
		vis[tmp.id]=1;
		for(int i=head[tmp.id];i;i=edge[i].next)
			if(dis[edge[i].to]>dis[tmp.id]+edge[i].dis)
			{
				dis[edge[i].to]=dis[tmp.id]+edge[i].dis;
				if(!vis[edge[i].to])
					q.push((Node){dis[edge[i].to],edge[i].to});
			}
	}
}

//Krusakl重构树
struct Path
{
	int to,from,height;
	bool operator < (const Path &x) const
	{
		return height>x.height;
	}
}path[MAXM];
int father[MAXN<<1],weight[MAXN<<1],tot,f[MAXN<<1][25];//father每个节点的父节点,weight每个节点的点权,f为倍增找根

inline int find(int pos)
{
	if(father[pos]==pos)
		return pos;
	return father[pos]=find(father[pos]);
}
void Kruskal()
{
	sort(path+1,path+1+m);
	tot=n;
	for(int i=1;i<=m;i++)
	{
		int father_x=find(path[i].to);
		int father_y=find(path[i].from);
		if(father_x==father_y)
			continue;
		father[father_x]=father[father_y]=++tot;
		weight[tot]=path[i].height;
		dis[tot]=min(dis[father_x],dis[father_y]);
		f[father_x][0]=f[father_y][0]=tot;
	}
	for(int j=1;(1<<j)<=tot;j++)
		for(int i=1;i<=tot;i++)
			f[i][j]=f[f[i][j-1]][j-1];
}
inline int query(int v,int p)
{
	for(int i=19;i>=0;i--)
		if(f[v][i]&&weight[f[v][i]]>p)
			v=f[v][i];
	return dis[v];
}

void reset()
{
	for(int i=1;i<=n;i++)
		head[i]=0;
	for(int i=1;i<=m*2;i++)
		edge[i].next=0;
	for(int i=1;i<=n*2;i++)
		father[i]=i;
	memset(f,0,sizeof(f));
	memset(vis,0,sizeof(vis));
	memset(dis,0x7f,sizeof(dis));
}

int main()
{
	t=read();
	while(t--)
	{
		lastans=0;
		n=read(),m=read();
		reset();
		for(int i=1;i<=m;i++)
		{
			int u=read(),v=read(),w=read(),h=read();
			add_edge(u,v,w,h);
			add_edge(v,u,w,h);
			path[i].to=u;
			path[i].from=v;
			path[i].height=h;
		}
		dijkstra();
		Kruskal();
		q=read(),k=read(),s=read();
		while(q--)
		{
			v=read(),p=read();
			v=(v+k*lastans-1)%n+1;
			p=(p+k*lastans)%(s+1);
			dijkstra();
			Kruskal();
			lastans=query(v,p);
			write(lastans);
			putchar('\n');
		}
	}
	system("pause");
	return 0;
}
2022/11/12 17:49
加载中...