bzoj4144

Posted 123456

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj4144相关的知识,希望对你有一定的参考价值。

最短路+最小生成树

因为所有东西都只跟加油站有关,那么我们只留加油站就行了。

先预处理出每个点到加油站的最短距离,这个用多源最短路,就是把所有加油站放到堆里,然后就是重构图,我们不用直接连边,计算出每条边的贡献,然后跑最小生成树,这里直接离线查询就行了。

技术分享图片
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
namespace IO 
{
    const int Maxlen = N * 50;
    char buf[Maxlen], *C = buf;
    int Len;
    inline void read_in()
    {
        Len = fread(C, 1, Maxlen, stdin);
        buf[Len] = \0;
    }
    inline void fread(int &x) 
    {
        x = 0;
        int f = 1;
        while (*C < 0 || 9 < *C) { if(*C == -) f = -1; ++C; }
        while (0 <= *C && *C <= 9) x = (x << 1) + (x << 3) + *C - 0, ++C;
        x *= f;
    }
    inline void fread(long long &x) 
    {
        x = 0;
        long long f = 1;
        while (*C < 0 || 9 < *C) { if(*C == -) f = -1; ++C; }
        while (0 <= *C && *C <= 9) x = (x << 1) + (x << 3) + *C - 0, ++C;
        x *= f;
    }
    inline void read(int &x)
    {
        x = 0;
        int f = 1; char c = getchar();
        while(c < 0 || c > 9) { if(c == -) f = -1; c = getchar(); }
        while(c >= 0 && c <= 9) { x = (x << 1) + (x << 3) + c - 0; c = getchar(); }
        x *= f;
    }
    inline void read(long long &x)
    {
        x = 0;
        long long f = 1; char c = getchar();
        while(c < 0 || c > 9) { if(c == -) f = -1; c = getchar(); }
        while(c >= 0 && c <= 9) { x = (x << 1ll) + (x << 3ll) + c - 0; c = getchar(); }
        x *= f;
    } 
} using namespace IO;
int rd()
{
    int x = 0, f = 1; char c = getchar();
    while(c < 0 || c > 9) { if(c == -) f = -1; c = getchar(); }
    while(c >= 0 && c <= 9) { x = x * 10 + c - 0; c = getchar(); }
    return x * f;
} 
int n, m, Q, cnt = 1, s;
int head[N], ans[N], c[N], fa[N], d[N], rank[N];
struct edge {
    int nxt, to, w;
} e[N << 1];
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 = 1; i <= s; ++i) 
    {
        fread(c[i]);
        d[c[i]] = 0;
        q.push(make_pair(0, c[i]));
    }
    for(int i = 1; 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 = 1; i <= Q; ++i) 
    {
        fread(que[i].u);
        fread(que[i].v);
        fread(que[i].b);
        que[i].id = i;
    }
    sort(que + 1, que + Q + 1);
    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 = 1; i <= m; ++i) E[i].w += d[E[i].u] + d[E[i].v];
    sort(E + 1, E + m + 1);
    for(int i = 1; i <= n; ++i) fa[i] = i;
    for(int i = 1, j = 0; i <= Q; ++i) 
    {
        while(j + 1 <= m && E[j + 1].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 = 1; i <= Q; ++i) puts(ans[i] ? "TAK" : "NIE");
    return 0;
}
View Code

 

以上是关于bzoj4144的主要内容,如果未能解决你的问题,请参考以下文章

bzoj4144

bzoj4144 [AMPPZ2014]Petrol 图论 最短路 并查集

bzoj4144 [AMPPZ2014]Petrol

BZOJ4144[AMPPZ2014]Petrol 最短路+离线+最小生成树

bzoj4144 [AMPPZ2014]Petrol

P4144 大河的序列(贪心)