2069: [POI2004]ZAW

链接

题意:

  给定一张带权图(边是双向的,但不同方向长度不同)。求从1出发,至少经过除1外的一个点,再回到1的最短路。点和边不能重复经过。 n≤5000,m≤10000

分析:

  因为不能重复经过,不能直接最短路的,考虑去掉不能重复经过一个点的限制。

  可以枚举所有与1有边的点,然后从这里面枚举一个点的,求出到其他与1相连的点最短路,来更新。 

  这样显然复杂度是关于1的出度的,复杂度并不是很理想。

  然后这里有一个技巧,按照二进制某一位上的01分成两个集合,然后跑多源最短路。

  为什么可以呢?任何两个不同的点的二进制位最少存在一位不同。  

  复杂度$O(nlog^2n)$

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define pa pair<int,int>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , INF = 1e9;
struct Edge{ int to, nxt, w; } e[];
int head[N], dis[N], df[N], dt[N], A[N], En;
bool vis[N];
priority_queue< pa, vector< pa >, greater< pa > > q; inline void add_edge(int u,int v,int w) {
++En; e[En].to = v, e[En].nxt = head[u], e[En].w = w; head[u] = En;
}
void Dijkstra() {
while (!q.empty()) {
int u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = ;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
q.push(pa(dis[v], v));
}
}
}
}
int main() {
int n = read(), m = read(), cnt = ;
for (int i = ; i <= m; ++i) {
int u = read(), v = read(), w;
if (u == ) {
df[v] = read(), dt[v] = read();
if (!vis[v]) vis[v] = , A[++cnt] = v;
}
else if (v == ) {
dt[u] = read(), df[u] = read();
if (!vis[u]) vis[u] = , A[++cnt] = u;
}
else {
w = read();add_edge(u, v, w);
w = read();add_edge(v, u, w);
}
}
int ans = INF;
for (int k = ; k <= cnt; k <<= ) {
for (int i = ; i <= n; ++i) dis[i] = INF, vis[i] = ;
for (int i = ; i <= cnt; ++i)
if (i & k) q.push(pa(dis[A[i]] = df[A[i]], A[i]));
Dijkstra();
for (int i = ; i <= cnt; ++i)
if (!(i & k)) ans = min(ans, dis[A[i]] + dt[A[i]]); for (int i = ; i <= n; ++i) dis[i] = INF, vis[i] = ;
for (int i = ; i <= cnt; ++i)
if (!(i & k)) q.push(pa(dis[A[i]] = df[A[i]], A[i]));
Dijkstra();
for (int i = ; i <= cnt; ++i)
if (i & k) ans = min(ans, dis[A[i]] + dt[A[i]]);
}
cout << ans;
return ;
}

2069: [POI2004]ZAW的更多相关文章

  1. BZOJ 2069: [POI2004]ZAW(Dijkstra + 二进制拆分)

    题意 给定一个有 \(N\) 个点 \(M\) 条边的无向图, 每条无向边 最多只能经过一次 . 对于边 \((u, v)\) , 从 \(u\) 到 \(v\) 的代价为 \(a\) , 从 \(v ...

  2. 【刷题】BZOJ 2069 [POI2004]ZAW

    Description 在Byte山的山脚下有一个洞穴入口. 这个洞穴由复杂的洞室经过隧道连接构成. 洞穴的入口是一条笔直通向"前面洞口"的道路. 隧道互相都不交叉(他们只在洞室相 ...

  3. BZOJ.2069.[POI2004]ZAW(最短路Dijkstra 按位划分)

    题目链接 \(Description\) 给定一张带权图(边是双向的,但不同方向长度不同).求从1出发,至少经过除1外的一个点,再回到1的最短路.点和边不能重复经过. \(n\leq5000,m\le ...

  4. BZOJ 2069 POI2004 ZAW 堆优化Dijkstra

    题目大意:给定一张无向图.每条边从两个方向走各有一个权值,求从点1往出走至少一步之后回到点1且不经过一条边多次的最短路 显然我们须要从点1出发走到某个和点1相邻的点上,然后沿最短路走到还有一个和点1相 ...

  5. BZOJ2069: [POI2004]ZAW

    2069: [POI2004]ZAW Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 303  Solved: 138[Submit][Status][D ...

  6. bzoj 2096 [POI2004]ZAW——二进制枚举

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2069 可以把直接相连的点分成  从1点出的一部分  和  走向1点的一部分.多起点最短路就和 ...

  7. 【刷题】BZOJ 2407 探险

    Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作人员说明了这次比赛的规则: ...

  8. 做题记录 To 2019.2.13

    2019-01-18 4543: [POI2014]Hotel加强版:长链剖分+树形dp. 3653: 谈笑风生:dfs序+主席树. POJ 3678 Katu Puzzle:2-sat问题,给n个变 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. [翻译] DraggableYoutubeFloatingVideo

    DraggableYoutubeFloatingVideo DraggableYoutubeFloatingVideo allows you to play videos on a floating ...

  2. PHP_CodeSniffer 使用攻略

    安装 PHP_CodeSniffer 安装 phpcs phpcs 是 PHP 代码规范的检测工具. # 下载 $ curl -OL https://squizlabs.github.io/PHP_C ...

  3. Gsoap在QT工程里如何调用

    Qt并没有SOAP的官方实现,都是借助三方库来实现,不过似乎有个QtSoap,不过这个不是太会用,所以还是用Gsoap 这里生成纯C文件, 1.下载gSOAP(http://sourceforge.n ...

  4. 安装配置maven私服-nexus

    1.ubuntu下的Bundle安装方式 1.1. 去官网下载安装包:http://www.sonatype.org/nexus/ 我这里下载的是:nexus-2.8.1-01-bundle.zip, ...

  5. HttpClient的包含注意事项

    HttpClient 功能介绍 以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的主页. 实现了所有 HTTP 的方法(GET,POST,PU ...

  6. NSURLProtocol总结:NSURLProtocol 的本质是对特殊的scechme进行特殊的协议定制

    NSURLProtocol 的本质是对特殊的scechme进行特殊的协议定制: 网络(应用层)请求的统一入口是nsurlconnection和nsurlsession; http.htp.mail等协 ...

  7. ResultJsonInfo<T>

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace QY.We ...

  8. virtualbox+vagrant学习-2(command cli)-12-vagrant Provision命令

    Provision 格式: vagrant provision [vm-name] [--provision-with x,y,z] 针对正在运行的vagrant托管计算机运行任何配置预配置程序. u ...

  9. Vmware10组建局域网

    Vmware10组建局域网很简单,特别是用Ubuntu16.04作为操作系统,基本上按照如下步骤来,是不会出现问题的. 1.首先,启动虚拟机,选择“编辑”->“虚拟网络编辑器” 2.改为桥接模式 ...

  10. 集合之HashSet

    在前篇博文中(java提高篇(二三)-----HashMap)详细讲解了HashMap的实现过程,对于HashSet而言,它是基于HashMap来实现的,底层采用HashMap来保存元素.所以如果对H ...