Gym 101149L Right Build
2.0 s
256 MB
standard input
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.
The first line contains four space-separated integers: n, m, a and b (2 ≤ n, m ≤ 2·105, 1 ≤ a, b ≤ n, a ≠ 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 ≤ n, 1 ≤ yj ≤ n, xj ≠ 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 a single integer — the minimal number of talent points sufficient to acquire talents a and b.
6 8 4 6
0 1
0 2
1 2
1 5
2 3
2 4
3 6
5 6
4
4 6 3 4
0 1
0 2
1 3
2 4
3 4
4 3
3
【题意】
给出一个n个点m条边的有向连通图每次只有起点可以用终点才可以用,初始时0点可以用,问最少要用多少点才能用到a点和b点
【分析】
如果只是要用一个点a,那么求0->a的最短路即可,但是要用两个点就可能0->a和0->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;
}
Gym 101149L Right Build的更多相关文章
- Fastlane- app自动编译、打包多个版本、上传到app store
Fastlane是一套使用Ruby写的自动化工具集,用于iOS和Android的自动化打包.发布等工作,可以节省大量的时间. Github:https://github.com/fastlane/fa ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- Codeforces Gym 100803F There is No Alternative 暴力Kruskal
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 100002 C "Cricket Field" 暴力
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
- How to Build a New Habit: This is Your Strategy Guide
How to Build a New Habit: This is Your Strategy Guide by James ClearRead this on JamesClear.com Acco ...
- Gym 101915
Gym - 101915A Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...
随机推荐
- Android APK反编译详解(附图) (转)
这段时间在学Android应用开发,在想既然是用Java开发的应该很好反编译从而得到源代码吧,google了一下,确实很简单,以下是我的实践过程. 在此郑重声明,贴出来的目的不是为了去破解人家的软件, ...
- 三种方法教你如何用PHP模拟post提交数据
php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...
- password、文件MD5加密,passwordsha256、sha384、sha512Hex等加密
package encryption; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io. ...
- Vue加载组件、动态加载组件的几种方式
https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...
- IPV4地址分类
IPV4地址的分类 私网地址: 10.0.0.0/8 //A类地址 172.16.0.0/16-172.31.0.0/16 ...
- hihocoder第237周:三等分带权树
题目链接 问题描述 给定一棵树,树中每个结点权值为[-100,100]之间的整数.树中包含结点总数不超过1e5.任选两个非根节点A.B,将这两个结点与其父节点断开,可以得到三棵子树.现要求三棵子树的权 ...
- 【Android】详解Android Service
目录结构: contents structure [+] Service简单概述 Service在清单文件中的声明 Service启动服务 Service绑定服务 扩展Binder类 使用Messen ...
- Jmeter进行接口测试
原文地址:https://www.cnblogs.com/nancyzhu/p/8035042.html web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开 ...
- 新手如何学习 jQuery?
可以看张晓菲的<锋利的jQuery>,重点是自己理解函数用法并自行实现一些常用的效果.如果需要快速查阅可以用这个api,每个函数都附有简单的示例:http://api.jquery.com ...
- 详述socket编程之select()和poll()函数
转自:http://www.cppblog.com/myjfm/archive/2011/10/26/159093.aspx select()函数和poll()函数均是主要用来处理多路I/O复用的情况 ...