[Luogu] 逛公园
https://www.luogu.org/problemnew/show/P3953
https://www.zybuluo.com/wsndy-xx/note/1134388
- #include<cstdio>
- #include<cstring>
- #include<cctype>
- using namespace std;
- const int N=1e5+,K=;
- int n,m,k,p,tot,ans=;
- int first[N],next[N<<],en[N<<],w[N<<];
- int first1[N*K],next1[N*K<<],en1[N*K<<],d[N*K];
- int q[N<<],dis[N],dis1[N],u[N<<],v[N<<],t[N<<],f[N*K];
- bool bz[N];
- inline int read() {
- int X=,w=;
- char ch=;
- while(!isdigit(ch)) {
- if(ch=='-') w=-;
- ch=getchar();
- }
- while(isdigit(ch)) X=(X<<)+(X<<)+ch-'',ch=getchar();
- return X*w;
- }
- inline void insert(int x,int y,int z) {
- next[++tot]=first[x];
- first[x]=tot;
- en[tot]=y;
- w[tot]=z;
- }
- inline void insert1(int x,int y) {
- next1[++tot]=first1[x];
- first1[x]=tot;
- en1[tot]=y;
- d[y]++;
- }
- inline int get(int x,int y) {
- return (x-)*(k+)+y+;
- }
- int main() {
- int T=read();
- while(T--) {
- n=read(),m=read(),k=read(),p=read();
- memset(first,tot=,sizeof(first));
- for(int i=; i<=m; i++) {
- u[i]=read(),v[i]=read(),t[i]=read();
- insert(u[i],v[i],t[i]);
- }
- memset(dis,,sizeof(dis));
- int l=dis[]=,r=q[]=;
- while(l<r) {
- int x=q[++l];
- bz[x]=false;
- for(int i=first[x]; i; i=next[i])
- if(dis[x]+w[i]<dis[en[i]]) {
- dis[en[i]]=dis[x]+w[i];
- if(!bz[en[i]]) bz[q[++r]=en[i]]=true;
- }
- }
- memset(first,tot=,sizeof(first));
- for(int i=; i<=m; i++) insert(v[i],u[i],t[i]);
- memset(dis1,,sizeof(dis1));
- l=dis1[q[r=]=n]=;
- while(l<r) {
- int x=q[++l];
- bz[x]=false;
- for(int i=first[x]; i; i=next[i])
- if(dis1[x]+w[i]<dis1[en[i]]) {
- dis1[en[i]]=dis1[x]+w[i];
- if(!bz[en[i]]) bz[q[++r]=en[i]]=true;
- }
- }
- memset(first1,tot=,sizeof(first1));
- memset(d,,sizeof(d));
- for(int i=; i<=m; i++) {
- int x=get(u[i],),y=get(v[i],dis[u[i]]+t[i]-dis[v[i]]);
- for(int j=dis[u[i]]; j+t[i]+dis1[v[i]]<=dis[n]+k; j++,x++,y++) insert1(x,y);
- }
- int num=(k+)*n,sum=;
- l=r=ans=;
- memset(f,,sizeof(f));
- for(int i=; i<=num; i++)
- if(!d[i]) q[++r]=i;
- f[]=;
- while(l<r) {
- int x=q[++l];
- sum++;
- for(int i=first1[x]; i; i=next1[i]) {
- if(!--d[en1[i]]) q[++r]=en1[i];
- f[en1[i]]+=f[x];
- f[en1[i]]=f[en1[i]]>p?f[en1[i]]-p:f[en1[i]];
- }
- }
- if(sum<num) printf("-1\n");
- else {
- for(int i=; i<=k; i++) ans=(ans+f[get(n,i)])%p;
- printf("%d\n",ans);
- }
- }
- return ;
- }
[Luogu] 逛公园的更多相关文章
- [luogu P3953] [noip2017 d1t3] 逛公园
[luogu P3953] [noip2017 d1t3] 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N ...
- [Luogu P3953] 逛公园 (最短路+拓扑排序+DP)
题面 传送门:https://www.luogu.org/problemnew/show/P3953 Solution 这是一道神题 首先,我们不妨想一下K=0,即求最短路方案数的部分分. 我们很容易 ...
- luogu 3953 逛公园
noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...
- Luogu P3953 逛公园(最短路+记忆化搜索)
P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...
- 【图论 动态规划拆点】luoguP3953 逛公园
经典的动态规划拆点问题. 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 NN 个点 MM 条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口, NN 号点是公园的出口,每条边有一个非负 ...
- [vijos P1083] 小白逛公园
不知怎地竟有种错觉此题最近做过= =目测是类似的?那道题貌似是纯动归? 本来今晚想做两道题的,一道是本题,一道是P1653疯狂的方格取数或NOI08 Employee,看看现在的时间目测这个目标又达不 ...
- Bzoj 1756: Vijos1083 小白逛公园 线段树
1756: Vijos1083 小白逛公园 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1021 Solved: 326[Submit][Statu ...
- BZOJ 1756: Vijos1083 小白逛公园
题目 1756: Vijos1083 小白逛公园 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 856 Solved: 264[Submit][Sta ...
- JDOJ-P1260 VIJOS-P1083 小白逛公园
首先,在这里给大家推荐一个网站,https://neooj.com:8082,这是我母校的网站 言归正传,题目描述 VIJOS-P1083 小白逛公园 Time Limit: 1 Sec Memor ...
随机推荐
- Devexpress xaf用代码打开菜单(Navigation Item)
第一种:ViewController继承WindowController public abstract class MyWindowController : WindowController { p ...
- 后端排序,debug模式中map的顺序出错
js中map遍历的顺序是按照插入的顺序来执行的.如果map的来源是字符串转换的,那么就会按照字符串中key值的顺序进行遍历.千万不要被debug中显示的顺序误导,这里应该是为了方便查看对key进行了字 ...
- ingress之tls和path使用
ingress tls 上节课给大家展示了 traefik 的安装使用以及简单的 ingress 的配置方法,这节课我们来学习一下 ingress tls 以及 path 路径在 ingress 对象 ...
- Lua模除运算的大坑
问题 对负数进行模除运算遇到的坑,Lua的%运算与C++的%有差异 实践 结论 Lua%运算的基本公式 a % b = a - ( ( a // b ) * b ) 1.在C,C++中 %运算符的取整 ...
- .Net C# Dictionary 和参数字符串互转
#region Parse #region Dictionary Parse To String /// <summary> /// Dictionary Parse To String ...
- codeforce E - Minimal Labels+hdu 4857
两个题目的意思差不多 都是希望得出的拓扑序如果有多种 要求输出字典序小的情况 这里引用一个大佬的博客 关于为什么不能直接建图然后用小根堆解决这个问题(http://blog.csdn.net/rgno ...
- VS 之github
VS 代码发布到TFS上 1. 登录 visualstudio.com. 进入 https://qgb.visualstudio.com Create Project 这里是相当于新建了一个文件夹 ...
- [NOIP10.3模拟赛]3.w题解--神奇树形DP
题目链接: 咕 闲扯: 这题考场上把子任务都敲满了,5个namespace,400行11k 结果爆0了哈哈,因为写了个假快读只能读入一位数,所以手测数据都过了,交上去全TLE了 把边分成三类:0. 需 ...
- c#如何使用MemoryStream和BinaryFormatter进行对象的序列化和返序列化
1 下面是我写的一个序列化的类 public static class ObjSerialize { /// <summary> /// 将对象数组obj序列化,内存中的缓冲区的数据序列化 ...
- HashMap闭环(死循环)的详细原因(转)
为何出现死循环简要说明 HashMap是非线程安全的,在并发场景中如果不保持足够的同步,就有可能在执行HashMap.get时进入死循环,将CPU的消耗到100%. HashMap采用链表解决Hash ...