代码恢复训练 2024.6.11.
分层图板子。
结束。
代码:
点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
ll st,en;
ll s,dis[300010],vis[300010],p=1,n,m,x,y,z;
ll f(ll x,ll y){
return (x-1)*3+y+1;
}
struct node{
ll dis,u;
bool operator>(const node& a) const{return dis>a.dis;}
};
struct nide{
ll v,w;
};
priority_queue<node,vector<node>,greater<node>>q;
vector<nide>e[300010];
void dij(ll s)
{
forl(i,1,n*3)
dis[i]=1e18,vis[i]=0;
dis[s]=0;
q.push({0,s});
while(!q.empty())
{
ll u=q.top().u;
q.pop();
if(vis[u])
continue;
vis[u]++;
for(nide a:e[u])
{
ll v=a.v,w=a.w;
if(dis[v]>(dis[u]+w))
{
dis[v]=(dis[u]+w);
q.push({dis[v],v});
}
}
}
}
void solve()
{
cin>>n>>m;
forl(i,1,m)
cin>>x>>y,e[f(x,0)].pb({f(y,1),1}),
e[f(x,1)].pb({f(y,2),1}),
e[f(x,2)].pb({f(y,0),1});
cin>>st>>en;
dij(f(st,0));
if(dis[f(en,0)]==1e18)
cout<<-1<<endl;
else
cout<<dis[f(en,0)]/3<<endl;
}
int main()
{
IOS;
t=1;
// cin>>t;
while(t--)
solve();
/******************/
/*while(L<q[i].l) */
/* del(a[L++]);*/
/*while(L>q[i].l) */
/* add(a[--L]);*/
/*while(R<q[i].r) */
/* add(a[++R]);*/
/*while(R>q[i].r) */
/* del(a[R--]);*/
/******************/
QwQ;
}