WOJ1006 - Language of Animals

这篇博客介绍了WOJ1006编程题目,该题目涉及动物之间的沟通问题。当各种动物登上诺亚方舟时,由于每种动物都有自己的语言,交流变得困难。为了解决这个问题,需要确定至少需要多少个翻译动物才能让两种不能直接交流的动物进行沟通。博客中提到,可以使用SPFA算法来解决每个查询中的最少翻译数,还提供了STL vector的用法介绍及SPFA算法的学习资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

According to Ernest Mayr, America's leading taxonomist, there are over 1 million species of animals in the world.When all
kinds of animals took boat,they found a problem.Each kind of animal had its own language,so the communication became
very hard.Assume that animal A could communicate with animal B but it couldn't communicate with animal C,while animal C
could only communicate with animal B. So,animal B must translate what one said to the other if animal A and animal C wanted
to communicate with each other.We called animal B translator.Noah wanted to know the least translators if two animals
wanted to communicate with each other.

输入格式

There will be only one test case.The first line contains two integers n(2 <= n <=200,000) and
m(1 <= m <= 300,000),where n represents the number of animals and m represents the number of pairs which could
communicate with each other. The next m lines contains two numbers each,representing two animals who can communicate
with each other. The number starts at 0.The next line contains a integer k(k<=20) representing the number of queries,and
the last k lines contains two number each,representing two animals who wanted to communicate with each other.

输出格式

For each query,you should output the least translators if two animals in that query wanted to communicate with each other.
If they couldn't communicate with each other by translating ,output "-1".

样例输入

3 2
0 1
1 2
2
0 0
0 2

样例输出

0
1

对k个询问每个用spfa做一次就OK了

STL vector用法介绍

学习SPFA:

http://www.360doc.com/content/13/1208/22/14357424_335569176.shtml

http://baike.baidu.com/link?url=P06iyRkNxxdnYofqhZPAO-b5ttALUlBfVwA23mQTKAD0dXB1KTWdRvsoPNaM43HoTpmdIc04KVZFp1IIzriLhS4dNJxwyizyz4cZvuFLGFMGrmHgDUdeDPi1S22TjbrZ5XlsB_EHYvBJWAIi8OQLTa

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int n,m,k,dist[200002];
vector<int> g[200002];

inline bool relax(int u,int v)
{
    if(dist[u]+1<dist[v]) 
    {
        dist[v]=dist[u]+1;
        return true;
    }
    return false;
}
 
void spfa(int s)
{
    int vis[n+5];
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    q.push(s);
    vis[s] = true;
    dist[s] = 0;
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=0;i<g[temp].size();i++) 
            if(relax(temp, g[temp][i])&&!vis[g[temp][i]])
            {
                q.push(g[temp][i]);
                vis[g[temp][i]]=true;
            }
        vis[temp] = false;
    }
}
 
int main()
{
    int a,b,i,loop;
    scanf("%d %d", &n, &m);
    for(i=0;i<n;i++)
	g[i].clear();
    for(i=0;i<m;i++){
        scanf("%d %d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);// 无向图
    }
    scanf("%d",&k);
    for(loop=0;loop<k;loop++){
        for(i=0;i<n;i++)
		dist[i] = 0x7fffffff;//int的最大值 
        scanf("%d %d", &a, &b);
        if(a == b){
			puts("0");//输出字符串并换行 
			continue;
		}
        spfa(a);
        if(dist[b]==0x7fffffff) puts("-1");
        else printf("%d\n", dist[b]-1);
    }
    return 0;
}

类似的bfs解法:

点击打开链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值