HDOJ 4460 Friend Chains 图的最长路

本文探讨了一种社交网络中连接任意两人的最短路径问题,即寻找最小的社交链长度k,使得网络中任意两人之间的社交链长度都不超过k。通过构建图模型并使用SPFA算法来解决该问题。


类似于树的直径,从任意一个点出发,找到距离该点最远的且度数最少的点.

然后再做一次最短路

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4227    Accepted Submission(s): 1359


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 .
 

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.
 

Output
For each case, print the minimum value k in one line. 
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
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年08月17日 星期一 16时35分00秒
File Name     :HDOJ4460.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=2100;

int n,m;
int id=1;
map<string,int> msi;


int getID(string name)
{
	if(msi[name]==0) msi[name]=id++;
	return msi[name]; 
}

struct Edge
{
	int to,next,cost;
}edge[maxn*maxn];

int Adj[maxn],Size;
int du[maxn];

void init()
{
	id=1; msi.clear();
	memset(du,0,sizeof(du));
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v)
{
	edge[Size].to=v;
	edge[Size].cost=1;
	edge[Size].next=Adj[u];
	Adj[u]=Size++;
}

int dist[maxn],cq[maxn];
bool inq[maxn];

bool spfa(int st)
{
    memset(dist,63,sizeof(dist));
    memset(cq,0,sizeof(cq));
    memset(inq,false,sizeof(inq));
    dist[st]=0;
    queue<int> q;
    inq[st]=true;q.push(st); cq[st]=1;

    while(!q.empty())
    {
        int u=q.front();q.pop();

        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                if(!inq[v])
                {
                    inq[v]=true;
                    cq[v]++;
                    if(cq[v]>=n+10) return false;
                    q.push(v);
                }
            }
        }
        inq[u]=false;
    }
    return true;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d",&n)!=EOF&&n)
	{
		init();
		string name1,name2;
		for(int i=1;i<=n;i++) 
		{
			cin>>name1;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			cin>>name1>>name2;
			int id1=getID(name1);
			int id2=getID(name2);
			Add_Edge(id1,id2); Add_Edge(id2,id1);
			du[id1]++; du[id2]++;
		}
		spfa(1);
		int st=1;
		for(int i=2;i<=n;i++)
		{
			if(dist[st]<dist[i]) st=i;
			else if(dist[st]==dist[i])
			{
				if(du[st]>du[i]) st=i;
			}
		}
		spfa(st);
		int ans=0;
		for(int i=1;i<=n;i++) ans=max(ans,dist[i]);
		if(ans==INF) ans=-1;
		cout<<ans<<endl;
	}
    
    return 0;
}




乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值