您的位置:首页 > 其它

POJ1986(LCA应用:求两结点之间距离)

2016-01-30 23:35 253 查看
Distance Queries
Time Limit: 2000MSMemory Limit: 30000K
Total Submissions: 11304Accepted: 3985
Case Time Limit: 1000MS
DescriptionFarmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input* Lines 1..1+M: Same format as "Navigation Nightmare"* Line 2+M: A single integer, K. 1 <= K <= 10,000* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output
13
3
36
离线:将所有查询输入完毕后再统一输出结果。
在线:查询一个输出一个。

dfs+并查集,离线
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
typedef pair<int,int> P;
const int MAXN=100005;
int V,E;
vector<P> G[MAXN];
vector<P> que[MAXN];
int par[MAXN];
int fnd(int x)
{
if(par[x]==x)
return x;
return par[x]=fnd(par[x]);
}
int vis[MAXN];
int ans[MAXN];
int d[MAXN];

void dfs(int u,int fa)
{
par[u]=u;
for(int i=0;i<que[u].size();i++)
{
P no=que[u][i];
int v=no.first;
if(vis[v])    ans[no.second]=d[u]+d[v]-2*d[fnd(v)];
}
vis[u]=1;
for(int i=0;i<G[u].size();i++)
{
P now=G[u][i];
if(now.first==fa)    continue;
d[now.first]=d[u]+now.second;
dfs(now.first,u);
par[now.first]=u;
}
}

int main()
{
while(scanf("%d%d",&V,&E)!=EOF&&V)
{
for(int i=1;i<=V;i++)
{
G[i].clear();
que[i].clear();
}
memset(vis,0,sizeof(vis));
memset(ans,0,sizeof(ans));
memset(d,0,sizeof(d));
for(int i=0;i<E;i++)
{
int u,v,cost;
scanf("%d %d %d %*c",&u,&v,&cost);
G[u].push_back(P(v,cost));
G[v].push_back(P(u,cost));
}
int Q;
scanf("%d",&Q);
for(int i=1;i<=Q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
que[u].push_back(P(v,i));
que[v].push_back(P(u,i));
}
dfs(1,-1);
for(int i=1;i<=Q;i++)
{
printf("%d\n",ans[i]);
}
}

return 0;
}
模板:RMQ求LCA在线算法(稀疏表实现RMQ)
#include"cstdio"
#include"cstring"
#include"vector"
#include"cmath"
#include"algorithm"
using namespace std;
const int MAXN=50005;
typedef pair<int,int> P;
vector<P> G[MAXN];
int V,E;
int depth[MAXN*2];
int vs[MAXN];
int pos[MAXN];
int d[MAXN];
int cnt;
int dep;
void dfs(int u,int fa)
{
int cdep=++dep;
depth[++cnt]=cdep;
vs[cdep]=u;
pos[u]=cnt;
for(int i=0;i<G[u].size();i++)
{
P now=G[u][i];
if(now.first==fa)    continue;
d[now.first]=d[u]+now.second;
dfs(now.first,u);
depth[++cnt]=cdep;
}
}

int dp[MAXN*2][20];
void init_rmq(int n)
{
for(int i=1;i<=n;i++)    dp[i][0]=depth[i];
int m=floor(log(n*1.0)/log(2.0));
for(int j=1;j<=m;j++)
for(int i=1;i<=n-(1<<j)+1;i++)
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int rmq(int a,int b)
{
int k=log((b-a+1)*1.0)/log(2.0);
return min(dp[a][k],dp[b-(1<<k)+1][k]);
}

int LCA(int u,int v)
{
if(pos[u]>pos[v])    swap(u,v);
int k=rmq(pos[u],pos[v]);
return vs[k];
}

int main()
{
while(scanf("%d%d",&V,&E)!=EOF)
{
for(int i=1;i<=V;i++)    G[i].clear();
cnt=0;
dep=0;
memset(d,0,sizeof(d));
for(int i=0;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d %*c",&u,&v,&cost);
G[u].push_back(P(v,cost));
G[v].push_back(P(u,cost));
}
dfs(1,-1);
init_rmq(cnt);
int Q;
scanf("%d",&Q);
while(Q--)
{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",d[u]+d[v]-2*d[LCA(u,v)]);
}
}

return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: