CSU 1659: Graph Center(SPFA)
1659: Graph Center
Time Limit: 1 Sec Memory Limit: id=1659">Submit
128 MB
Submit: 63 Solved: 25
[
Board]
Description
The center of a graph is the set of all vertices of minimum eccentricity, that is, the set of all vertices A where the greatest distance d(A,B) to other vertices B is minimal. Equivalently, it is the set of vertices with eccentricity equal to the graph's
radius. Thus vertices in the center (central points) minimize the maximal distance from other points in the graph.
------wikipedia
Now you are given a graph, tell me the vertices which are the graph center.
Input
There are multiple test cases.
The first line will contain a positive integer T (T ≤ 300) meaning the number of test cases.
For each test case, the first line contains the number of vertices N (3 ≤ N ≤ 100) and the number of edges M (N - 1 ≤ N * (N - 1) / 2). Each of the following N lines contains two vertices x (1 ≤ x ≤ N) and y (1 ≤ y ≤ N), meaning there is an edge between x and
y.
Output
The first line show contain the number of vertices which are the graph center. Then the next line should list them by increasing order, and every two adjacent number should be separated by a single space.
Sample Input
2
4 3
1 3
1 2
2 4
5 5
1 4
1 3
2 4
2 3
4 5
Sample Output
2
1 2
3
1 2 4
HINT
Source
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std; const int MAXN = 105;
const int MAXM = 100005;
const int INF = 1<<30;
struct EDG{
int to,next;
}edg[MAXM];
int eid,head[MAXN]; void init(){
eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v){
edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;
edg[eid].to=u; edg[eid].next=head[v]; head[v]=eid++;
}
int spfa(int s,int n){
queue<int>q;
bool inq[MAXN]={0};
int d[MAXN];
for(int i=1; i<=n; i++)
d[i]=INF;
d[s]=0;
q.push(s);
while(!q.empty()){
int u=q.front(); q.pop();
inq[s]=0;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(d[v]>d[u]+1){
d[v]=d[u]+1;
if(!inq[v])
q.push(v),inq[v]=1;
}
}
}
int maxt=0;
for(int i=1; i<=n; i++)
if(maxt<d[i])
maxt=d[i];
return maxt;
}
int main(){
int T,n,m,u,v,d[MAXN],id[MAXN];
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
while(m--){
scanf("%d%d",&u,&v);
addEdg(u,v);
}
int mint=INF;
for(int i=1;i<=n;i++){
d[i]=spfa(i,n);
if(d[i]<mint)
mint=d[i];
}
int k=0;
for(int i=1; i<=n; i++)
if(mint==d[i]){
id[k++]=i;
}
printf("%d\n",k);
for(int i=0; i<k; i++){
printf("%d",id[i]);
if(i!=k-1)
printf(" ");
else
printf("\n");
}
}
} /**************************************************************
Problem: 1659
User: aking2015
Language: C++
Result: Accepted
Time:256 ms
Memory:1848 kb
****************************************************************/
CSU 1659: Graph Center(SPFA)的更多相关文章
- csu - 1659 Graph Center(最短路)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659 题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出. 注 ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- 【转】使用Boost Graph library(二)
原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...
- 最短路(SPFA)
SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...
- Bellman-Ford算法及其队列优化(SPFA)
一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...
- Funny Car Racing CSU - 1333 (spfa)
There is a funny car racing in a city with n junctions and m directed roads. The funny part is: each ...
- sgu 240 Runaway (spfa)
题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...
- codevs 1021 玛丽卡(spfa)
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
- 【POJ】1062 昂贵的聘礼(spfa)
http://poj.org/problem?id=1062 此题一开始果断想到暴力.. 但是n<=100果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...
随机推荐
- SuSE(SLES)安装配置syslog-ng日志server,可整合splunk
Update History 2014年04月25日 - 撰写初稿 引言 在自己主动化部署AutoYast.自己主动化监控BMC Patrol双方面形成雏形后.日志的收集.管理.分析也顺势成为我们须要 ...
- MVC多语言应用
MVC多语言应用 最近发现资源文件是个好东西, 用的好了可以给开发人员借阅不少的时间. 例如做一个多语言的网站, 资源文件就有不小的用处. 这里以MVC4模版项目的登录页为例, 简单说一下过程: 1. ...
- OCP读书笔记(13) - 管理内存
SGA 1. 什么是LRULRU表示Least Recently Used,也就是指最近最少使用的buffer header链表LRU链表串联起来的buffer header都指向可用数据块 2. 什 ...
- 使用Ajax以及Jquery.form异步上传图片
一.前言 之前做图片上传一直用的第三方插件,Uploadify 这个应该是用的比較多的,相同也用过别的,在方便了自己的同一时候也非常赞叹人家的功能. 思来想去,仅仅会用别的人东西,始终自己学到的少, ...
- STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0 这个程序使用了vect ...
- POj 1879 Tempus et mobilius Time and motion (模拟+群)
题目特别长,大意为球的传递. 三个轨道,一个库.各自是分钟单位的轨道.5min单位的轨道.一小时单位的轨道.还有就是n容量的库. 每过一分钟,一个小球从库里面出来,库符合先进先出,进入分钟轨道.假设分 ...
- firebug登陆之数据包分析
登陆之数据包分析 工具: python-urllib2 | firefox+firebug或者chrome,用浏览器打开登陆页面之后,按F12键会默认打开开发者工具或者启动firebug,点击n ...
- hdu1260(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 分析:简单dp,dp[i]=min(dp[i-1]+a[i],dp[i-2]); #includ ...
- STL之Vector(不定长数组)
vector是同一种对象的集合,每一个对象都有一个相应的整数索引值.和string对象一样,标准库将负责管理与存储元素相关的类存. 引入头文件 #include<vector> 1.vec ...
- delphi 发送消息控制滚动条
1.Perform 函数 DBGrid1.Perform(WM_VSCROLL,SB_PAGEDOWN,0); //控制滚动条,向后翻页 DBGrid1.Perform(WM_VSCROLL,SB_ ...