ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

TZOJ 3965 Six Degrees of Separation 最基本最短路 dijstra算法

2019-11-16 13:57:15  阅读:394  来源: 互联网

标签:case Koobecaf Six two dijstra 3965 connected others five


Have you ever heard of the word "six degrees of separation"? It is said that two individuals are connected by at most five others. Lee is wondering about this and he wants to check it via the world's biggest friendship web called Koobecaf.

On Koobecaf, if A and B are friends, so are B and C, but either A or C knows the others, we say A and C are connected by one others, that's, B. The following pictures shows this case.

And the following one shows that 1 can reach 8 via five others. (Hmm... It is the first case of the sample input below.)

Now Lee will randomly pick two individuals and please help him check if the two are connected by less than six others.

 

输入

 

Input will consist of multiple test cases. Then for each test case:

  1. One line with two integers n and m, where n is the number of users on the Koobecaf (2 ≤ n ≤ 1000) and m is the number of pairs of friends (0 ≤ m ≤ 106).
  2. m lines following. Each line contains two integers A and B, denoted that A and B are friends (1 ≤ A, B ≤ n, A != B). It is guarantee that each pair is unique. And of course, "A B" is the same as "B A", thus they won't exist at the same time.
  3. The last line contains two integers X and Y, the two individuals to check (1 ≤ X, Y ≤ n, X != Y).

 

输出

 

For each case, output "YES" (without quotation marks) if X and Y are connected by at most five others, or "NO" otherwise.

样例输入

 

10 9
1 2
2 3
3 1
3 4
4 5
5 6
6 7
7 8
8 10
1 8
10 9
1 2
2 3
3 1
3 4
4 5
5 6
6 7
7 8
8 10
1 10

样例输出

YES
NO

 

大致题意:给定n个人和m对关系  最后给定2个人  问是否两人之间相隔不超过6人。

#include<bits/stdc++.h>
#define inf 1<<29
#define CL(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=1010;
int n,m,a,b;
int Map[N][N];
bool vis[N];
int dist[N];
int dijkstra(int x,int y)
{
    CL(vis,false);
    for(int i=1;i<=n;i++) dist[i]=inf;
    dist[x]=0;
    while(1){
        int v=-1;
        for(int u=1;u<=n;u++)
            if(!vis[u]&&(v==-1||dist[u]<dist[v])) v=u;
        if(v==-1) break;
        vis[v]=true;
        for(int u=1;u<=n;u++)
            dist[u]=min(dist[u],dist[v]+Map[v][u]);
    }
    return dist[y];
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                Map[i][j]=Map[j][i]=inf;
            Map[i][i]=0;
        }
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            Map[a][b]=Map[b][a]=1;
        }
        scanf("%d%d",&a,&b);
        if(dijkstra(a,b)>=7)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

 

 

 

标签:case,Koobecaf,Six,two,dijstra,3965,connected,others,five
来源: https://www.cnblogs.com/lhlccc/p/11871551.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有