HDOJ 4460 Friend Chains 图的最长路
2015-08-17 17:25
525 查看
类似于树的直径,从任意一个点出发,找到距离该点最远的且度数最少的点.
然后再做一次最短路
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
2012 Asia Hangzhou Regional Contest
/* ***********************************************
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;
}
然后再做一次最短路
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
2012 Asia Hangzhou Regional Contest
/* ***********************************************
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;
}
相关文章推荐
- 2015 Multi-University Training Contest 8(HDOJ5384、5389)
- 2015 Multi-University Training Contest 8
- Testng 运行报错:"Total tests run: 0, Failures: 0, Skips: 0"以及找不到class文件的问题
- [观察]HDOJ1021Fibonacci Again
- 2015 HUAS Summer Trainning #6~G
- 关于hp proliant sl210t服务器raid 1阵列配置
- Agent admitted failure to sign using the key
- retain(strong)和copy之间的关系
- Air Raid
- 基于QMainWindow的主窗口程序例子
- 70 Climbing Stairs
- 【问题及解决】Elasticsearch--Failed to deserialize exception response from stream
- UVA_10651_PebbleSolitaire
- 读Avoiding the Disk Bottleneck in the Data Domain Deduplication File System
- 如何自定义WPF项目的Main函数
- 关于NSString的retainCount的各种结果原因
- HDU 1401 Solitaire (双向搜索)
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
- [Linux 运维 -- 存储] RAID入门
- HDU 2457 DNA repair (AC自动机 + DP)