Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
2 seconds
256 megabytes
standard input
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:
- With a plan of this type you can open a portal from planet v to planet u.
- With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
- 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.
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).
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.
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
0 28 12
4 3 1
3 4 1 3 12
2 2 3 4 10
1 2 4 16
0 -1 -1 12
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.
【分析】在一条数轴上,某人初始位置在s点,它有三种功能枪,第一种就是 从u点开一条到v点的即时通道,第二种是开一条从u到[l,r]区间任一点的通道,
第三种是开一条从[l,r]区间任一点到v的通道,所有通道都是单向的即时的,而且一种枪买来只能用一次,用完了还想使用的话就得花钱买,然后问从s点到其他各点
的最小花费。
很明显这是个最短路。问题是从一个点到区间,需要将区间所有的点都建边,这样指定超时。看到区间,于是我们想到线段树。。。对于每个区间我们找到它对应的几个根节点,然后建边,注意,这里要建两棵树分别代表 左端点和右端点,然后就是dij了。
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=6e6+;
const int mod=1e9+;
int n,m,s,cnt,idl[N<<],idr[N<<];
bool vis[N];
LL d[N];
vector<pii>edg[N];
void buildl(int rt,int l,int r)
{
idl[rt]=++cnt;
if(l==r)return ;
int m=l+r>>;
buildl(rt<<,l,m);
buildl(rt<<|,m+,r);
edg[idl[rt<<]].push_back(make_pair(idl[rt],));
edg[idl[rt<<|]].push_back(make_pair(idl[rt],));
}
void buildr(int rt,int l,int r)
{
idr[rt]=++cnt;
if(l==r)return ;
int m=l+r>>;
buildr(rt<<,l,m);
buildr(rt<<|,m+,r);
edg[idr[rt]].push_back(make_pair(idr[rt<<],));
edg[idr[rt]].push_back(make_pair(idr[rt<<|],));
}
void pre(int rt,int l,int r)
{
if(l==r)
{
edg[l].push_back(make_pair(idl[rt],));
edg[idr[rt]].push_back(make_pair(l,));
return;
}
int m=l+r>>;
pre(rt<<,l,m);
pre(rt<<|,m+,r);
}
void addl(int rt,int l,int r,int L,int R,int w){
if(L<=l&&r<=R){
edg[idl[rt]].push_back(make_pair(cnt,w));
return;
}
int mid=(l+r)/;
if(L<=mid)addl(rt*,l,mid,L,R,w);
if(R>mid)addl(rt*+,mid+,r,L,R,w);
}
void addr(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R){
edg[cnt].push_back(make_pair(idr[rt],));
return;
}
int mid=(l+r)/;
if(L<=mid)addr(rt*,l,mid,L,R);
if(R>mid)addr(rt*+,mid+,r,L,R);
}
struct man{
int v;
LL w;
bool operator<(const man &e)const{
return w>e.w;
}
};
priority_queue<man>q;
void dij(int s){
for(int i=;i<=cnt;i++)d[i]=-,vis[i]=;
d[s]=;
q.push(man{s,});
while(!q.empty()){
int u=q.top().v;q.pop();
if(vis[u])continue;
vis[u]=;
for(int i=;i<edg[u].size();++i){
int v=edg[u][i].first,w=edg[u][i].second;
if(!vis[v]&&(d[v]==-||d[v]>d[u]+w)){
d[v]=d[u]+w;
q.push(man{v,d[v]});
}
}
}
}
int main()
{
int w;
scanf("%d%d%d",&n,&m,&s);
cnt=n;
buildl(,,n);
buildr(,,n);
pre(,,n);
while(m--)
{
++cnt;
int op,l,r,x,y;
scanf("%d",&op);
if(op==)
{
scanf("%d%d%d",&x,&y,&w);
addl(,,n,x,x,w);
addr(,,n,y,y);
}
else if(op==)
{
scanf("%d%d%d%d",&x,&l,&r,&w);
addl(,,n,x,x,w);
addr(,,n,l,r);
}
else
{
scanf("%d%d%d%d",&x,&l,&r,&w);
addl(,,n,l,r,w);
addr(,,n,x,x);
}
}
dij(s);
for(int i=;i<=n;i++)printf("%lld%c",d[i],i==n?'\n':' ');
return ;
}
Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)的更多相关文章
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 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 ...
- Codeforces Round #406 (Div. 2) 787-D. Legacy
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)
Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...
- Codeforces Round #271 (Div. 2) E. Pillars 线段树优化dp
E. Pillars time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
随机推荐
- ionic2 手风琴效果
user.ts import { Component } from '@angular/core';import { IonicPage, NavController, NavParams } fro ...
- 使用JMeter进行一次简单的带json数据的post请求测试
使用JMeter进行一次简单的带json数据的post请求测试 原文:https://www.cnblogs.com/summer-mm/p/7717812.html 1.启动jmeter:在bin下 ...
- 妮可妮可妮 [Hash]
妮可妮可妮 题目描述 小P特别喜欢动画Love Live中的角色妮可,每当他听到妮可说"niconiconi"时,他总会感到特别兴奋,还会露出绅士般的微笑. 作为一名理论计算机科学 ...
- webkit在vs2008中编译
转载自:http://xjchilli.blog.163.com/blog/static/4534773920091016115533158/ webkit的官方网站写的webkit需要在vs2005 ...
- MySQL 8.0.11(zip)安装及配置
(1)下载MySQL8.0.11: (2)解压zip文件: 我解压到了D:/MySQL/mysql-8.0.11-winx64 (3)配置环境变量: 右键此电脑->属性 高级系统设置 环境变 ...
- 使用e.target.dataset的问题
在微信开发中我们经常会用到标签中属性的属性值,有时候我们通过 data-* 和 e.target.dateset 来获取属性值会出现一点小bug,即是调用出来的数据是undefined. 1)方案1– ...
- JQuery如何监听DIV内容变化
这几天在做一个微博的接入,需要判断微博是否被关注,要检查微博标签的DIV是否有“已关注”的字符,但这个DIV的内容是微博JSSDK动态生 成.$("#id").html()是获取不 ...
- 动态规划:状压DP
状压DP可以用在NP问题的小规模求解中(不理解,感觉和可以搜索的题很类似) 如果状态是个网格,数据范围很小,基本锁定状压DP 例题是BZOJ1725 题意是这样的,给定一个黑白图,然后种田,要求田与田 ...
- DotNETCore 学习笔记 异常处理
Error Handling public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseIISP ...
- linux基础-临时和永久修改ip地址以及通配符相关
一.临时配置网络(ip,网关,dns) 修改临时ip地址: 1.ifconfig查看当前的网卡和ip地址 2.临时修改IP地址:ifconfig ens32 192.168.16.200/24,ifc ...