最短路 + 记忆化

记忆化搜索更容易实现


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue> using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
const int oo = (1 << 30); int head1[N], head2[N], cnt;
struct Node {int v, w, nxt;} G1[M], G2[M]; #define gc getchar()
inline int read() {int x = 0; char c = gc; while(c < '0' || c > '9') c = gc;
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x;}
#undef gc int n, m, k, Mod;
int dis[N];
bool vis[N];
int f[N][55];
bool c[N][55]; inline void Link(int u, int v, int w) {
++ cnt;
G1[cnt].v = v, G1[cnt].w = w, G1[cnt].nxt = head1[u], head1[u] = cnt;
swap(u, v);
G2[cnt].v = v, G2[cnt].w = w, G2[cnt].nxt = head2[u], head2[u] = cnt;
} void Init() {
cnt = 0;
for(int i = 1; i <= n; i ++) head1[i] = head2[i] = -1;
memset(f, -1, sizeof f);
memset(c, 0, sizeof c);
for(int i = 1; i <= m; i ++) {
int u = read(), v = read(), w = read();
Link(u, v, w);
}
} int Q[N << 2]; struct Node_ {
int u, dis_;
bool operator < (Node_ a) const {
return dis_ > a.dis_;
}
}; priority_queue <Node_> Que; void Dij() {
for(int i = 1; i <= n; i ++) vis[i] = 0, dis[i] = oo;
dis[1] = 0;
Que.push((Node_) {1, 0});
while(!Que.empty()) {
Node_ top_ = Que.top();
Que.pop();
if(vis[top_.u]) continue;
vis[top_.u] = 1;
int u = top_.u;
for(int i = head1[u]; ~ i; i = G1[i].nxt) {
int v = G1[i].v;
if(dis[v] > dis[u] + G1[i].w) {
dis[v] = dis[u] + G1[i].w;
Que.push((Node_) {v, dis[v]});
}
}
}
} bool flag; int Dp(int u, int K) {
if(flag == 0) return 0;
if(~ f[u][K]) return f[u][K];
f[u][K] = 0, c[u][K] = 1;
for(int i = head2[u]; ~ i; i = G2[i].nxt) {
int v = G2[i].v;
int t = dis[u] + K - dis[v] - G2[i].w;
if(t < 0) continue;
if(c[v][t]) {flag = 0; return 0;}
(f[u][K] += Dp(v, t)) %= Mod;
}
c[u][K] = 0;
return f[u][K];
} void Solve() {
Dij();
f[1][0] = 1;
int Ans = 0;
flag = 1;
for(int i = 0; i <= k; i ++) {
(Ans += Dp(n, i)) %= Mod;
if(flag == 0) {
cout << "-1" << "\n";
return ;
}
}
cout << Ans << "\n";
} int main() {
for(int T = read(); T; T --) {
n = read(), m = read(), k = read(), Mod = read();
Init();
Solve();
}
return 0;
}

loj #2316的更多相关文章

  1. [Noi2016]区间 BZOJ4653 洛谷P1712 Loj#2086

    额... 首先,看到这道题,第一想法就是二分答案+线段树... 兴高采烈的认为我一定能AC,之后发现n是500000... nlog^2=80%,亲测可过... 由于答案是求满足题意的最大长度-最小长 ...

  2. Loj #2192. 「SHOI2014」概率充电器

    Loj #2192. 「SHOI2014」概率充电器 题目描述 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品--概率充电器: 「采用全新纳米级加工技术,实现元件与导线能否通电完 ...

  3. Loj #3096. 「SNOI2019」数论

    Loj #3096. 「SNOI2019」数论 题目描述 给出正整数 \(P, Q, T\),大小为 \(n\) 的整数集 \(A\) 和大小为 \(m\) 的整数集 \(B\),请你求出: \[ \ ...

  4. Loj #3093. 「BJOI2019」光线

    Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ...

  5. Loj #3089. 「BJOI2019」奥术神杖

    Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ...

  6. Loj #2542. 「PKUWC2018」随机游走

    Loj #2542. 「PKUWC2018」随机游走 题目描述 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次 ...

  7. Loj #2331. 「清华集训 2017」某位歌姬的故事

    Loj #2331. 「清华集训 2017」某位歌姬的故事 IA 是一名会唱歌的女孩子. IOI2018 就要来了,IA 决定给参赛选手们写一首歌,以表达美好的祝愿.这首歌一共有 \(n\) 个音符, ...

  8. 【LOJ#3097】[SNOI2019]通信(费用流)

    [LOJ#3097][SNOI2019]通信(费用流) 题面 LOJ 题解 暴力就直接连\(O(n^2)\)条边. 然后分治/主席树优化连边就行了. 抄zsy代码,zsy代码是真的短 #include ...

  9. 【LOJ#3096】[SNOI2019]数论

    [LOJ#3096][SNOI2019]数论 题面 LOJ 题解 考虑枚举一个\(A\),然后考虑有多少个合法的\(B\). 首先这个数可以写成\(a_i+kP\)的形式,那么它模\(Q\)的值成环. ...

随机推荐

  1. PowerBuilder学习笔记之删除和加载PBL文件的方法

    删除PBL目录的方法:直接点删除键删除 加载PBL文件的方法:点Browse按钮选择PBL文件

  2. 使用postman mock server

    需要写一个小的Java程序,用来调用云平台的接口 由于云平台的接口程序还没有写好,只能用模拟的方式先行开发代码, 用了post来模拟接口程序. 需要模拟的接口如下: ■请求地址 /openapi/ip ...

  3. Java UpperBound

    Java UpperBound /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  4. 使用Harbor搭建Docker私有镜像仓库

    Harbor介绍:https://goharbor.io/ 前置条件 需要安装了docker和docker-compose 下载Harbor 在harbor下载页(https://github.com ...

  5. jQuery 事件介绍

    什么是事件?页面对不同访问者的响应叫做事件.事件处理程序指的是当 HTML 中发生某些事件时所调用的方法. 常用的时间主要有以下几种: click()事件:click() 方法是当按钮点击事件被触发时 ...

  6. java利用反射打印出类的结构

    1 输入一个类名:java.lang.String将打印出String类定义的结构,例如: public final class java.lang.String { public java.lang ...

  7. 安卓开发之生成cache目录和files目录

    package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...

  8. DBShop后台RCE之曲线救国

    本文最早发布在朋友的公众号 黑客信徒 中,文章是自己写的 不存在抄袭  特此申明 --------------------- 前言 DBShop是一款基于ZendFramework2框架的电子商务系统 ...

  9. Android利用系统原生BottomNavigationView实现底部导航

    <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" androi ...

  10. 【leetcode】280.Wiggle Sort

    原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...