题目原文:

企鹅豆豆即将要去考长跑了,但是作为一只企鹅,长跑自然比不过鸵鸟和鸡。为了公平起见,教练告诉豆豆,他可以从 K 个指定地点中选择两个不同的地点分别作为起点和终点来考试.考试地图是一个由 N 个点 M 条边组成的没有重边和自环的连通无向图,一条边的长度为 Ai 。豆豆想知道他的长跑考试最少需要跑多远。

【数据范围】

对于 30% 的数据,K≤4;

对于另外 10% 的数据,K=N;

对于另外 30% 的数据,M=N-1;

对于 100% 的数据,1≤N,M≤100000;T≤5;1≤K≤n;1≤边长≤100000。

题目分析:

就是求图中最近的两个特殊点之间的距离。

  • 乱搞版:对每个特殊点进行一遍dijkstra,一遇到其他特殊点就break,更新答案。数据没有刻意去卡,加上部分分的优化勉强能卡掉。
  • 高大上的正解: 将k个点编号1...k,枚举编号的二进制的每一位,将这一位为1的点连边S(作为起点),为0的点连边T(作为终点),这样跑\(log(k)\)次dijkstra即可得到答案。总复杂度\(O(nlog(n)log(k))\),还是尽量加上部分分的优化。

code

乱搞版:

#include<bits/stdc++.h>
using namespace std; typedef long long ll; namespace IO{
inline int readint(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline ll readll(){
ll i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wrint(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wrint(x / 10);
putchar(x % 10 + '0');
}
inline void wrll(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wrll(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; const int N = 1e5 + 5, M = 1e5 + 5, OO = 0x3f3f3f3f;
int n, m, k, ecnt, adj[N], go[M << 1], nxt[M << 1], T;
vector<int> keyPoint;
typedef pair<ll, int> P;
priority_queue<P, vector<P>, greater<P> > que;
bool key[N];
ll len[M << 1], dis[N], minn; inline void addEdge(int u, int v, ll w){ nxt[++ecnt] = adj[u], adj[u] = ecnt, go[ecnt] = v, len[ecnt] = w;} inline void solve(){
ll ret = OO;
for(int i = 0; i < k; i++){
int u = keyPoint[i];
memset(dis, OO, sizeof dis), dis[u] = 0;
while(!que.empty()) que.pop(); que.push(P(0, u));
while(!que.empty()){
P t = que.top(); que.pop();
if(t.second != u && key[t.second]){
ret = min(ret, t.first);
break;
}
for(int e = adj[t.second]; e; e = nxt[e]){
int v = go[e];
if(dis[v] > t.first + len[e]){
dis[v] = t.first + len[e];
que.push(P(dis[v], v));
}
}
}
}
wrll(ret), putchar('\n');
} int main(){
freopen("h.in", "r", stdin);
T = readint();
while(T--){
n = readint(), m = readint();
memset(adj, 0, sizeof adj), ecnt = 0, keyPoint.clear(), memset(key, 0, sizeof key), minn = OO;
for(int i = 1; i <= m; i++){
int u = readint(), v = readint();
ll w = readll(); minn = min(minn, w);
addEdge(u, v, w), addEdge(v, u, w);
}
k = readint();
for(int i = 1; i <= k; i++){ int x; keyPoint.push_back(x = readint()), key[x] = true;}
if(k == n){ wrll(minn), putchar('\n');}
else solve();
}
return 0;
}

正解:

#include<bits/stdc++.h>
using namespace std; typedef long long ll; namespace IO{
inline int readint(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline ll readll(){
ll i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wrint(int x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wrint(x / 10);
putchar(x % 10 + '0');
}
inline void wrll(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wrll(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; const int N = 1e5 + 5, M = 1e5 + 5, OO = 0x3f3f3f3f;
int n, m, k, ecnt, adj[N], go[M << 1], nxt[M << 1], T;
vector<int> keyPoint;
typedef pair<ll, int> P;
priority_queue<P, vector<P>, greater<P> > que;
ll len[M << 1], dis[N], num[N], minn; inline void addEdge(int u, int v, ll w){ nxt[++ecnt] = adj[u], adj[u] = ecnt, go[ecnt] = v, len[ecnt] = w;} inline void solve(){
ll ret = OO;
for(int i = 0; (1 << i) <= k; i++){
memset(dis, OO, sizeof dis);
while(!que.empty()) que.pop();
for(int j = 0; j < keyPoint.size(); j++)
if((1 << i) & num[keyPoint[j]]) dis[keyPoint[j]] = 0, que.push(P(0, keyPoint[j]));
while(!que.empty()){
P t = que.top(); que.pop();
if(num[t.second] && !((1 << i) & num[t.second])){ ret = min(ret, t.first); break; }
for(int e = adj[t.second], v; e; e = nxt[e]){
if(dis[v = go[e]] > t.first + len[e]){
dis[v] = t.first + len[e];
que.push(P(dis[v], v));
}
}
}
}
wrll(ret), putchar('\n');
} int main(){
freopen("h.in", "r", stdin);
T = readint();
while(T--){
n = readint(), m = readint();
memset(adj, 0, sizeof adj), ecnt = 0, keyPoint.clear(), memset(num, 0, sizeof num), minn = OO;
for(int i = 1; i <= m; i++){
int u = readint(), v = readint();
ll w = readll(); minn = min(minn, w);
addEdge(u, v, w), addEdge(v, u, w);
}
k = readint();
for(int i = 1; i <= k; i++){ int x; keyPoint.push_back(x = readint()), num[x] = i;}
if(k == n) { wrll(minn), putchar('\n');}
else solve();
}
return 0;
}

NOIP模拟 path - 按二进制位分组的更多相关文章

  1. 2019.8.3 [HZOI]NOIP模拟测试12 C. 分组

    2019.8.3 [HZOI]NOIP模拟测试12 C. 分组 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 刚看这题觉得很难,于是数据点分治 k只有1和2两种,分别 ...

  2. NOIP模拟 17.8.20

    NOIP模拟17.8.20 A.阶乘[题目描述]亲爱的xyx同学正在研究数学与阶乘的关系,但是他喜欢颓废,于是他就制作了一个和阶乘有关系的数学游戏:给出两个整数 n,m,令 t = !n,每轮游戏的流 ...

  3. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  4. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  5. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  6. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  7. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  8. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  9. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

随机推荐

  1. loadrunner监控apache服务

    一.apache配置步骤(假设apache服务已安装) 1.使用find / -name httpd.conf命令查找httpd.conf文件 2.使用cd opt/lampp/apache2/con ...

  2. JPA的配置文件persistence.xml参数详解

    <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0&q ...

  3. CodeVs——T 4919 线段树练习4

    http://codevs.cn/problem/4919/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  4. Fragment Summary 1/2

    转自:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment ...

  5. Altium Designer如何调整鼠标形状

    在 里面有一个

  6. (转)oracle表空间使用率统计查询

    转自:http://www.cnblogs.com/xwdreamer/p/3511047.html 参考文献 文献1:http://blog.itpub.net/24104518/viewspace ...

  7. Docker---(1)Docker 简介

    原文:Docker---(1)Docker 简介 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/weixin_39 ...

  8. 全面详细介绍一个P2P网贷领域的ERP系统的主要功能

        一般的P2P系统,至少包括PC网站的前端和后端.前端系统的功能,可以参考"P2P系统哪家强,功能其实都一样" http://blog.csdn.net/fansunion/ ...

  9. 一些mysql innodb的建议

    http://blog.csdn.net/yunhua_lee/article/details/8239145 原文:http://dev.mysql.com/doc/refman/5.5/en/in ...

  10. 算法求解中的变量、数组与数据结构(STL 中的容器)

    本质上算法都是对数据的操作,没有数据,没有存储数据的容器和组织方式,算法就是无源之水无本之木,就是巧妇也难为无米之炊.算法是演员,变量.数组.容器等就是舞台, 然后整个算法的处理流程,都是针对这些数据 ...