[USACO08OCT]牧场散步Pasture Walking
Posted 心之所向 素履以往
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO08OCT]牧场散步Pasture Walking相关的知识,希望对你有一定的参考价值。
[USACO08OCT]牧场散步Pasture Walking
题目描述
The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i.
Some pairs of pastures are connected by one of N-1 bidirectional walkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i <= 10,000).
The walkways are set up in such a way that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree.
The cows are very social and wish to visit each other often. Ever in a hurry, they want you to help them schedule their visits by computing the lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures (each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).
POINTS: 200
有N(2<=N<=1000)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方 便,我们假设编号为i的牛恰好在第i号牧场上.
有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路 上行走.第i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度 是 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.
奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只 需要计算出Q(1 < Q < 1000)对点之间的路径长度•每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.
输入输出格式
输入格式:
-
Line 1: Two space-separated integers: N and Q
-
Lines 2..N: Line i+1 contains three space-separated integers: A_i, B_i, and L_i
- Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2
输出格式:
- Lines 1..Q: Line i contains the length of the path between the two pastures in query i.
输入输出样例
4 2 2 1 2 4 3 2 1 4 3 1 2 3 2
2 7
说明
Query 1: The walkway between pastures 1 and 2 has length 2.
Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.
考察对floyd的理解
10000条边 10000次询问
显然我们要尽量做到O(1) 查询
n<=250 显然 考虑 Floyd
n^3 是可以承受的
最短路明显好求
断点k 并不一定是当前的最大点权
这样比较的k就不是路径上除i,j之外的点权最大值
所以要枚举最大点权
但是n^4 代价太大 所以先对 点权排序
为了不让 点权的标号混乱 统一用新的点替换 旧点
做到O(1)枚举 最大点权
k既是断点 也是当前的最大点权
跑一遍floyd就好了
1 #include <queue> 2 #include <cstdio> 3 #include <cctype> 4 #include <cstring> 5 #include <algorithm> 6 7 const int MAXN=500; 8 const int INF=0x3f3f3f3f; 9 10 int n,m,K; 11 12 int a[MAXN],f[MAXN][MAXN],map[MAXN][MAXN]; 13 14 struct node { 15 int val; 16 int id; 17 node() {} 18 node(int val,int id):val(val),id(id) {} 19 friend bool operator < (node a,node b) { 20 return a.val<b.val; 21 } 22 }; 23 node e[MAXN]; 24 25 inline void read(int&x) { 26 int f=1;register char c=getchar(); 27 for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar()); 28 for(;isdigit(c);x=x*10+c-48,c=getchar()); 29 x=x*f; 30 } 31 32 inline int min(int a,int b) {return a<b?a:b;} 33 34 inline int max(int a,int b) {return a<b?b:a;} 35 36 int hh() { 37 // freopen("fee.in","r",stdin); 38 // freopen("fee.out","w",stdout); 39 read(n);read(m);read(K); 40 memset(f,INF,sizeof f); 41 memset(map,INF,sizeof map); 42 for(int i=1;i<=n;++i) read(e[i].val),e[i].id=i; 43 std::sort(e+1,e+1+n); 44 for(int i=1;i<=n;++i) a[e[i].id]=i; 45 for(int x,y,z,i=1;i<=m;++i) { 46 read(x);read(y);read(z); 47 x=a[x];y=a[y]; 48 f[x][y]=min(f[x][y],z); 49 f[y][x]=f[x][y]; 50 } 51 // for(int i=1;i<=n;++i) f[i][i]=0; 52 for(int k=1;k<=n;++k) 53 for(int i=1;i<=n;++i) 54 for(int j=1;j<=n;++j) { 55 f[i][j]=min(f[i][j],f[i][k]+f[k][j]); 56 map[i][j]=min(map[i][j],f[i][j]+max(e[k].val,max(e[i].val,e[j].val))); 57 } 58 for(int S,T,i=1;i<=K;++i) { 59 read(S);read(T); 60 if(map[a[S]][a[T]]==INF) printf("-1\n"); 61 else printf("%d\n",map[a[S]][a[T]]); 62 } 63 return 0; 64 } 65 66 int sb=hh(); 67 int main(int argc,char**argv) {;}
以上是关于[USACO08OCT]牧场散步Pasture Walking的主要内容,如果未能解决你的问题,请参考以下文章
洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
P2912 [USACO08OCT]牧场散步Pasture Walking
洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
洛谷P2912[USACO08OCT]牧场散步Pasture Walking