D. Legacy
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.

There are n planets in their universe numbered from 1 to n. Rick is in planet number s (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial.

By default he can not open any portal by this gun. There are q plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.

Plans on the website have three types:

  1. With a plan of this type you can open a portal from planet v to planet u.
  2. With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
  3. With a plan of this type you can open a portal from any planet with index in range [l, r] to planet v.

Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.

Input

The first line of input contains three integers n, q and s (1 ≤ n, q ≤ 105, 1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.

The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).

Output

In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or  - 1 if it's impossible to get to that planet.

Examples
Input
3 5 1
2 3 2 3 17
2 3 2 2 16
2 2 2 3 3
3 3 1 1 12
1 3 3 17
Output
0 28 12 
Input
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
Output
0 -1 -1 12 
Note

In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.

题目连接:http://codeforces.com/contest/787/problem/D

题意:有n个点,q个询问,每次询问有一种操作。操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w;操作2:[l,r]→u的距离为w;操作3:u到v的距离为w;求起点到其他点的最短距离,到达不了输出-1。

思路:线段树建立模型,搜索最短路。最好不要用map之类的,容易超时。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=5e6+,inf=0x3f3f3f3f,MOD=1e9+;
const ll INF=1e17+;
struct node
{
int to;
ll w;
int next;
} edge[MAXN];
int cou,head[MAXN];
int Max;
int vis[MAXN];
ll ans[MAXN];
void init()
{
cou=;
Max=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void add(int u,int v,ll w)
{
cou++;
edge[cou].to=v;
edge[cou].w=w;
edge[cou].next=head[u];
head[u]=cou;
///cout<<u<<" "<<v<<" "<<w<<endl;
}
void build(int l,int r,int pos,int flag,int t)
{
if(t==) Max=max(Max,pos+flag);
if(l==r)
{
if(t==) add(pos+flag,l,0LL);
else add(l,pos+flag,0LL);
return;
}
if(t==)
{
add(pos+flag,(pos<<)+flag,0LL);
add(pos+flag,(pos<<|)+flag,0LL);
}
else
{
add((pos<<)+flag,pos+flag,0LL);
add((pos<<|)+flag,pos+flag,0LL);
}
int mid=(l+r)>>;
build(l,mid,pos<<,flag,t);
build(mid+,r,pos<<|,flag,t);
}
void update(int L,int R,ll w,int l,int r,int pos,int flag,int u,int t)
{
if(L<=l&&r<=R)
{
if(t==) add(u,pos+flag,w);
else add(pos+flag,u,w);
return;
}
int mid=(l+r)>>;
if(L<=mid) update(L,R,w,l,mid,pos<<,flag,u,t);
if(R>mid) update(L,R,w,mid+,r,pos<<|,flag,u,t);
}
struct mmp
{
int s;
ll dis;
mmp() {}
mmp(ll ss,ll d)
{
s=ss,dis=d;
}
bool operator <(const mmp &x)const
{
return dis>x.dis;
}
};
priority_queue<mmp>q;
void dij(int s)
{
ans[s]=0LL;
q.push(mmp(s,0LL));
while(!q.empty())
{
mmp now=q.top();
q.pop();
int u=now.s;
if(vis[u]) continue;
vis[u]=;
for(int i = head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
ll w=edge[i].w;
if(ans[v]>now.dis+w)
{
///cout<<u<<" "<<v<<" "<<ans[u]+w<<endl;
q.push(mmp(v,now.dis+w));
ans[v]=now.dis+w;
}
}
}
}
int main()
{
int n,q,s;
scanf("%d%d%d",&n,&q,&s);
init();
build(,n,,n+,);
build(,n,,Max+,);
for(int i=; i<=q; i++)
{
int t;
scanf("%d",&t);
if(t==)
{
int u,v;
ll w;
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
}
else
{
int u,l,r;
ll w;
scanf("%d%d%d%lld",&u,&l,&r,&w);
if(t==) update(l,r,w,,n,,n+,u,t);
else update(l,r,w,,n,,Max+,u,t);
}
}
for(int i=;i<MAXN;i++) ans[i]=INF;
dij(s);
for(int i=; i<=n; i++)
{
if(ans[i]>=INF) cout<<"-1 ";
else cout<<ans[i]<<" ";
}
cout<<endl;
return ;
}

线段树+搜索

Codeforces 787D. Legacy 线段树建模+最短路的更多相关文章

  1. codeforces 787D - Legacy 线段树优化建图,最短路

    题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...

  2. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  3. Codeforces 787D Legacy 线段树 最短路

    题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...

  4. Codeforces 786B. Legacy 线段树+spfa

    题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...

  5. Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)

    题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...

  6. CodeForces - 786B Legacy (线段树+DIjkstra+思维)

    题意:给N个点和Q条选项,有三种类型的选项:1.从u到v花费w修建一条路:2.从u到下标区间为[L,R]的点花费w修建一条路; 3.从下标区间为[L,R]的点到u花费w修建一条路. 然后求起点s到其余 ...

  7. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  8. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  9. 786B - Legacy(线段树 + 最短路)线段树优化建图

    题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...

随机推荐

  1. springcloud eureka.instance

    1.在springcloud中服务的 Instance ID 默认值是: ${spring.cloud.client.hostname}:${spring.application.name}:${sp ...

  2. jdbc连接模拟用户登陆密码判断

    package com.aaa.demo1; import com.aaa.utils.JdbcUtils; import java.sql.Connection; import java.sql.P ...

  3. ssh远程端口转发

    当ssh的连接方向和应用连接的方向不一致时,这就称为ssh远程转发. 主机3是一台web server 应用请求是主机2到主机1 ssh请求是主机1到主机2 主机2开启ssh服务 service ss ...

  4. JMeter3.0(三十八)图形化HTML报告中文乱码问题处理(转载)

    转载自 http://www.cnblogs.com/yangxia-test 由于个人在JMeter 3.0的实际应用中,脚本中的Test Plan/Sampler等元件命名都没有使用中文,所以在之 ...

  5. Jenkins 传递自定义的参数

    1.同一个job之间,不同的shell之间传递参数 注意:如果是job参数化构建自定义的参数,可以在job的不同shellj间引用,但是不能改变他的值供后面的shell使用   job在执行时会针对所 ...

  6. rancher 2 安装 longhorn

    宿主机为centos 7 注意一定要安装open-iscsi yum install iscsi-initiator-utils 集群--默认命名空间--目录应用--longhorn 安装

  7. 替换空格(python)

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. # -*- coding:ut ...

  8. ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    这是因为我装了tensorflow-gpu 1.5版本,而我用的是cuda 8.0和cudnn6.0,1.5版本要求cuda 9.0,我的做法就是回滚tensorflow版本.

  9. 转:WEB前端性能优化规则

    14条规则摘自<High Performance Web Sites>,本文地址 1.减少Http请求 使用图片地图 使用CSS Sprites 合并JS和CSS文件 这个是由于浏览器对同 ...

  10. jQuery禁止Ajax请求缓存

    一 现象 get请求在有些浏览器中会缓存.浏览器不会发送请求,而是使用上次请求获取到的结果. post请求不会缓存.每次都会发送请求. 二 解决 jQuery提供了禁止Ajax请求缓存的方法: $.a ...