luogu3563 逛公园
两遍 spfa 然后建立分层图拓扑排序 dp 一下。
写得很差劲。效率很低。
时间复杂度 \(\mathrm{O}(Tnk)\)。
参见这里秒懂。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int T, n, m, k, p, cnt[2], hea[2][100005], dis[2][100005], uu, vv, ww, he[5100005], cn, dp[5100005];
int ind[5100005], f[100005][55];
bool vis[5100005];
queue<int> d;
struct Edge{
int too, nxt, val;
}edge[2][200005], edg[10200005];
void rn(int &x){
char ch=getchar();
x = 0;
while(ch<'0' || ch>'9') ch = getchar();
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
}
void add_edge(int rr, int fro, int too, int val){
edge[rr][++cnt[rr]].nxt = hea[rr][fro];
edge[rr][cnt[rr]].too = too;
edge[rr][cnt[rr]].val = val;
hea[rr][fro] = cnt[rr];
}
void add_edg(int fro, int too){
edg[++cn].nxt = he[fro];
edg[cn].too = too;
he[fro] = cn;
}
void init(){
rn(n); rn(m); rn(k); rn(p);
memset(he, 0, sizeof(he));
memset(dp, 0, sizeof(dp));
memset(hea, 0, sizeof(hea));
memset(vis, 0, sizeof(vis));
memset(ind, 0, sizeof(ind));
memset(dis, 0x3f, sizeof(dis));
dis[0][1] = dis[1][n] = cnt[0] = cnt[1] = cn = 0;
dp[1] = 1;
for(int i=1; i<=m; i++){
rn(uu); rn(vv); rn(ww);
add_edge(0, uu, vv, ww);
add_edge(1, vv, uu, ww);
}
int qwq=0;
for(int i=1; i<=n; i++)
for(int j=0; j<=k; j++)
f[i][j] = ++qwq;
}
void spfa(int rr){
d.push(rr?n:1);
vis[rr?n:1] = true;
while(!d.empty()){
int x=d.front();
d.pop();
vis[x] = false;
for(int i=hea[rr][x]; i; i=edge[rr][i].nxt){
int t=edge[rr][i].too;
if(dis[rr][t]>dis[rr][x]+edge[rr][i].val){
dis[rr][t] = dis[rr][x] + edge[rr][i].val;
if(!vis[t]){
vis[t] = true;
d.push(t);
}
}
}
}
}
void build(){
for(int i=1; i<=n; i++)
for(int j=0; j<=k; j++)
if(dis[0][i]+dis[1][i]+j<=dis[0][n]+k)
vis[f[i][j]] = true;
for(int i=1; i<=n; i++)
for(int j=0; j<=k; j++)
if(vis[f[i][j]])
for(int l=hea[0][i]; l; l=edge[0][l].nxt){
int t=edge[0][l].too, v=j+dis[0][i]+edge[0][l].val-dis[0][t];
if(v<=k && f[t][v]){
add_edg(f[i][j], f[t][v]);
ind[f[t][v]]++;
}
}
}
void topsort(){
for(int i=1; i<=n; i++)
for(int j=0; j<=k; j++)
if(vis[f[i][j]] && !ind[f[i][j]])
d.push(f[i][j]);
while(!d.empty()){
int x=d.front();
d.pop();
for(int i=he[x]; i; i=edg[i].nxt){
int t=edg[i].too;
ind[t]--;
if(ind[t]==0) d.push(t);
dp[t] = dp[t]+dp[x]>=p?dp[t]+dp[x]-p:dp[t]+dp[x];
}
}
}
int chk(){
for(int i=1; i<=f[n][k]; i++)
if(ind[i]!=0)
return -1;
int re=0;
for(int i=0; i<=k; i++)
re = re+dp[f[n][i]]>=p?re+dp[f[n][i]]-p:re+dp[f[n][i]];
return re;
}
int main(){
cin>>T;
while(T--){
init();
spfa(0);
spfa(1);
build();
topsort();
printf("%d\n", chk());
}
return 0;
}
luogu3563 逛公园的更多相关文章
- [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 ...
- [NOIP2017] 逛公园
[NOIP2017] 逛公园 题目大意: 给定一张图,询问长度 不超过1到n的最短路长度加k 的1到n的路径 有多少条. 数据范围: 点数\(n \le 10^5\) ,边数\(m \le 2*10^ ...
- [NOIp 2017]逛公园
Description 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N$号点是公园的出口,每条边有一个非负权值, 代表策策经 ...
- 【NOIP 2017】逛公园
Description 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要 ...
- 逛公园 [NOIP2017 D1T3] [记忆化搜索]
Description 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值,代表策策经过这条边所要花的 ...
- [luogu P3953] [noip2017 d1t3] 逛公园
[luogu P3953] [noip2017 d1t3] 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N ...
随机推荐
- _bzoj1029 [JSOI2007]建筑抢修【贪心 堆】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1029 经典的贪心问题,不解释. #include <cstdio> #inclu ...
- 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...
- 金蝶Apusic中间件适配JetSpeed2过程记录
金蝶Apusic中间件适配JetSpeed2过程记录: 1.安装金蝶并配置域,确保域运行正常. 2.参考<JetSpeed2部署至Apusic操作步骤记录>进行应用迁移. https:// ...
- 在sz
在大城市,sz, 每天骑单车去公交车站. 每天用高德地图 坐快线巴士 车上下班要3个小时. 用guomei 的回收管家 回收 旧空调. 我在kfc 看书 在班车上睡觉/眯眼 在办公室睡觉,看书,工作 ...
- 直接修改HEX修改液晶显示内容的方法
一先通过HEX2bin工具转成bin文件,可粗略看到字节流对应的内容. 二确定原汉字的扫描方式(美术字是图形方式,不确定扫描方式的穷举各种扫描方式),然后根据字体大小.MSB的位置,利用液晶工具生成汉 ...
- PoolManager插件(转载)
http://www.xuanyusong.com/archives/2974 前几天我在博客里面分享了为什么Unity实例化很慢的原因,并且也分享了一个缓存池的工具.有朋友给我留言说PoolMana ...
- C#实现较为实用的SQLhelper
第一次写博客,想不到写什么好b( ̄▽ ̄)d ,考虑的半天决定从sqlhelper开始,sqlhelper对程序员来说就像helloworld一样,很简单却又很重要,helloworld代表着程序员萌新 ...
- 代码文件导到word里
# 创建并写入word文档 import docx import sys import os dir = "D:\\gitwork\\fm.qimeng.c-class\\src\\main ...
- AJPFX关于java中的方法
java中的方法和c语言中的函数类似,是新手入门面向对象之前的内容最大的难关如何写方法 1,明确返回值类型 2,明确参数列表 * 修饰符:目前就用 public stati ...
- 数据库连接池proxool的两种使用方式
数据库连接池可以通过两种方式来应用,针对web应用和非web应用而来. 非web应用的数据库连接池配置 第一种方式:工厂类 非web应用可以使用工厂模式将数据库连接创建封装在一个工具类中,工具类中又使 ...