洛谷 3953 NOIP2017提高组Day1 T3 逛公园


【题解】
先建反向图,用dijkstra跑出每个点到n的最短距离dis[i]
设f[u][k]表示dis(u,n)<=mindis(u,n)+k的方案数。对于边e(u,v,w),走了这条边的话需要多走的距离就是这条边的边权-原来u,v之间的距离,即w-(dis[u]-dis[v])
那么转移就是f[u][k]=sigma( f[v][k-w+(dis[u]-dis[v])] ),记忆化搜索非常好写。
判无数解的话记录当前状态是否在栈中就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define rg register
#define N 200010
using namespace std;
int T,n,m,k,p,tot,last[N],dis[N],pos[N],f[N][];
bool in[N][];
struct edge{int to,pre,dis;}e[N];
struct heap{int p,d;}h[N];
struct rec{int u,v,w;}r[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void MOD(int &k){if(k>=p) k-=p;}
inline void up(int x){
int fa;
while((fa=x>>)&&h[fa].d>h[x].d) swap(h[x],h[fa]),swap(pos[h[x].p],pos[h[fa].p]),x=fa;
}
inline void down(int x){
int son;
while((son=x<<)<=tot){
if(h[son+].d<h[son].d&&son<tot) son++;
if(h[son].d<h[x].d) swap(h[x],h[son]),swap(pos[h[x].p],pos[h[son].p]),x=son;
else return;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=1e9;
h[pos[x]=tot=]=(heap){x,dis[x]=};
while(tot){
int now=h[].p; pos[h[tot].p]=; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now],to;i;i=e[i].pre)if(dis[to=e[i].to]>dis[now]+e[i].dis){
dis[to]=dis[now]+e[i].dis;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].d=dis[to];
up(pos[to]);
}
}
}
int dfs(int x,int d){
if(in[x][d]) return -;
if(f[x][d]) return f[x][d];
in[x][d]=; f[x][d]=(x==n)?:;
for(rg int i=last[x],to,num;i;i=e[i].pre){
int tmp=-dis[x]+dis[to=e[i].to]+e[i].dis;
if(tmp<=d){
if((num=dfs(to,d-tmp))==-) return -;
MOD(f[x][d]+=num);
}
}
return in[x][d]=,f[x][d];
}
inline void Pre(){
memset(in,,sizeof(in));
memset(last,,sizeof(last));
memset(pos,,sizeof(pos));
memset(f,,sizeof(f));
tot=;
}
int main(){
T=read();
while(T--){
Pre();
n=read(); m=read(); k=read(); p=read();
for(rg int i=,u,v;i<=m;i++){
r[i].u=u=read(); r[i].v=v=read();
e[++tot]=(edge){u,last[v],r[i].w=read()}; last[v]=tot;
}
dijkstra(n);
memset(last,,sizeof(last)); tot=;
for(rg int i=;i<=m;i++){
int u=r[i].u,v=r[i].v;
e[++tot]=(edge){v,last[u],r[i].w}; last[u]=tot;
}
printf("%d\n",dfs(,k));
}
return ;
}
洛谷 3953 NOIP2017提高组Day1 T3 逛公园的更多相关文章
- 【前缀和】【前缀MAX】洛谷 P1351 NOIP2014提高组 day1 T2 联合权值
不难发现,树中与某个点距离为2的点只可能是它的父亲的父亲.儿子的儿子 或者 兄弟,分类讨论一下即可. 只有对于兄弟我们不能暴力搞,维护一下每个节点的所有儿子的前缀和.前缀MAX就行了. #includ ...
- 【模拟】洛谷 P1328 NOIP2014提高组 day1 T1 生活大爆炸版石头剪刀布
把所有情况打表,然后随便暴力. #include<cstdio> using namespace std; int n,an,bn,p1,p2; ],b[]; ][]; int ans1, ...
- Noip2011 提高组 Day1 T3 Mayan游戏
题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...
- 洛谷P1312 [NOIP2011提高组Day1T3]Mayan游戏
Mayan游戏 题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游 ...
- 洛谷P1003 [NOIP2011提高组Day1T1]铺地毯
P1003 铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号 ...
- 洛谷P1080 [NOIP2012提高组D1T2]国王游戏 [2017年5月计划 清北学堂51精英班Day1]
P1080 国王游戏 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 ...
- 洛谷 P2678 & [NOIP2015提高组] 跳石头
题目链接 https://www.luogu.org/problemnew/show/P2678 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布 ...
- 洛谷 P1025 & [NOIP2001提高组] 数的划分(搜索剪枝)
题目链接 https://www.luogu.org/problemnew/show/P1025 解题思路 一道简单的dfs题,但是需要剪枝,否则会TLE. 我们用dfs(a,u,num)来表示上一个 ...
- 洛谷P1514 [NOIP2010提高组T4]引水入城
P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城 ...
随机推荐
- NOIp 2017 奶酪 【并查集】 By cellur925
题目传送门 Orz去年考场上做这道题的我应该还在抱怨没学过空间几何,不一会太困了就开始打瞌睡,然后为了防止睡觉开始在devc++上写默写离骚(逃 思路:如果两个空洞相交,那么把他们并在一个集合里.最后 ...
- C++中的static修饰的变量和函数
原文地址:http://blog.csdn.net/he3913/archive/2008/09/18/2944737.aspxC++里的静态成员函数(不能用const的原因+static在c++中的 ...
- [POI2001]Goldmine
Description Byteman作为Byteland的The Goldmine(某一公司厂矿)的最有功的雇员之一,即将在年末退休.为了表示对他的 认真勤恳的工作的承认,The Goldmine的 ...
- _bzoj2818 Gcd【线性筛法 欧拉函数】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 若gcd(x, y) = 1,则gcd(x * n, y * n) = n.那么,当y ...
- _bzoj1015 [JSOI2008]星球大战starwar【并查集】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 倒过来做就ok了. #include <cstdio> #include ...
- C语言中Static和Const关键字的的作用 -- 转
static作用:“改变生命周期” 或者 “改变作用域” 程序的局部变量存在于(堆栈)中,全局变量存在于(静态区 )中,动态申请数据存在于( 堆)中. 1.作用于变量: 用static声明局部变量-- ...
- java IO流 复制图片
(一)使用字节流复制图片 //字节流方法 public static void copyFile()throws IOException { //1.获取目标路径 //(1)可以通过字符串 // St ...
- absolute元素水平居中
原始(未居中): .con{ width:200px; height:200px; background:#ccc; position:relative; } .abs{ width:40px; he ...
- CCF|碰撞的小球
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...