NOIP 货车运输

题目描述 Description

A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。

输入描述 Input Description

第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。
接下来 m 行每行 3 个整数 x、y、z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意:x 不等于 y,两座城市之间可能有多条道路。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意:x 不等于 y。

输出描述 Output Description

输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。

样例输入 Sample Input

4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3

样例输出 Sample Output

3
-1
3

数据范围及提示 Data Size & Hint

对于 30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q < 1,000;
对于 60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q < 1,000;
对于 100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q < 30,000,0 ≤ z ≤ 100,000。

 题解

最大生成树+LCA

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<cstring>
  7 #include<vector>
  8 #include<queue>
  9 using namespace std;
 10 const int maxn=10005;
 11 const int maxm=50005;
 12 int N,M,Q;
 13 vector<int> to[maxn],cost[maxn];
 14 int p[maxn][50],next[maxn][50];
 15 int dep[maxn];
 16 int fa[maxn];
 17 struct node{
 18     int uu,vv,cc;
 19 }a[maxm];
 20 int cmp(const node&q,const node&w){
 21     if(q.cc>w.cc) return 1;
 22     return 0;
 23 }
 24 inline int get_fa(int x){
 25     if(x!=fa[x]) fa[x]=get_fa(fa[x]);
 26     return fa[x];
 27 }
 28 inline void dfs(int x){
 29     for(int i=0;i<to[x].size();i++){
 30         int y=to[x][i];
 31         if(y!=p[x][0]){
 32             dep[y]=dep[x]+1;
 33             p[y][0]=x;
 34             next[y][0]=cost[x][i];
 35             for(int k=1;k<=30;k++){
 36                 int zu=1<<k;
 37                 if(zu<=dep[y]){
 38                     p[y][k]=p[p[y][k-1]][k-1];
 39                     next[y][k]=min(next[y][k-1],next[p[y][k-1]][k-1]);
 40                 }
 41                 else break;
 42             }        
 43             dfs(y);
 44         }
 45     }
 46 }
 47 inline int LCA(int x,int y){
 48     int ANS=1e9;
 49     if(dep[x]>dep[y]) swap(x,y);
 50     int delta=dep[y]-dep[x];
 51     for(int i=0;i<=30;i++){
 52         int h=1<<i; h=h&delta;//这步很关键,如果直接用 if((1<<i)&delta!=0) 会出错 
 53         if(h!=0){
 54             ANS=min(ANS,next[y][i]);
 55             y=p[y][i];
 56         }
 57     }
 58     if(x==y) return ANS;
 59     for(int i=30;i>=0;i--){
 60         if(p[x][i]!=p[y][i]){
 61             ANS=min(ANS,next[x][i]); ANS=min(ANS,next[y][i]);
 62             x=p[x][i]; y=p[y][i];
 63         }
 64     }
 65     ANS=min(ANS,next[y][0]); ANS=min(ANS,next[x][0]);
 66     return ANS;
 67 }
 68 int main(){
 69 //    freopen("truck.in","r",stdin);
 70 //    freopen("truck.out","w",stdout);
 71     scanf("%d%d",&N,&M);
 72     for(int i=1;i<=N;i++) fa[i]=i;
 73     for(int i=1;i<=M;i++){
 74         int u,v,c;
 75         scanf("%d%d%d",&u,&v,&c);
 76         a[i].uu=u; a[i].vv=v; a[i].cc=c;
 77     }
 78     sort(a+1,a+M+1,cmp);
 79     for(int i=1;i<=M;i++){
 80         int u=a[i].uu; int v=a[i].vv; int c=a[i].cc;
 81         int fau=get_fa(u); int fav=get_fa(v);
 82         if(fau!=fav){
 83             if(fau<fav) fa[fav]=fau;
 84             else fa[fau]=fav;
 85             to[u].push_back(v); to[v].push_back(u);
 86             cost[u].push_back(c); cost[v].push_back(c);
 87         }
 88     }
 89     p[1][0]=-1; dep[1]=0;
 90     dfs(1);
 91     scanf("%d",&Q);
 92     for(int i=1;i<=Q;i++){
 93         int u,v;
 94         scanf("%d%d",&u,&v);
 95         int now=LCA(u,v);
 96         if(now==0) now=-1;
 97         cout<<now<<endl;
 98     }
 99     return 0;
100 }
原文地址:https://www.cnblogs.com/CXCXCXC/p/4938461.html