NOIP 货车运输
題目描述?Description
A 國有 n 座城市,編號從 1 到 n,城市之間有 m 條雙向道路。每一條道路對車輛都有重量限制,簡稱限重?,F在有 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δ//這步很關鍵,如果直接用 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
總結
- 上一篇: struts2.1.6教程二、strut
- 下一篇: 优化android studio编译的a