标签:== 题意 tee users 基本 unique ane set const
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:
输出
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; }
TZOJ 3965 Six Degrees of Separation 最基本最短路 dijstra算法
标签:== 题意 tee users 基本 unique ane set const
原文地址:https://www.cnblogs.com/lhlccc/p/11871551.html