# 【bzoj1354: [Baltic2005]Bus Trip】图论+动态规划

## 1354: [Baltic2005]Bus Trip

Time Limit: 5 Sec   Memory Limit: 162 MB
Submit: 121   Solved: 86
[ Submit][ Status][ Discuss]

## Input

The input file name is TRIP.IN. The first line contains the integer numbers N (1<=N<=50,000), M (1<=M<=100,000), P ( 1<=P<=N), and T (0<=T<=1,000,000,000).
The following M lines describe the bus routes. Each line contains the integer numbers si, ti, ai, bi, ci, di, where si and ti are the source and destination towns of the bus route i, and ai, bi, ci, di describe the departure and arrival times as explained above (1<=si<=N, 1<=ti<=N, 0<=ai<=bi < ci<=di<=1,000,000,000).

3 6 2 100
1 3 10 20 30 40
3 2 32 35 95 95
1 1 1 1 7 8
1 3 8 8 9 9
2 2 98 98 99 99
1 2 0 0 99 101

## Sample Output

32

#include<cstdio>
#include<algorithm>
#define N 200005
#define INF 2e9
using namespace std;
struct he{
int s,t,a,b,c,p;
}b[N];
int dis[N],f[N],n,m,P,T,cnt;
bool cmp(he a,he b){
if(a.a==b.a) return (a.p&1)>(b.p&1);
return a.a<b.a;
}
void work(){
for(int i=1;i<=n;i++) dis[i]=-INF;
dis[1]=0;
for(int i=1;i<=cnt;i++)
if(b[i].a<=T){
if(b[i].p&1){
if(f[b[i].p/2]+b[i].c-b[i].b>dis[b[i].t]) {
dis[b[i].t]=f[b[i].p/2]+b[i].c-b[i].b;
}
}else{
f[b[i].p/2]=dis[b[i].s];
}
}else break;
if(dis[P]<0) printf("-1\n");
else printf("%d\n",T-dis[P]);
}
int main(){
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&P,&T);
for(int i=1;i<=m;i++){
cnt++;
scanf("%d%d%d%d%d%d",&b[cnt].s,&b[cnt].t,&b[cnt].a,&b[cnt].b,&b[cnt].c,&b[cnt+1].a);
b[cnt+1].s=b[cnt].s;b[cnt+1].t=b[cnt].t;
b[cnt+1].b=b[cnt].b;b[cnt+1].c=b[cnt].c;
b[cnt].p=i*2;b[cnt+1].p=i*2+1;cnt++;
}
//	for(int i=1;i<=cnt;i++)
//	 printf("%d %d %d %d %d %d \n",b[i].s,b[i].t,b[i].a,b[i].b,b[i].c,b[i].p);
sort(b+1,b+1+cnt,cmp);
work();
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

BinGoo0o0o

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
11-22 21

08-05 167
08-18 316
08-25 1113
06-03 38