zoukankan      html  css  js  c++  java
  • CSU 1659: Graph Center(SPFA)

    1659: Graph Center

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 63  Solved: 25
    [Submit][Status][Web 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
    ",k); 
             for(int i=0; i<k; i++){ 
                printf("%d",id[i]); 
                if(i!=k-1) 
                 printf(" "); 
                else
                printf("
    "); 
             } 
        } 
    } 
      
    /************************************************************** 
        Problem: 1659 
        User: aking2015 
        Language: C++ 
        Result: Accepted 
        Time:256 ms 
        Memory:1848 kb 
    ****************************************************************/ 
    


  • 相关阅读:
    Android-PullToRefresh 下拉刷新增加setOnItemLongClickListener
    【453】周志华-机器学习-读书笔记
    【452】pandas筛选出表中满足另一个表所有条件的数据
    【451】python 同一行打印进度条
    【449】Win10 蓝牙耳机链接没有声音
    HBase(一)HBase入门简介
    kafka可视化客户端工具(Kafka Tool)的基本使用
    Kafka(五)Kafka的API操作和拦截器
    Kafka(四)Kafka在zookeeper中的存储
    Kafka(三)Kafka的高可用与生产消费过程解析
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5123869.html
Copyright © 2011-2022 走看看