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

然后求起点s到其余点的最短路。

如果直接暴力建图,建图本身就会超时。对于区间上的操作,考虑用线段树解决。线段树上的结点本身就能代表一段区间的点,所以在建图时,用树上的结点充当中间结点。(有点网络流的思想?)

因为要建一张有向图,所以图的点到树上结点要连边,反之亦然;但是在一棵线段树上双向连边就不能对所有的点跑最短路了(因为图中的点可能会在树上找到权值为0的回路回到自己)。

所以再建一棵线段树去表示反向连边的关系。

对每个树上结点从N+1开始编号,将其与之维护的区间中的点连一条花费为0的边。每次更新就是用把给定区间[L,R]囊括的所有树上结点与给定的u连边。操作2和操作3分别对应两棵线段树的结点。

#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define Lson l,m,lson
#define Rson m+1,r,rson
using namespace std;
typedef long long LL;
const int maxn = 4e5+;
const LL INF = (1LL)<<;
struct Edge{
int to,next;
LL w;
}edges[maxn<<];
int head[maxn<<],tot;
int ID[maxn<<],rID[maxn<<];
int id; void init(int N)
{
memset(head,-,sizeof(head));
tot=;
id = N;
} void AddEdge(int u,int v,LL w){
edges[tot] = (Edge){v,head[u],w};
head[u] = tot++;
} void build(int l,int r,int rt,bool flag)
{
if(!flag) ID[rt] = ++id;
else rID[rt] = ++id;
if(!flag) {for(int i=l;i<=r;++i) AddEdge(ID[rt],i,);}
else {for(int i=l;i<=r;++i) AddEdge(i,rID[rt],);}
if(l==r) return;
int m = (l+r)>>;
build(Lson,flag);
build(Rson,flag);
} void update(int L,int R,LL w,int u,int l,int r,int rt,bool flag)
{
if(L<=l && R>=r){
if(!flag)AddEdge(u,ID[rt],w);
else AddEdge(rID[rt],u,w);
return;
}
int m =(l+r)>>;
if(L<=m) update(L,R,w,u,Lson,flag);
if(R>m) update(L,R,w,u,Rson,flag);
} LL d[maxn<<];
bool used[maxn<<];
struct HeapNode{
LL d;
int u;
bool operator <(const HeapNode & rhs) const {return d > rhs.d;}
};
void dijkstra(int s){
memset(used,,sizeof(used));
priority_queue<HeapNode> Q;
for(int i=;i<=id;++i) d[i]=INF;
d[s]=;
Q.push((HeapNode){,s});
while(!Q.empty()){
HeapNode x =Q.top();Q.pop();
int u =x.u;
if(used[u]) continue;
used[u]= true;
for(int i=head[u];~i;i=edges[i].next){
Edge & e = edges[i];
if(d[e.to] > d[u] + e.w){
d[e.to] = d[u] +e.w;
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,q,s,u,v,op,L,R;
LL w;
while(scanf("%d%d%d",&N,&q,&s)==){
init(N);
build(,N,,);
build(,N,,);
while(q--){
scanf("%d",&op);
if(op==){
scanf("%d%d%lld",&u,&v,&w);
AddEdge(u,v,w);
}
else if(op==){
scanf("%d%d%d%lld",&u,&L,&R,&w);
update(L,R,w,u,,N,,);
}
else{
scanf("%d%d%d%lld",&u,&L,&R,&w);
update(L,R,w,u,,N,,);
}
}
dijkstra(s);
for(int i=;i<=N;++i)
printf("%lld%c",d[i]==INF?-:d[i],i==N?'\n':' ');
}
return ;
}

CodeForces - 786B Legacy (线段树+DIjkstra+思维)的更多相关文章

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

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

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

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

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

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

  4. 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 最 ...

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

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

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

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

  7. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

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

    [题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...

  9. Codeforces 786B Legacy(线段树优化建图)

    题目链接  Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...

随机推荐

  1. 路由器port触发与转发---Port Forwarding &amp; Port Triggering

    What is Port Triggering? If you have not read my explanation of port forwarding do so now. You can f ...

  2. Angular2 初识

    AppComponent 壳的三个实现文件: app.component.ts— 组件的类代码,这是用 TypeScript 写的. app.component.html— 组件的模板,这是用 HTM ...

  3. Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授

     本文转载至 http://blog.csdn.net/woaifen3344/article/details/41311023 Code3000极光推送erroryour certificate n ...

  4. Eclipse虚拟内存不足【Eclipse中虚拟内存设置】

    Eclipse最近在做J2EE项目中 发现老是出现虚拟内存不足的提示 前2天去加了根内存 问题同样存在 为了让我在写代码时 不在出现那讨厌的内存不足的提示 也为了 不让那破机器再卡住 今天找到了解决方 ...

  5. bzoj2656

    题目链接:传送门 题目大意:已知 a0=0:a1=1: n为偶数 an=a(n/2):n为基数 an=a(n/2)+a(n/2+1): 题目思路:因为n过大,所以要用java高精度,还有最多20组数据 ...

  6. Asp.Net WebApi核心对象解析

    在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...

  7. [Algorithms] Longest Common Substring

    The Longest Common Substring (LCS) problem is as follows: Given two strings s and t, find the length ...

  8. powershell Start-Sleep

    秒: Start-Sleep –s 10 ,毫秒) Start-Sleep –m 10000 语法 Start-Sleep [-seconds] <int> [<CommonPara ...

  9. 学习js的正确姿势

    轻松入门: http://dwz.cn/6yYLoo 全面理解: http://www.shouce.ren/api/view/a/1116 大师进阶: http://es6.ruanyifeng.c ...

  10. Eclipse打包java工程

    Eclipse打包java工程步骤如下: 1.选择预打包的工程->Export. 2.选择java->JAR file. 3.导出JAR文件设置. 这里有几个选项: Export gene ...