NOIP模拟 path - 按二进制位分组
题目原文:
企鹅豆豆即将要去考长跑了,但是作为一只企鹅,长跑自然比不过鸵鸟和鸡。为了公平起见,教练告诉豆豆,他可以从 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 - 按二进制位分组的更多相关文章
- 2019.8.3 [HZOI]NOIP模拟测试12 C. 分组
2019.8.3 [HZOI]NOIP模拟测试12 C. 分组 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 刚看这题觉得很难,于是数据点分治 k只有1和2两种,分别 ...
- NOIP模拟 17.8.20
NOIP模拟17.8.20 A.阶乘[题目描述]亲爱的xyx同学正在研究数学与阶乘的关系,但是他喜欢颓废,于是他就制作了一个和阶乘有关系的数学游戏:给出两个整数 n,m,令 t = !n,每轮游戏的流 ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
随机推荐
- css的三种表现形式
1.行内样式(内嵌样式):结构的内部,即写在标签内的样式:写在标签的开始部分内部,style属性当中:<标记 style="样式的属性名1:样式的属性值1:属性名2:属性值2:.... ...
- 初学者路径规划 | 人生苦短我用Python
纵观编程趋势 人生苦短,我用Python,比起C语言.C#.C++和JAVA这些编程语言相对容易很多.Python非常适合用来入门.有人预言,Python会成为继C++和Java之后的第三个主流编程语 ...
- session的生命周期是怎样的
session的生命周期是怎样的 一.总结 一句话总结:Tomcat中Session的默认失效时间为20分钟.如果我们敲代码的时候把它设置成1个月,那么这一个月的数据会代替默认20分钟的数据,使ses ...
- (转)iptables详细教程:基础、架构、清空规则、追加规则、应用实例
转自:http://lesca.me/archives/iptables-tutorial-structures-configuratios-examples.html iptables防火墙可以用于 ...
- MWPhotoBrowser 属性详解 和代理解释
--------0.MWPhoto简单属性解释---------------- MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString: ...
- C# AutoMapper
http://www.cnblogs.com/xlhblogs/p/3356748.html
- 将html转换为word文档的几种方式
1 基于wps直接将页面信息下载成word文档 public void test() { WPS.Application wps = null; try { wps = new WPS.Applica ...
- 【习题 3-2 UVA - 1586】Molar mass
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟. 主要是找那个数字. [代码] #include <bits/stdc++.h> using namespace ...
- Python爬虫之路——简单的网页抓图
转载自我自己的博客:http://www.mylonly.com/archives/1401.html 用Python的urllib2库和HTMLParser库写了一个简单的抓图脚本.主要抓的是htt ...
- mjpg-streamer摄像头远程传输UVC
mjpg-streamer摄像头远程传输UVC 1 下载源代码 mjpg-streamer的源代码地址 https://github.com/codewithpassion/mjpg-streame ...