POJ 1986 Distance Queries && HDOJ 2586 How far away?


Distance Queries

Time Limit: 2000MS

 

Memory Limit: 30000K

Total Submissions: 11677

 

Accepted: 4124

Case Time Limit: 1000MS

Description

Farmer 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

Hint

Farms 2 and 6 are 20+3+13=36 apart. 



LCA算法简介:传送门

这道题很不爽的地方便是输入说明在另一题中(要不要这么懒。。。),贴上那道题的链接


题意:给出k个询问,求每个询问中的节点间的最短距离。

思路:我们可以分别求出各点(i)到根节点的距离,设为dis[i],则对于x,y两点,它们之间的最短距离为dis[x]+dis[y]-2*dis[LCA(x,y)].

  1. 给出的信息中有方向,但由于最后这些点总会构成一棵树,故可忽略方向.
  2. 因为是无向图,所以无法知道根节点,所以我们在存边时,同时存反向边(建双向图),然后每个点都可以作为根节点,为方便起见,取1为根节点
  3. 在求LCA时,同时更新dis值
  4. 由于n最大可为40000,故无法用邻接矩阵存边,只能用邻接表或vector,我采用了邻接表
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn= 40005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

int n,m;
int cnt;
int vis[maxn];
int pre[maxn];
int head1[maxn],head2[maxn];
int dis[maxn];

struct node
{
    int s,t,w,lca,next;
}e1[maxn*2],e2[maxn];

void init()
{
    mst(vis,0);
    mst(dis,0);
    mst(head1,-1);
    mst(head2,-1);
    for(int i=1;i<=n;i++)
        pre[i]=i;
}

int find(int x)
{
    int t,r=x;
    while(x!=pre[x])
    {
        x=pre[x];
    }
    while(r!=x)
    {
        t=pre[r];
        pre[r]=x;
        r=t;
    }
    return x;
}

void join(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
        pre[B]=A;
}
void add1(int s,int t,int w)
{
    e1[cnt].s=s;
    e1[cnt].t=t;
    e1[cnt].w=w;
    e1[cnt].next=head1[s];
    head1[s]=cnt++;
}

void add2(int s,int t)
{
    e2[cnt].s=s;
    e2[cnt].t=t;
    e2[cnt].lca=-1;
    e2[cnt].next=head2[s];
    head2[s]=cnt++;
}

void Tarjan(int now)
{
    vis[now]=1;
    for(int i=head1[now];i!=-1;i=e1[i].next)
    {
        int t=e1[i].t;
        if(vis[t]==0)
        {
            int w=e1[i].w;
            dis[t]=dis[now]+w;
            Tarjan(t);
            join(now,t);
        }
    }
    for(int i=head2[now];i!=-1;i=e2[i].next)
    {
        int t=e2[i].t;
        if(vis[t]==1)
        {
            e2[i].lca=find(t);
            e2[i^1].lca=e2[i].lca;
        }
    }
}

int main()
{
    int x,y,w;
    int k;
    char s[5];
    while(~scanf("%d%d",&n,&m))
    {
        init();
        cnt=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%s",&x,&y,&w,s);
            add1(x,y,w);
            add1(y,x,w);
        }
        cnt=0;
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            scanf("%d%d",&x,&y);
            add2(x,y);
            add2(y,x);
        }
        Tarjan(1);
        for(int i=0;i<k;i++)
        {
            int now=i*2;
            int u=e2[now].s;
            int v=e2[now].t;
            int lca=e2[now].lca;
            int ans=dis[u]+dis[v]-2*dis[lca];
            printf("%d\n",ans);
        }
    }
    return 0;
}



接下来是HDOJ上一道几乎一样的题目(当然代码还是重新敲了一下加深印象,上下可能会略有不同)


How far away

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 15532    Accepted Submission(s): 5891

Problem Description

There are n houses in the village and some bidirectional roads connecting them. Everyday peole always like to ask like this "How far is it if I want to go fromhouse A to house B"? Usually it hard to answer. But luckily int thisvillage the answer is always unique, since the roads are built in the way thatthere is a unique simple path("simple" means you can't visit a placetwice) between every two houses. Yout task is to answer all these curiouspeople.

 

 

Input

First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbersn(2<=n<=40000) and m (1<=m<=200),the number of houses and thenumber of queries. The following n-1 lines each consisting three numbers i,j,k,separated bu a single space, meaning that there is a road connecting house iand house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answerthe distance between house i and house j.

 

 

Output

For each test case,output m lines. Each line represents the answer of the query.Output a bland line after each test case.

 

 

Sample Input

2

3 2

1 2 10

3 1 15

1 2

2 3

 

2 2

1 2 100

1 2

2 1

 

 

Sample Output

10

25

100

100


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn1= 40005;
const int maxn2= 205;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)

int n,m;
int cnt;
int pre[maxn1];
int head1[maxn1],head2[maxn1];
int vis[maxn1];
int dis[maxn1];

struct node
{
    int s,t,w,lca,next;
}e1[maxn1*2],e2[maxn2*2];

void add1(int s,int t,int w)
{
    e1[cnt].s=s;
    e1[cnt].t=t;
    e1[cnt].w=w;
    e1[cnt].next=head1[s];
    head1[s]=cnt++;
}

void add2(int s,int t)
{
    e2[cnt].s=s;
    e2[cnt].t=t;
    e2[cnt].lca=-1;
    e2[cnt].next=head2[s];
    head2[s]=cnt++;
}

void init()
{
    mst(vis,0);
    mst(dis,0);
    mst(head1,-1);
    mst(head2,-1);
    for(int i=1;i<=n;i++)
        pre[i]=i;
}

int find(int x)
{
    int t,r=x;
    while(x!=pre[x])
    {
        x=pre[x];
    }
    while(r!=x)
    {
        t=pre[r];
        pre[r]=x;
        r=t;
    }
    return x;
}

void join(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
        pre[B]=A;
}

void Tarjan(int now)
{
    vis[now]=1;
    for(int i=head1[now];i!=-1;i=e1[i].next)
    {
        int t=e1[i].t;
        if(vis[t]==0)
        {
            int w=e1[i].w;
            dis[t]=dis[now]+w;
            Tarjan(t);
            join(now,t);
        }
    }
    for(int i=head2[now];i!=-1;i=e2[i].next)
    {
        int t=e2[i].t;
        if(vis[t]==1)
        {
            e2[i].lca=find(t);
            e2[i^1].lca=e2[i].lca;
        }
    }
}

int main()
{
    int x,y,w;
    rush()
    {
        scanf("%d%d",&n,&m);
        init();
        cnt=0;
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            add1(x,y,w);
            add1(y,x,w);
        }
        cnt=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            add2(x,y);
            add2(y,x);
        }
        Tarjan(1);
        for(int i=0;i<m;i++)
        {
            int now=i*2;
            int s=e2[now].s;
            int t=e2[now].t;
            int lca=e2[now].lca;
            int ans=dis[s]+dis[t]-2*dis[lca];
            printf("%d\n",ans);
        }
    }
    return 0;
}




    • 1
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值