建立新图,原图中每条边在新图中是点,新图中每个点的点权为$-e[i].c+e[i].b$,边权为$0$。

若$e[i].d\leq e[j].a$,则连一条$i$到$j$的单向边。

对于原图中每个点,将所有入边和出边分别排序,然后建立一排虚点表示后缀,通过双指针将边数优化至$O(m)$。

在新图中求出最短路,最后将答案加上$T$即可。

注意到新图是个DAG,因此可以记搜求解。

时间复杂度$O(m\log m)$。

#include<cstdio>
#include<algorithm>
const int N=50010,M=100010;
int n,m,cnt,P,T,i,j,k,gi[N],go[N],g[M*2],v[M*5],nxt[M*5],ed,a[M],b[M],ca,cb,vis[M*2],f[M*2],ans;
struct E{int x,y,a,b,c,d;}e[M];
inline bool cmpa(int x,int y){return e[x].d<e[y].d;}
inline bool cmpb(int x,int y){return e[x].a<e[y].a;}
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline void add(int*g,int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
inline void up(int&x,int y){if(x>y)x=y;}
int dp(int x){
if(vis[x])return f[x];
vis[x]=1;
int&t=f[x];
t=1;
if(x<=m)if(e[x].x==1)t=0;
for(int i=g[x];i;i=nxt[i])up(t,dp(v[i]));
if(t<1&&x<=m)t+=e[x].b-e[x].c;
return t;
}
int main(){
read(n),read(m),read(P),read(T);
for(i=1;i<=m;i++){
read(e[i].x),read(e[i].y),read(e[i].a),read(e[i].b),read(e[i].c),read(e[i].d);
add(go,e[i].x,i);
add(gi,e[i].y,i);
}
cnt=m;
for(i=1;i<=n;i++){
for(ca=0,j=gi[i];j;j=nxt[j])a[++ca]=v[j];
if(!ca)continue;
for(cb=0,j=go[i];j;j=nxt[j])b[++cb]=v[j];
if(!cb)continue;
std::sort(a+1,a+ca+1,cmpa);
std::sort(b+1,b+cb+1,cmpb);
for(j=1;j<=cb;j++){
if(j<cb)add(g,cnt+j+1,cnt+j);
add(g,b[j],cnt+j);
}
for(j=ca,k=cb+1;j;j--){
while(k>1&&e[a[j]].d<=e[b[k-1]].a)k--;
if(k<=cb)add(g,cnt+k,a[j]);
}
cnt+=cb;
}
if(P!=1)ans=1;
for(i=1;i<=m;i++)if(e[i].y==P&&e[i].d<=T){
dp(i);
if(f[i]<1)up(ans,f[i]);
}
if(ans>0)ans=-1;else ans+=T;
return printf("%d",ans),0;
}

  

BZOJ1395 : [Baltic2005]Trip的更多相关文章

  1. BZOJ 1395 [Baltic2005]Trip(最短路+DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1354 [题目大意] 给出一些车的班次,包括起点,终点,到达起点时间区间, 到达终点时间 ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. Lesson 4 An existing trip

    Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...

  4. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  5. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

  6. 1301. The Trip

    A number of students are members of a club that travels annually to exotic locations. Their destinat ...

  7. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  8. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  9. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

随机推荐

  1. springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布

    一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...

  2. Bootstrap3.0学习第一轮(入门)

    详情请查看 http://aehyok.com/Blog/Detail/7.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  3. 使用C#进行应用程序间通信(WPF与Unity通信)

    首先程序主体来自网络,我只是应用在我自己的项目中,其中出现了一系列的问题,有些已经解决,有些使用了折中的方案,如果有大神能够给予知道,感激不尽! 首先是发送端程序: 这是我的程序任务执行主界面,此处已 ...

  4. 基于TLS的EAP 认证方法

    TLS: transport level security , 安全传输层协议,用于在两个通信应用程序之间提供保密性和数据完整性.该协议由两层组成: TLS 记录协议(TLS Record)和 TLS ...

  5. 使用Navicat Premium对sqlserver 2008进行表、字段及用户权限的精细化管理

    在一些特殊的业务场景,我们需要对数据库进行精细化的管理,比如只能授权给某用户某个表的操作权限,最小权限法则可以保障数据库最大的安全.利用navicat这个轻量化的工具可以很轻松的解决这个问题 1.通过 ...

  6. android 手机拍照返回 Intent==null 以及intent.getData==null

    手机拍照第一种情况:private void takePicture(){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Si ...

  7. compile php with openssl on mac osx error 填坑

    从源码手动编译 PHP 时出现如下错误:           Default   1 2 3 4 5 6 7 8 9 10 11 12 13 14 Undefined symbols for arch ...

  8. C++ code:for loop designs

    1  用for循环编出系列图形 该图形一共10行,每一行增加一个字符,所以应循环10次,每次输出一行.其循环模式为: :i<=;++i) { 输出第i行 换行 } 我们注意到,每一行长度的变化正 ...

  9. java uitl

    Random类  //生成随机数 https://i.cnblogs.com/EditPosts.aspx?opt=1

  10. Python decorator

    1.编写无参数的decorator Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...