码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj4144

时间:2018-01-25 00:26:44      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:std   src   瓶颈   blog   amp   const   har   operator   clu   

最短路+最小生成树

有点忘了...

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

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

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

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

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

技术分享图片
#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

标签:std   src   瓶颈   blog   amp   const   har   operator   clu   

原文地址:https://www.cnblogs.com/19992147orz/p/8343648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!