zoukankan      html  css  js  c++  java
  • codefores 786B. Legacy(最短路,线段树优化拆点,好题)

    题目链接

    B. Legacy
    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them.

    There are n planets in their universe numbered from 1 to n. Rick is in planet number s (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial.

    By default he can not open any portal by this gun. There are q plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more.

    Plans on the website have three types:

    With a plan of this type you can open a portal from planet v to planet u.
    With a plan of this type you can open a portal from planet v to any planet with index in range [l, r].
    With a plan of this type you can open a portal from any planet with index in range [l, r] to planet v.
    Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet.

    Input
    The first line of input contains three integers n, q and s (1 ≤ n, q ≤ 105, 1 ≤ s ≤ n) — number of planets, number of plans and index of earth respectively.

    The next q lines contain the plans. Each line starts with a number t, type of that plan (1 ≤ t ≤ 3). If t = 1 then it is followed by three integers v, u and w where w is the cost of that plan (1 ≤ v, u ≤ n, 1 ≤ w ≤ 109). Otherwise it is followed by four integers v, l, r and w where w is the cost of that plan (1 ≤ v ≤ n, 1 ≤ l ≤ r ≤ n, 1 ≤ w ≤ 109).

    Output
    In the first and only line of output print n integers separated by spaces. i-th of them should be minimum money to get from earth to i-th planet, or  - 1 if it's impossible to get to that planet.

    Examples
    input
    3 5 1
    2 3 2 3 17
    2 3 2 2 16
    2 2 2 3 3
    3 3 1 1 12
    1 3 3 17
    output
    0 28 12
    input
    4 3 1
    3 4 1 3 12
    2 2 3 4 10
    1 2 4 16
    output
    0 -1 -1 12
    Note
    In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.

    题意:给出 (n) 个点,存在 (q) 条路径,起点为 (s),路径有一下三种:

    1.给出 (u,v,w) ,表示点 (u) 到点 (v) 有一条费用为 (w) 的边.

    2.给出 (v,l,r,w) 表示点 (v) 到区间 ([l,r]) 的点,费用为 (w).

    3.给出 (v,l,r,w) 表示区间 ([l,r]) 的点到点 (v) ,费用为 (w).

    求起点 (s) 到每个点的最小花费。

    题解:其实和这一题思路大体一样,不过,直接对 ([l,r]) 缩成点连接 ([l,r]) 的所有点,那么 (10^5)(q) 可能得连 (10^{10}) 的边,肯定不行,这里用到了一个辅助工具—线段树,建立两颗线段树,一棵是每个结点连出去的出度“大点”,一棵是连向每个点的入度“大点”,对于第一棵,建树时就

    (add(seg[i*2][ty],seg[i][ty],0);)

    (add(seg[i*2+1][ty],seg[i][ty],0);)

    然后对于每个类型2,3,找到对应区间 ([l,r]) 的点进行连接即可。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<set>
    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define fi first
    #define se second
    #define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
    typedef vector<int> VI;
    typedef long long ll;
    typedef pair<int,int> PII;
    const ll inf=1e17;
    const ll mod=1000000007;
    const int maxn=1e5+100;
    int seg[maxn*4][2];
    int tot=0;
    vector<PII> g[maxn*9];
    void add(int u,int v,int w)
    {
        g[u].pb(make_pair(v,w));
    }
    void build(int i,int l,int r,int ty)
    {
        seg[i][ty]=++tot;
        if(l==r)
        {
            if(!ty) add(seg[i][ty],l,0);
            else add(l,seg[i][ty],0);
            return;
        }
        int m=(l+r)/2;
        build(i*2,l,m,ty);
        build(i*2+1,m+1,r,ty);
        if(!ty)
        {
            add(seg[i][ty],seg[i*2][ty],0);
            add(seg[i][ty],seg[i*2+1][ty],0);
        }
        else
        {
            add(seg[i*2][ty],seg[i][ty],0);
            add(seg[i*2+1][ty],seg[i][ty],0);
        }
    }
    
    VI V;
    void update(int i,int l,int r,int L,int R,int ty)
    {
        if(l==L&&r==R)
        {
            V.pb(seg[i][ty]);
            return;
        }
        int m=(L+R)/2;
        if(r<=m) update(i*2,l,r,L,m,ty);
        else if(l>m) update(i*2+1,l,r,m+1,R,ty);
        else
        {
            update(i*2,l,m,L,m,ty);
            update(i*2+1,m+1,r,m+1,R,ty);
        }
    }
    set<pair<ll,int> > st;
    ll d[maxn*10];
    void dij(int s)
    {
        rep(i,1,tot+1) d[i]=inf;
        d[s]=0;
        st.insert(make_pair(0,s));
        while(!st.empty())
        {
            int u=st.begin()->se;
            st.erase(st.begin());
            for(int i=0;i<g[u].size();i++)
            {
                int v=g[u][i].fi;
                if(d[v]>d[u]+1ll*g[u][i].se)
                {
                    st.erase(make_pair(d[v],v));
                    d[v]=d[u]+1ll*g[u][i].se;
                    st.insert(make_pair(d[v],v));
                }
            }
        }
    }
    int main()
    {
        int n,q,s;
        scanf("%d%d%d",&n,&q,&s);
        tot=n;
        build(1,1,n,0);
        build(1,1,n,1);
        while(q--)
        {
            int ty,v,u,w;
            scanf("%d%d",&ty,&v);
            int l,r;
            if(ty==1)
            {
                scanf("%d%d",&u,&w);
                add(v,u,w);
            }
            else
            {
                scanf("%d%d%d",&l,&r,&w);
                V.clear();
                update(1,l,r,1,n,ty-2);
                for(int i=0;i<V.size();i++)
                {
                    if(ty-2)
                        add(V[i],v,w);
                    else
                        add(v,V[i],w);
                }
            }
        }
        dij(s);
        rep(i,1,n+1)
        {
            if(d[i]==inf) d[i]=-1;
            printf("%lld ",d[i]);
        }
        puts("");
        return 0;
    }
    
  • 相关阅读:
    IOS-网络(大文件下载)
    IOS-网络(小文件下载)
    IOS-网络(监听网络状态)
    IOS-网络(数据安全:MD5加密)
    IOS-网络(发送JSON数据给服务器和多值参数)
    IOS-网络(GET请求和POST请求、HTTP通信过程、请求超时、URL转码)
    IOS-网络(JSON解析数据与XML解析数据)
    IOS-网络(HTTP请求、同步请求、异步请求、JSON解析数据)
    IOS-CoreData(增删改查、表关联、分页和模糊查询、多个数据库)
    App6种常见的数据加载设计
  • 原文地址:https://www.cnblogs.com/tarjan/p/7473525.html
Copyright © 2011-2022 走看看