CodeForces - 786B Legacy (线段树+DIjkstra+思维)
题意:给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+思维)的更多相关文章
- Codeforces.786B.Legacy(线段树优化建图 最短路Dijkstra)
题目链接 \(Description\) 有\(n\)个点.你有\(Q\)种项目可以选择(边都是有向边,每次给定\(t,u,v/lr,w\)): t==1,建一条\(u\to v\)的边,花费\(w\ ...
- Codeforces 786B. Legacy 线段树+spfa
题目大意: 给定一个\(n\)的点的图.求\(s\)到所有点的最短路 边的给定方式有三种: \(u \to v\) \(u \to [l,r]\) \([l,r] \to v\) 设\(q\)为给定边 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- 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 最 ...
- Codeforces 787D Legacy 线段树 最短路
题意: 有\(n(1 \leq n \leq 10^5)\)个点,\(q(1 \leq q \leq 10^5)\)条路和起点\(s\) 路有三种类型: 从点\(v\)到点\(u\)需要花费\(w\) ...
- 786B - Legacy(线段树 + 最短路)线段树优化建图
题意: 就是给定一张n nn个点的图,求源点s ss到每个点的单源最短路.这张图共有q组边,连边方式有3种: a→b ,边权为w的单向边:a→[l,r] ,即a到连续区间[l,r]中的每一个点都有一条 ...
- [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
[Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...
- CodeForces 786B Legacy(线段树优化建图+最短路)
[题目链接] http://codeforces.com/problemset/problem/786/B [题目大意] 给出一些星球,现在有一些传送枪,可以从一个星球到另一个星球, 从一个星球到另一 ...
- Codeforces 786B Legacy(线段树优化建图)
题目链接 Legacy 首先对于输入的$n$,建立一棵线段树. 显然线段树有大概$2n$个结点,每个节点对应一段区间 我们把这$2n$个结点加入我们的无向图中,一起跑最短路. 具体连边方案: 我们把 ...
随机推荐
- Photoshop脚本之eps转换成jpg
function saveEPS( doc, saveFile ) { var saveOptions = new JPEGSaveOptions( ); saveOptions.encoding = ...
- 【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3432 题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可. 原因太简单了.... ...
- 【转】CStdioFile UNICODE编译 英文系统下读取中文汉字乱码解决
转载出处:http://www.cnblogs.com/ct0421/p/3242418.html 函数原形为:char *setlocale( int category, const char *l ...
- SurvivalShooter学习笔记(八.敌人管理器)
敌人管理器:管理敌人的随机出生点创建 在场景中建立几个空物体,作为敌人的出生点 public class EnemyManager : MonoBehaviour { public PlayerHea ...
- GCD介绍(四): 完结
转自http://www.dreamingwish.com/dream-2012/gcd-four-the-the-odds-and-ends.html Dispatch Queue挂起 dispat ...
- IOS学习笔记28—SQLite3第三方库之FMDB
本文转载至 http://blog.csdn.net/happyrabbit456/article/details/11609451 SQLite是一种小型的轻量级的关系型数据库,在移动设备上使用是非 ...
- Android Download机制详解(一)DocumentUI部分
在Android中Google为我们集成了一套十分便利的Download机制,用来下载网络上的资源文件.以此省去了我们编写和维护大量与Download相关的代码. 组成 Android中Downloa ...
- 【Python之路】第十八篇--MySQL(一)
一.概述 1.什么是数据库 ? 答:数据的仓库. 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一个软件,都有两个主要的功能: a. ...
- 转!!java中File的delete()方法删除文件失败的原因
一般来说 java file.delete失败 有以下几个原因 1.看看是否被别的进程引用,手工删除试试(删除不了就是被别的进程占用)2.file是文件夹 并且不为空,有别的文件夹或文件, 3.极有可 ...
- Android Studio "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)
1.注释模板效果图 今天在网上看到一段有趣的注释,佛祖保佑 永无bug, 效果如下图所示: 代码如下所示: /** * _ooOoo_ * o8888888o * 88" . "8 ...