跳到正文

题解

题解 CF1442C 【Graph Transpositions】

Lynkcat约 7 分钟
原文
文章信息与版本

文章信息

发布
2020年11月28日
收录
2026年11月6日
更新
2026年11月11日
篇幅
约 7 分钟
获赞
10
题目
CF1442C

版本

  1. v1当前

考虑一种玄学做法。

首先建立分层图

首先观察到 2192^{19} 已经大于当前这个 nn 了,所以我们可以发现:在 1919 层及以上时,如果当前层能到达终点,走到下一层一定不优。

所以我们可以考虑分两种情况:1919 层以下和 1919 层以上。

显然 1919 层以下跑个最短路就行了。

考虑 1919 层以上,我们可以将每层之间的转移的代价设为一个极大值(我设的是 10710^7 )。再跑最短路,最后得出来的结果除以 10710^7 就是转移的次数了。接着最后的答案用快速幂计算即可。

代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=Next[i])
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf (1e18)
#define mod 998244353
#define int ll
#define N 400005
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll a){if(a<0){a=-a; putchar('-');}write(a); putchar(' ');}
inline void writeln(ll a){if(a<0){a=-a; putchar('-');}write(a); puts("");}
ll quickPower(int x,int y)
{
	int ans=1;x%=mod;
	while (y>0)
	{
		if (y&1) (ans*=x)%=mod;
		(x*=x)%=mod;
		y>>=1;
	}
	return ans;
}
int n,m,dis[19][N],dis1[N],u,v,ans,vis[N];
vector<int>G[N][2];
vector<pair<int,int> >G1[N];
struct wcr
{
	int x,y;
	bool operator <(const wcr &A) const
	{
		return dis[x][y]>dis[A.x][A.y];
	}
};
priority_queue<wcr>q;
priority_queue<pair<int,int> >q1;
wcr now;
pair<int,int>now1;
signed main()
{
	n=read(),m=read();
	F(i,0,18)
		F(j,1,n)
			dis[i][j]=inf;
	F(i,1,n) G1[i].push_back(mp(i+n,10000000)),G1[i+n].push_back(mp(i,10000000));
	F(i,1,m) 
	{
		u=read(),v=read();
		G[u][0].push_back(v);
		G[v][1].push_back(u);
		G1[u].push_back(mp(v,1));
		G1[v+n].push_back(mp(u+n,1));
	}
	q.push({0,1});
	dis[0][1]=0;
	while (!q.empty())
	{
		now=q.top();
		q.pop();
		if (now.x<=17)
		{
			if (dis[now.x+1][now.y]>dis[now.x][now.y]+(1ll<<now.x))
			{
				dis[now.x+1][now.y]=dis[now.x][now.y]+(1ll<<now.x);
				q.push({now.x+1,now.y});
			}
		}
		R(i,0,G[now.y][now.x%2].size())
		{
//			cout<<dis[now.x][G[now.y][now.x%2][i]]<<endl;
			if (dis[now.x][G[now.y][now.x%2][i]]>dis[now.x][now.y]+1)
			{
				dis[now.x][G[now.y][now.x%2][i]]=dis[now.x][now.y]+1;
				q.push({now.x,G[now.y][now.x%2][i]});
			}
		}
	}
	ans=inf;
	F(i,0,18) ans=min(ans,dis[i][n]);
	if (ans!=inf) writeln(ans%mod); else
	{
		F(i,1,2*n+5) dis1[i]=inf;
		dis1[1]=0;
		q1.push(mp(0,1));
		while (!q1.empty())
		{
			now1=q1.top();q1.pop();
			if (vis[now1.second]) continue;
			vis[now1.second]=1;
			R(i,0,G1[now1.second].size())
			{
				if (dis1[G1[now1.second][i].first]>dis1[now1.second]+G1[now1.second][i].second)
				{
					dis1[G1[now1.second][i].first]=dis1[now1.second]+G1[now1.second][i].second;
					q1.push(mp(-dis1[G1[now1.second][i].first],G1[now1.second][i].first));
				}
			}
		}
		writeln((quickPower(2,(min(dis1[n],dis1[n+n])/(10000000)))-1+mod+(min(dis1[n],dis1[n+n])%(10000000)))%mod);
	}	
}