隐藏页面特效

Gym 101149L Right Build

L. Right Build
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

In a MMORPG «Path of Exile» characters grow by acquiring talents for special talent points received in a game process. Talents can depend on others. A talent can be acquired only if a character has at least one of the talents it depends on. Acquiring one talent costs one talent point. At the beginning of the game a character has a single starting talent.

Schoolboy Vasiliy decided to record a video manual how to level-up a character in a proper way, following which makes defeating other players and NPCs very easy. He numbered all  talents as integers from 0 to n, so that the starting talent is numbered as 0. Vasiliy thinks that the only right build is acquiring two different talents a and b as quickly as possible because these talents are imbalanced and much stronger than any others. Vasiliy is lost in thought what minimal number of talent points is sufficient to acquire these talents from the start of the game.

Input

The first line contains four space-separated integers: nma and b (2 ≤ n, m ≤ 2·1051 ≤ a, b ≤ na ≠ b) — the total number of talents, excluding the starting talent, the number of dependencies between talents, and the numbers of talents that must be acquired.

Each of the next m lines contains two space-separated integers: xj and yj (0 ≤ xj ≤ n1 ≤ yj ≤ nxj ≠ yj), which means the talent yjdepends on the talent xj. It's guaranteed that it's possible to acquire all  talents given the infinite number of talent points.

Output

Output a single integer — the minimal number of talent points sufficient to acquire talents a and b.

Examples
input
Copy
6 8 4 6 0 1 0 2 1 2 1 5 2 3 2 4 3 6 5 6
output
Copy
4
input
Copy
4 6 3 4 0 1 0 2 1 3 2 4 3 4 4 3
output
Copy
3

 

【题意】

给出一个n个点m条边的有向连通图每次只有起点可以用终点才可以用,初始时0点可以用,问最少要用多少点才能用到a点和b

 

【分析】

如果只是要用一个点a,那么求0->a的最短路即可,但是要用两个点就可能0->a0->b都不是最短路,但是由于路径重叠很多点可以被用两次以节省用的点数,但是注意到两条路径只会是开始时重叠在一次,之后分开就再也不会相交,因为如果再次相交不如之前全部重叠更优,那么我们枚举这个叉点c,求出0->c,a->c,b->c的最短路来更新最优解即可

 

【代码】

#include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int N=2e5+5; typedef long long ll; #define pir pair<int,int> #define m(s) memset(s,0,sizeof s); int n,m,a,b,dis[3][N]; struct node{int v,w,next;}e[N<<2];int tot,head[N];bool vis[N]; struct data{int x,y,z;}ed[N]; inline void add(int x,int y,int z){ e[++tot].v=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot; } inline void Init(){ scanf("%d%d%d%d",&n,&m,&a,&b); for(int i=1;i<=m;i++) scanf("%d%d",&ed[i].x,&ed[i].y),add(ed[i].x+1,ed[i].y+1,1); memset(dis[0],0x3f,sizeof dis[0]); } #define mp make_pair inline void dijkstra(int S,int t){ priority_queue<pir,vector<pir>,greater<pir> >q; q.push(mp(dis[t][S]=0,S)); while(!q.empty()){ pir p=q.top();q.pop(); int x=p.second; if(vis[x]) continue; vis[x]=1; for(int i=head[x];i;i=e[i].next){ int v=e[i].v; if(!vis[v]&&dis[t][v]>dis[t][x]+e[i].w){ q.push(mp(dis[t][v]=dis[t][x]+e[i].w,v)); } } } } inline void out(int x){ for(int i=1;i<=n+1;i++) printf("%12d",dis[x][i]);puts(""); } inline void Solve(){ dijkstra(1,0); tot=0;m(head); for(int i=1;i<=m;i++) add(ed[i].y+1,ed[i].x+1,1); m(vis);memset(dis[1],0x3f,sizeof dis[1]); dijkstra(++a,1); m(vis);memset(dis[2],0x3f,sizeof dis[2]); dijkstra(++b,2); int ans=2e9; for(int i=1;i<=n+1;i++) ans=(int)min((ll)ans,(ll)dis[0][i]+dis[1][i]+dis[2][i]); printf("%d",ans); } int main(){ Init(); Solve(); return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/10420864.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(161)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2017-02-22 [Sdoi2013]直径(树的直径)
2017-02-22 3123: [Sdoi2013]森林
2017-02-22 [Sdoi2013]随机数生成器(BSGS)
2017-02-22 [SDOI2011]计算器
点击右上角即可分享
微信分享提示