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果断不行. 一看题解,噗!最短路... 构图很巧妙. 每一个物品对应的所需物品相当于一个 ...
随机推荐
- 通用型CRM还是行业型CRM?-定制为王
大数据时代,怎样利用工具摆脱繁杂的数据管理之苦,洞察有价值的销售信息,是每一个管理者的迫切须要.Zoho CRM问世10年来,见证了一个个行业客户怎样在CRM帮助下实现了效率和业绩提升.相同,广泛的 ...
- Struts2 拦截器具体配置过程
拦截器差点儿遍布每个程序中,所以贴出拦截器配置的具体过程,希望可以帮到大家. Struts2 拦截器具体配置过程 <interceptors> <!-- 先定义拦截器 --> ...
- H3C低端交换机MAC绑定
1.MAC地址和端口的绑定<h3c>system[h3c]interface e0/1[h3c-interface]mac-address max-count #关闭交换机端口的MAC学习 ...
- Django写的投票系统1(转)
当然主要是从django的帮助文档里面来的,权当是翻译吧 这个投票系统的主要功能有 1.一个前台页面,可以让用户来投票 2.一个管理员页面,可以用来添加.修改.删除投票 首页第一步要确定你已经安装了D ...
- Hdu 4539 【状态DP】.cpp
题意: 一个炮兵可以攻打和他之间曼哈顿距离为2的士兵,给出你一块n*m的战场,告诉你哪些地方可以站人哪些地方不可以,问你最多可以安放多少个士兵? n <= 100, m <= 10 思路: ...
- kubuntu14.04以下vpn(vpnc)连接配置
前几天在公司内部一直配置不了kubuntu14.04以下的vpn,从而无法实如今外网訪问公司内网的一些功能:是不方便在回家后继续coding(当然还有其他的事情.如邮件收发等.能够不用在linux以下 ...
- linux下查看进城(ps)的方法 与 杀死进程(kill)的N种方法
PS查看进程 inux上进程有5种状态: 1. 运行(正在运行或在运行队列中等待) 2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号) 3. 不可中断(收到信号不唤醒和不可运行, 进程必 ...
- eclipse 安装vrapper vim插件
http://vrapper.sourceforge.net/update-site/stable 如果安装不上,设置下代理./window/pereference/network* - manul
- java导入excel
package com.duosen.gate.test; import java.io.File; import java.io.FileInputStream; import java.io.Fi ...
- 一百万数据索引实例測试--mysql
推荐书籍:http://pan.baidu.com/s/1sjJIyRV 任务描写叙述: 如果一高频查询例如以下 SELECT * FROM user WHERE area='amoy' AND s ...