题解:CF1693C Keshi in Search of AmShZ
我们倒着思考,考虑一种类似 DP 的方法:设
这个式子比较好理解:我们考虑我们最后一定会选一条边走出这个点,钦定当前选定的边是
注意到 DP 值非负,考虑 Dijkstra trick 优化该 DP。我们从终点
代码:
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair <int , int>
#define pb push_back
#define fi first
#define se second
#define fastio ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
using namespace std;
const int MAXN = 2e5 + 10 , INF = 0x3f3f3f3f;
int n , m;
int f[MAXN] , cnt[MAXN];
bitset <MAXN> vis;
struct node
{
int id;
int val;
bool operator < (const node &x) const {return val > x.val;}
};
vector <int> a[MAXN];
priority_queue <node> q;
void solve()
{
q.push({n , 0});
while(!q.empty())
{
auto now = q.top();
q.pop();
if(vis[now.id]) continue;
vis[now.id] = 1;
f[now.id] = now.val;
for(int i : a[now.id])
{
if(vis[i]) continue;
q.push({i , now.val + cnt[i]});
cnt[i]--;
}
}
return;
}
int main()
{
fastio;
cin >> n >> m;
for(int i = 1 ; i <= m ; i++)
{
int u , v;
cin >> u >> v;
a[v].pb(u);
cnt[u]++;
}
memset(f , INF , sizeof(f));
solve();
cout << f[1];
return 0;
}