HDU 4460Friend Chains(對每個點BFS)
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2040 Accepted Submission(s): 712
Problem Description
For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than
7 persons.
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
Input
There are multiple cases.
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group.
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group.
Each of the next M lines contains two names which are separated by a space ,and they are friends.
Input ends with N = 0.
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group.
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group.
Each of the next M lines contains two names which are separated by a space ,and they are friends.
Input ends with N = 0.
Output
For each case, print the minimum value k in one line.
If the value of k is infinite, then print -1 instead.
If the value of k is infinite, then print -1 instead.
Sample Input
3
XXX
YYY
ZZZ
2
XXX YYY
YYY ZZZ
0
Sample Output
2
Source
題目大意:給你n個人,有m條連線,求任意兩點之間的最短距離的最大距離,如果有兩個點之間未聯通,那麼輸出-1.
解題思路:最短路的問題,當時能想到的就是弗洛伊德不過時間複雜度是O(10^9)會超時。可以對每個點都BFS,這樣時間複雜度就降下來了,具體實現見程式碼。
題目地址:Friend Chains
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<map>
#include<vector>
#include<queue>
using namespace std;
map <string,int> mq;
vector <int> p[1002];
queue <int> que;
int visi[1002];
int dis[1002][1002];
const int ma=10000;
void bfs(int i)
{
int j;
memset(visi,0,sizeof(visi));
visi[i]=1;
dis[i][i]=0;
que.push(i);
while(!que.empty())
{
int x=que.front(); //取出隊頭元素
que.pop();
for(j=0;j<p[x].size();j++)
{
int t=p[x][j];
if(!visi[t])
{
visi[t]=1;
dis[i][t]=dis[i][x]+1;
que.push(t);
}
}
}
}
int main()
{
int n,m,i,j,res;
char tmp[15];
while(scanf("%d",&n)&&n)
{
mq.clear(); //名字
for(i=0;i<n;i++)
p[i].clear(); //聯絡
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
dis[i][j]=ma; //初始化最大值
}
for(i=0;i<n;i++)
{
scanf("%s",tmp);
mq[tmp]=i;
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
int a,b;
scanf("%s",tmp);
a=mq[tmp];
scanf("%s",tmp);
b=mq[tmp];
p[a].push_back(b);
p[b].push_back(a); //兩者聯絡加進去
}
for(i=0;i<n;i++)
bfs(i);
res=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
res=max(res,dis[i][j]);
if(res==ma) puts("-1"); //存在兩個點不聯通的
else printf("%d\n",res);
}
return 0;
}
相關文章
- HDU 4460 Friend Chains(map + spfa)AI
- hdu5040 優先佇列+bfs佇列
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- 關於RAC每個節點更改對應的記憶體引數記憶體
- HDU 1026(優先佇列+BFS+前驅記錄)佇列
- 填充每個節點的下一個右側節點指標指標
- HDU1007Quoit Design(最小點對)
- BFS廣度優先搜尋(11)--hdu2102(基礎題)
- 填充每個節點的下一個右側節點指標 II指標
- 每個微服務對應一個程式碼庫嗎? - Reddit微服務
- 【分治 求最近點對】hdu 1007 Quoit Design
- BFS廣度優先搜尋(4)--hdu2717(poj3278)(基礎題)
- cache buffers chains and cache buffers lru chainsAI
- 熱點塊競爭和解決--cache buffers chainsAI
- 圖 - 每對頂點間最短路徑----Floyd演算法演算法
- 給每一個結點新增向右的next指標結點指標
- 2014西安網路賽1006||hdu5012 bfs
- mysql備份每個庫下面每個表MySql
- 116. 填充每個節點的下一個右側節點指標指標
- 每週一個前端動畫之三:twitter點贊動畫前端動畫
- LeetCode BFS題目以及要注意的點LeetCode
- SSL certificate chainsAI
- CACHE BUFFER CHAINSAI
- 【JAVA習題一】古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一 對兔子,假如兔子都不死,問每個月的兔子總數為多少?Java
- hdu3065 AC自動機-每個標準串在模式串中出現的次數模式
- Count BFS Graph
- 01BFS
- HDU 2732 Leapin' Lizards(拆點+最大流)API
- LeetCode-116-填充每個節點的下一個右側節點指標LeetCode指標
- LeetCode-117-填充每個節點的下一個右側節點指標 IILeetCode指標
- LeetCode117-填充每個節點的下一個右側節點指標 IILeetCode指標
- 2013成都站D題||hdu4784 bfs+DP+priority_queue(思路從spfa得到啟示)
- 每一個JavaScript開發者應該瞭解的浮點知識JavaScript
- HDU 1330 Nearest Common Ancestors(求兩個點的最近公共祖先)REST
- javascript實現的對陣列每一個元素都執行一個函式JavaScript陣列函式
- Oracle Cache Buffer ChainsOracleAI
- Latch: cache buffer chains (%)AI
- latch: cache buffers chainsAI