最短路+最小生成树

有点忘了...

这题只要判断能不能就行了

具体做法是把所有加油站放到堆里然后跑dij,然后把边权w=d[u]+d[v]+w,跑最小生成树

对于点对(x,y)是否能到达只要判断最大瓶颈路的长度是否>b就行了

具体原因是因为对于一条边(u,v),边权表示了经过这条边的最小油量。

然后最小生成树一边跑,一边离线就行了

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + ;
namespace IO
{
const int Maxlen = N * ;
char buf[Maxlen], *C = buf;
int Len;
inline void read_in()
{
Len = fread(C, , Maxlen, stdin);
buf[Len] = '\0';
}
inline void fread(int &x)
{
x = ;
int f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void fread(long long &x)
{
x = ;
long long f = ;
while (*C < '' || '' < *C) { if(*C == '-') f = -; ++C; }
while ('' <= *C && *C <= '') x = (x << ) + (x << ) + *C - '', ++C;
x *= f;
}
inline void read(int &x)
{
x = ;
int f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << ) + (x << ) + c - ''; c = getchar(); }
x *= f;
}
inline void read(long long &x)
{
x = ;
long long f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = (x << 1ll) + (x << 3ll) + c - ''; c = getchar(); }
x *= f;
}
} using namespace IO;
int rd()
{
int x = , f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = x * + c - ''; c = getchar(); }
return x * f;
}
int n, m, Q, cnt = , s;
int head[N], ans[N], c[N], fa[N], d[N], rank[N];
struct edge {
int nxt, to, w;
} e[N << ];
struct Edge {
int u, v, w;
Edge() {}
Edge(int u, int v, int w) : u(u), v(v), w(w) {}
bool friend operator < (const Edge &a, const Edge &b) {
return a.w < b.w;
}
} E[N];
struct Query {
int u, v, id, b;
bool friend operator < (const Query &a, const Query &b) {
return a.b < b.b;
}
} que[N];
void link(int u, int v, int w)
{
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].to = v;
e[cnt].w = w;
}
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main()
{
read_in();
fread(n);
fread(s);
fread(m);
memset(d, 0x7f7f, sizeof(d));
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
for(int i = ; i <= s; ++i)
{
fread(c[i]);
d[c[i]] = ;
q.push(make_pair(, c[i]));
}
for(int i = ; i <= m; ++i)
{
int u, v, w;
fread(u);
fread(v);
fread(w);
E[i] = Edge(u, v, w);
link(u, v, w);
link(v, u, w);
}
fread(Q);
for(int i = ; i <= Q; ++i)
{
fread(que[i].u);
fread(que[i].v);
fread(que[i].b);
que[i].id = i;
}
sort(que + , que + Q + );
while(!q.empty())
{
pair<int, int> o = q.top();
q.pop();
int u = o.second;
if(o.first > d[u]) continue;
for(int i = head[u]; i; i = e[i].nxt) if(d[e[i].to] > d[u] + e[i].w)
{
d[e[i].to] = d[u] + e[i].w;
q.push(make_pair(d[e[i].to], e[i].to));
}
}
for(int i = ; i <= m; ++i) E[i].w += d[E[i].u] + d[E[i].v];
sort(E + , E + m + );
for(int i = ; i <= n; ++i) fa[i] = i;
for(int i = , j = ; i <= Q; ++i)
{
while(j + <= m && E[j + ].w <= que[i].b)
{
++j;
int u = find(E[j].u), v = find(E[j].v);
if(u == v) continue;
if(rank[u] < rank[v]) swap(u, v);
rank[u] += rank[v];
fa[u] = v;
}
ans[que[i].id] = find(que[i].u) == find(que[i].v);
}
for(int i = ; i <= Q; ++i) puts(ans[i] ? "TAK" : "NIE");
return ;
}

bzoj4144的更多相关文章

  1. 【BZOJ4144】[AMPPZ2014]Petrol 最短路+离线+最小生成树

    [BZOJ4144][AMPPZ2014]Petrol Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油 ...

  2. bzoj4144【AMPPZ2014】Petrol

    题解:  首先注意到起点和终点都是加油站;          假设中途经过某个非加油站的点u,u连到v,离u最近的加油站是x,那么从u到x加油后回到u,再到v一定不比直接从u到v差:        因 ...

  3. BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】

    题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...

  4. [BZOJ4144][AMPPZ2014]Petrol[多源最短路+MST]

    题意 题目链接 分析 假设在 \(a \rightarrow b\) 的最短路径中出现了一个点 \(x\) 满足到 \(x\) 最近的点是 \(c\) ,那么我们完全可以从 \(a\) 直接走到 \( ...

  5. BZOJ4144: [AMPPZ2014]Petrol(最短路 最小生成树)

    题意 题目链接 Sol 做的时候忘记写题解了 可以参考这位大爷 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  6. bzoj4144 [AMPPZ2014]Petrol

    link 题意: 给一个n个点m条边的带权无向图,其中k个点是加油站,每个加油站可以加满油,但不能超过车的油量上限.有q个询问,每次给出x,y,b,保证x,y都是加油站,问一辆油量上限为b的车从x出发 ...

  7. 【BZOJ4144】[AMPPZ2014]Petrol(最短路+最小生成树+并查集)

    Description 给定一个n个点.m条边的带权无向图,其中有s个点是加油站. 每辆车都有一个油量上限b,即每次行走距离不能超过b,但在加油站可以补满. q次询问,每次给出x,y,b,表示出发点是 ...

  8. [题解] [BZOJ4144] 「AMPPZ2014」Petrol

    题面 怎么是权限题啊 题解 有一次考过, 但是不记得了 如果每个点都是加油站的话, 这道题就是货车运输 考虑如何转化 我们可以设

  9. 题解 [BZOJ4144] Petrol

    题目描述 ​ 有一张 n 个点 m 条边的无向图,其中有 s 个点上有加油站.有 Q 次询问(a,b,c), 问能否开一辆油箱容积为 c 的车从 a 走到 b.(a,b均为加油站) 输入格式 ​ 第一 ...

随机推荐

  1. Unity3D游戏开发之简单的碰撞检測

    在"Project"面板中单击"Create"旁边的小三角,选择"javascript"创建一个名为"collision" ...

  2. 在eclipse中查找指定文件 [多种方法]

    在eclipse中查找指定文件   1.ctrl+h打开搜索界面 File Search: containing text填*,File name patterns填写hello.*,可以找到hell ...

  3. Vuex demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. SegmentFault 巨献 1024 程序猿游戏「红岸的呼唤」第二天任务攻略

    眼看实验室就要关门了.走之前写一下解题过程(事实上大家都等着第三题出来吧大概-=). 高速传送门:http://segmentfault.com/game/2 那么接昨天的博客,今天的题目是这种: 完 ...

  5. VC ++6.0英文版常用菜单使用参考【转载整理】

    (1)File菜单 New:打开“new”对话框,以便创建新的文件.工程或工作区. Close Workspace:关闭与工作区相关的所有窗口. Exit:退出VC6环境,将提示保存窗口内容等. (2 ...

  6. 【BZOJ3162】独钓寒江雪 树同构+DP

    [BZOJ3162]独钓寒江雪 题解:先进行树hash,方法是找重心,如果重心有两个,则新建一个虚点将两个重心连起来,新点即为新树的重心.将重心当做根进行hash,hash函数不能太简单,我的方法是: ...

  7. EasyDarwin流媒体服务器高性能优化方向

    我们在EasyDarwin开源流媒体服务器上做了很多的优化,包括前面说到的<EasyDarwin开源流媒体服务器将select改为epoll的方法>.<EasyDarwin开源流媒体 ...

  8. keybd_event、SendInput笔记

    void keybd_event(BYTE bVk, BYTE bScan, DWORD dwFlags, ULONG_PTR dwExtraInfo); bVk:虚拟键码 bScan:键的硬件扫描码 ...

  9. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(1)

    1. 所先看一下官方地址,了解一下这个是不是你想要的测试工具 https://macacajs.github.io/macaca/environment-setup.html 2. 去掉sudo -- ...

  10. PAT 天梯赛 L1-050. 倒数第N个字符串 【字符串】

    题目链接 https://www.patest.cn/contests/gplt/L1-050 思路 因为是求倒数 我们不如直接 倒过来看 令 zzz 为第一个字符串 我们可以理解为 十进制 转换为 ...