HDU 4460 Friend Chains(map + spfa)
題目大意:給你一個朋友之間的關係讓你判斷所有人之間的朋友“鏈”的最大長度,如果大於7就不可以,否則輸出最大值。
列舉n個點進行n次spfa。
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4008 Accepted Submission(s): 1290
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <time.h>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
///#define LL long long
#define LL __int64
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007
using namespace std;
const int maxn = 1010;
map<string, int> mp;
string str;
string str1;
struct node
{
int v;
int w;
int next;
}f[maxn*maxn/2];
int head[maxn*maxn/2];
int cnt;
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int w)
{
f[cnt].v = v;
f[cnt].w = w;
f[cnt].next = head[u];
head[u] = cnt++;
f[cnt].v = u;
f[cnt].w = w;
f[cnt].next = head[v];
head[v] = cnt++;
}
int spfa(int x, int n)
{
bool vis[maxn];
int dis[maxn];
queue<int>que;
for(int i = 1; i <= n; i++)
{
dis[i] = INF;
vis[i] = false;
}
dis[x] = 0;
vis[x] = true;
que.push(x);
while(!que.empty())
{
int sx = que.front();
que.pop();
vis[sx] = false;
for(int i = head[sx]; i != -1; i = f[i].next)
{
int v = f[i].v;
if(dis[v] > dis[sx]+f[i].w)
{
dis[v] = dis[sx]+f[i].w;
if(!vis[v])
{
vis[v] = true;
que.push(v);
}
}
}
}
int Max = 0;
for(int i = 1; i <= n; i++)
{
if(!dis[i]) continue;
Max = max(Max, dis[i]);
}
return Max;
}
vector<int>g[maxn];
bool sta[maxn];
void dfs(int x, int fa)
{
sta[x] = 1;
for(int i = head[x]; i != -1; i = f[i].next)
{
int sx = f[i].v;
if(sta[sx] || sx == fa) continue;
dfs(sx, x);
}
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
for(int i = 1; i <= n; i++)
{
cin >>str;
mp[str] = i;
}
int m;
scanf("%d",&m);
int x, y;
init();
for(int i = 0; i < m; i++)
{
cin >>str>>str1;
x = mp[str];
y = mp[str1];
add(x, y, 1);
}
memset(sta, false, sizeof(sta));
dfs(1, -1);
int xflag = 0;
for(int i = 1; i <= n; i++)
{
if(sta[i]) continue;
xflag = 1;
break;
}
if(xflag)
{
puts("-1");
continue;
}
int flag = 0;
int Max = 0;
for(int i = 1; i <= n; i++)
{
int sx = spfa(i, n);
if(sx >= 7)
{
flag = 1;
break;
}
Max = max(Max, sx);
}
if(flag)
{
puts("-1");
continue;
}
printf("%d\n",Max);
}
}
相關文章
- HDU 4460Friend Chains(對每個點BFS)AI
- hdu 4568 spfa 最短路演算法+旅行商問題演算法
- HDU 5726-GCD(暴力+map)GC
- HDU4941Magical Forest(map)REST
- P4460
- HDU-3172 Virtual Friends 並查集+map並查集
- hdu1874 暢通工程續 Bellman-Ford演算法SPFA演算法
- cache buffers chains and cache buffers lru chainsAI
- SPFA演算法演算法
- spfa最佳化
- SPFA && dijkstra 模版
- 2013成都站D題||hdu4784 bfs+DP+priority_queue(思路從spfa得到啟示)
- SSL certificate chainsAI
- CACHE BUFFER CHAINSAI
- Oracle Cache Buffer ChainsOracleAI
- Latch: cache buffer chains (%)AI
- latch: cache buffers chainsAI
- BZOJ4460 : [Jsoi2013]廣告計劃JS
- hdu 4821 ||2013年長春站J題 字串雜湊+map的應用字串
- Oracle Least Recently Used ChainsOracleASTAI
- latch:cache buffers chains案例AI
- POJ 1511-Invitation Cards(SPFA)
- SPFA演算法模板(C/C++)演算法C++
- 最短路(DJsktra,spfa,flyd).mdJS
- 用於排查cache buffers chainsAI
- Cache Buffers chains,存在共享模式?AI模式
- sudoku 數獨 XY-ChainsAI
- POJ 1724 ROADS(優先佇列+spfa)佇列
- 物件導向設計裡引入 Friend 是對封裝性的破壞嗎,friend 在 SAP ABAP 裡的應用場景物件封裝
- 每日一句:A friend and I went out to a bar and got wasted.GoAST
- 深入理解latch: cache buffers chainsAI
- Trouble shooting latch: cache buffers chainsAI
- ORACLE等待事件latch: cache buffers chainsOracle事件AI
- 等待事件_cache_buffers_chains_latch事件AI
- 最短路-SPFA演算法&Floyd演算法演算法
- Map
- 最短路演算法詳解(Dijkstra/SPFA/Floyd)演算法
- latch:cache buffers chains解決步驟AI