建立新图,原图中每条边在新图中是点,新图中每个点的点权为$-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. Async 详解

    一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...

  2. CAS 策略已被 .NET Framework 弃用

    背景 本来想这里有啥写的,就算了吧.突然看到dev了,我的天啊,这个.net大神,坑了多少开发人员了.功能太强大,以至于后来很长时间我都不知道jquery.当时为了操作dev,为了实现一个功能,都把官 ...

  3. 通达OA系统故障解决案例记录

    案例1: 现象:在人员访问量大的时候OA系统经卡死,并且经常宕机,需要启动apache服务 优化配置如下: D:\MYOA\conf\http.conf 修改参数如下: <IfModule mp ...

  4. better-scroll插件

    参考网址:慕课网  http://coding.imooc.com/lesson/74.html#mid=1640 这个详细:https://zhuanlan.zhihu.com/p/25369923 ...

  5. Laravel 禁用指定 URL POST 请求的 csrf 检查

    由于在 chrome 插件中使用了跨域请求,所以需要禁用掉 laravel 默认的 post csrf 检查. 配置方法: 在 app/Http/Middleware/VerifyCsrfToken. ...

  6. hdu1754splaytree区间查询

    以前用线段树做的题..发现splay好神奇 splay的区间查询就是把那个区间移到两个节点之间进行操作即可,同时每次rotate不要忘记pushup #include<iostream> ...

  7. hdu1255扫描线计算覆盖两次面积

    总体来说也是个模板题,但是要开两个线段树来保存被覆盖一次,两次的面积 #include<iostream> #include<cstring> #include<cstd ...

  8. python 全栈开发,Day9(函数的初始,返回值,传参,三元运算)

    一.函数的初始 比如python没有len()方法,如何求字符串的长度使用for循环 s = 'fdshfeigjoglfkldsja' count = 0 for i in s: count += ...

  9. bootstrap改变上传文件按钮样式,并显示已上传文件名

    参考博文: html中,文件上传时使用的<input type="file">的样式自定义 html中<input type="file"&g ...

  10. javax.inject包

    javax.inject包 java提出的依赖注入标准,有别于以下传统的对象获取方式 构造方法 工厂模式 服务器定位模式(e.g. JNDI) 开发过程中是会有很多层层依赖的对象的,例如,Stopwa ...