E - Escape from the Island
Big brother problem solving , code wind really loves
State expression: f (u, j) f(u,j)f(u,j ) currently atuuu point,jjhas been drawnThe shortest distance from the end point at step j .
State transition:
take the initiative to draw one step min, then move to the next pointvvv
f ( u , j ) = f ( v , j + 1 ) + 1 , ( u , v ) ∈ E , ( u , v ) ∈ E f(u,j)=f(v,j+1)+1,(u,v)\in E,(u,v)\in E f(u,j)=f(v,j+1)+1,( u ,v )∈E,( u ,v )∈E
drifts max along the water and moves to the next pointvvv
f ( u , j ) = f ( v , 0 ) + 1 , ( u , v ) ∈ E f(u,j)=f(v,0)+1,(u,v)\in E f(u,j)=f(v,0)+1,( u ,v )∈E
Since we know the state of the final point is f (n, j) = 0 f(n, j) = 0f(n,j)=0 So consider that bfs is updated backwards and
take the initiative to draw a step:(v, j + 1) → (u, j) (v,j+1)\to(u,j)( v ,j+1)→( u ,j )
Drift one step along the water:(v, 0) → (u, j) (v,0)\to(u,j)( v ,0)→( u ,j)
Very dt is the longest path update, while bfs is the shortest update,
such as from (u, j) → (v 1, 0), (v 2, 0)… (vk, 0) (u,j) \to(v_1,0),(v_2,0)\dots(v_k,0)( u ,j)→( v1,0),( v2,0)…( vk,0 ) draws a step. During the bfs process,the point closesttothe end pointwill alwaysbe dequeued first. You might as well set the dequeue order as(v 1, 0), (v 2, 0)… (vk, 0) (v_1, 0),(v_2,0)\dots(v_k,0)( v1,0),( v2,0)…( vk,0 )即 默认f (v 1, 0) ≤ f (v 2, 0) ≤ ⋯ ≤ f (vk, 0) f (v_1,0) \ leq f (v_2,0) \ leq \ dots \ leq f v_k, 0)f(v1,0)≤f(v2,0)≤⋯≤f(vk,0 ) , we only need to let the last dequeue point(vk, 0) (v_k,0)( vk,0 ) update(u, j) (u, j)( u ,j ) OK, that is to recorduuThe out degree of u , when the out degree is 0, it is the last point, update it.
For the case of no bounds, we let it update in place
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
const int N=100010,M=200010;
int h[N],e[M],ne[M],idx;
int d[N];
bool nd[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int f[N][55];
int n,m,k;
void init()
{
memset(h,-1,sizeof(int)*(n+1));idx=0;
memset(d,0,sizeof(int)*(n+1));
memset(nd,0,sizeof(bool)*(n+1));
for(int i=1;i<=n;i++)
for(int j=0;j<=k;j++)
f[i][j]=0x3f3f3f3f;
}
queue<pii> q;
void update(int a,int b,int c,int d)
{
if(f[c][d]==0x3f3f3f3f)
f[c][d]=f[a][b]+1,q.push({
c,d});
}
void bfs()
{
for(int i=0;i<=k;i++)
f[n][i]=0,q.push({
n,i});
while(q.size())
{
auto [u,t]=q.front();q.pop();
if(t>0)
for(int i=h[u];i!=-1;i=ne[i])
update(u,t,e[i],t-1);
else
{
// t==0
for(int i=h[u];i!=-1;i=ne[i])
{
//0代表顺着水流 1代表逆这水流
if(i%2==0) continue; //倒着更新需要逆着水流
--d[e[i]];
if(!d[e[i]])
for(int j=0;j<=k;j++)
update(u,t,e[i],j);
}
if(nd[u])
for(int j=0;j<=k;j++)
update(u,t,u,j);
}
}
}
int main()
{
IO;
int T=1;
cin>>T;
for(int ca=1;ca<=T;ca++)
{
cin>>n>>m>>k;
init();
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
add(u,v),add(v,u);
++d[u];
}
for(int i=1;i<=n;i++)
if(!d[i]) nd[i]=1;//没有出度
bfs();
printf("Case #%d:\n",ca);
for(int i=1;i<=n;i++)
printf("%d\n",(f[i][0]==0x3f3f3f3f?-1:f[i][0]));
}
return 0;
}
Come on~