uva 12655 Trucks [LCA](樹鏈剖分+MST)

來源:互聯網
上載者:User

The Subtle Balloons Company (SBC) is the main balloon provider for programming contests; it has
huge factories and warehouses, as well as an extensive truck fleet to ensure the contestants’ happiness.
There are lots of competition sites in Nlogonia, and all of them hired SBC for supplying balloons for
their contests. Nlogonia is an archipelago connected by several bridges. Every island of Nlogonia may
have several regional sites and may also house several SBC warehouses.
When planning the routes for balloon deliveries, SBC faced a problem: for safety issues, every
bridge in Nlogonia has some maximum weight limit for vehicles which cross it. And because of the
great net weight of the transported merchandise, SBC operations’ chief asked you to write a program to
determine the maximum weight allowed to be transported between warehouses and competition sites.
Input
The input contains several test cases. The first line of a test case contains three integers N, M and S
which indicate, respectively, the number of islands, the number of bridges that connect the islands and
the number of sites. The islands are numbered from 1 to N.
Each of the next M lines describes a bridge. The description of a bridge consists in a line with
three integers A, B and W, indicating respectively the two islands connected by the bridge and the
maximum weight allowed in that bridge, in tons.
All bridges are two-way roads; every pair of islands is connected by at most one bridge; and it is
possible to reach every other island in the archipelago using only bridges (naturally it may be needed
to pass through other islands to do so).
Each of the next S lines describe a competition site and contains two integers L and H indicating,
respectively, the number of the island where this site is and the number of the island where the
wharehouse which will be used to deliver the balloons to the site is.
Output
For each site in a test case, in the order they were given, your program must produce a single line,
containing a single integer, the biggest weight which can be transported by truck from the warehouse
to the site.
Restrictions
• 2 ≤ N ≤ 2 × 104
• 1 ≤ M ≤ 105
• 1 ≤ S ≤ 5 × 104
• 1 ≤ A, B, L, H ≤ N, A ̸= B, L ̸= H
• 0 ≤ W ≤ 105
Sample Input
4 5 4
1 2 9
1 3 0
2 3 8
2 4 7
3 4 4
1 4
2 1
3 1
4 3
4 5 2
1 2 30
2 3 20
3 4 10
4 1 40
2 4 50
1 3
1 2
Sample Output
7
9
8
7
20
40

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=2e5+9;struct edge{    int u,v,w;} e[maxn],ee[maxn];int cmp(edge a,edge b){    return a.w>b.w;}int n,m,s,a,b;int sz[maxn],fa[maxn],tp[maxn],id[maxn],dep[maxn],son[maxn],idx,v[maxn];int val[maxn<<2],pre[maxn],eused=0;vector<int> mp[maxn];int ff(int x){    return x==pre[x]?x:pre[x]=ff(pre[x]);}void MST(){    sort(e+1,e+m+1,cmp);    for(int i=1; i<=m; i++)    {        int u=ff(e[i].u);        int v=ff(e[i].v);        if(u!=v)        {            pre[u]=v;            ee[++eused]=e[i];            mp[e[i].u].push_back(e[i].v);            mp[e[i].v].push_back(e[i].u);         //   printf("------%d %d\n",e[i].u,e[i].v);        }    }}void dfs1(int u,int f,int d){    sz[u]=1;    fa[u]=f;    dep[u]=d;    son[u]=0;    for(int i=0; i<mp[u].size(); i++)    {        int v=mp[u][i];        if(v==fa[u]) continue;        dfs1(v,u,d+1);        sz[u]+=sz[v];        if(son[u]==0||sz[son[u]]<sz[v])            son[u]=v;    }}void dfs2(int u,int tpp){    tp[u]=tpp;    id[u]=++idx;    if(son[u]) dfs2(son[u],tpp);    for(int i=0; i<mp[u].size(); i++)    {        int v=mp[u][i];        if(v==fa[u]||v==son[u])            continue;        dfs2(v,v);    }}void pushup(int rt){    val[rt]=min(val[rt<<1],val[rt<<1|1]);}void build(int l,int r,int rt){    if(l==r)    {        val[rt]=v[l];        return;    }    int m=l+r>>1;    build(lson);    build(rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)        return val[rt];    int m=l+r>>1;    int ans=0x3f3f3f3f;    if(L<=m) ans=min(ans,query(L,R,lson));    if(m<R) ans=min(ans,query(L,R,rson));    return ans;}int qq(int u,int v){    //  printf("---%d %d\n",u,v);    int t1=tp[u],t2=tp[v];    int ans=0x3f3f3f3f;    while(t1!=t2)    {        if(dep[t1]<dep[t2])        {            swap(u,v);            swap(t1,t2);        }        ans=min(ans,query(id[t1],id[u],1,idx,1));        u=fa[t1];        t1=tp[u];    }    if(u!=v)    {        if(dep[u]>dep[v]) swap(u,v);        ans=min(ans,query(id[son[u]],id[v],1,idx,1));    }    return ans;}int main(){    while(~scanf("%d%d%d",&n,&m,&s))    {        idx=eused=0;        for(int i=1; i<=n; i++)        {            pre[i]=i;            mp[i].clear();        }        for(int i=1; i<=m; i++)            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);        MST();        dfs1(1,0,1);        dfs2(1,1);        for(int i=1; i<=eused; i++)        {            if(dep[ee[i].u]<dep[ee[i].v]) swap(ee[i].u,ee[i].v);            v[id[ee[i].u]]=ee[i].w;        }        build(1,idx,1);        while(s--)        {            scanf("%d%d",&a,&b);            printf("%d\n",qq(a,b));        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.