标签:
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659
题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出.
注意这里大到其他点的距离是找一个最短的距离,那么就转化成了最短路的问题,求出每个点与其它点的最大距离,
保存并且求出最大距离中的最小距离,然后判断有几个点满足条件输出即可.
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <string> 9 #include <set> 10 #include <functional> 11 #include <numeric> 12 #include <sstream> 13 #include <stack> 14 #include <map> 15 #include <queue> 16 #include <deque> 17 //#pragma comment(linker, "/STACK:102400000,102400000") 18 #define CL(arr, val) memset(arr, val, sizeof(arr)) 19 20 #define ll long long 21 #define INF 0x7f7f7f7f 22 #define lc l,m,rt<<1 23 #define rc m + 1,r,rt<<1|1 24 #define pi acos(-1.0) 25 26 #define L(x) (x) << 1 27 #define R(x) (x) << 1 | 1 28 #define MID(l, r) (l + r) >> 1 29 #define Min(x, y) (x) < (y) ? (x) : (y) 30 #define Max(x, y) (x) < (y) ? (y) : (x) 31 #define E(x) (1 << (x)) 32 #define iabs(x) (x) < 0 ? -(x) : (x) 33 #define OUT(x) printf("%I64d\n", x) 34 #define lowbit(x) (x)&(-x) 35 #define Read() freopen("a.txt", "r", stdin) 36 #define Write() freopen("b.txt", "w", stdout); 37 #define maxn 110 38 #define maxv 5010 39 #define mod 1000000000 40 using namespace std; 41 42 struct edge 43 { 44 int to,weight; 45 edge() {} 46 edge(int a,int b) 47 { 48 to=a; 49 weight=b; 50 } 51 }; 52 53 vector<edge>adjmap[maxv]; 54 bool in_queue[maxn]; 55 int dist[maxn]; 56 int n,m,p[maxn],ans[maxn]; 57 58 int spfa(int a) 59 { 60 queue<int>que; 61 for(int i=1;i<=n;i++) 62 { 63 in_queue[i]=false; 64 dist[i]=INF; 65 } 66 que.push(a); 67 dist[a]=0; 68 in_queue[a]=true; 69 while(!que.empty()) 70 { 71 int x=que.front();que.pop(); 72 in_queue[x]=false; 73 for(int i=0;i<adjmap[x].size();i++) 74 { 75 int to=adjmap[x][i].to; 76 edge temp=adjmap[x][i]; 77 if(dist[x]<INF&&(dist[to]>dist[x]+temp.weight)) 78 { 79 if(!in_queue[to]) 80 { 81 in_queue[to]=true; 82 dist[to]=dist[x]+temp.weight; 83 que.push(to); 84 } 85 } 86 } 87 } 88 int x=0; 89 for(int i=1;i<=n;i++) 90 x=max(x,dist[i]); 91 return x; 92 } 93 int main() 94 { 95 //Read(); 96 int t,a,b; 97 scanf("%d",&t); 98 while(t--) 99 { 100 scanf("%d%d",&n,&m); 101 for(int i=1;i<=n;i++) adjmap[i].clear(); 102 for(int i=1;i<=m;i++) 103 { 104 scanf("%d%d",&a,&b); 105 adjmap[a].push_back(edge(b,1)); 106 adjmap[b].push_back(edge(a,1)); 107 } 108 int x=INF,j=0; 109 for(int i=1;i<=n;i++) 110 { 111 p[i]=spfa(i); 112 x=min(x,p[i]); 113 } 114 for(int i=1;i<=n;i++) 115 { 116 if(p[i]==x) ans[j++]=i; 117 } 118 printf("%d\n",j); 119 for(int i=0;i<j;i++) 120 { 121 if(i!=j-1) printf("%d ",ans[i]); 122 else printf("%d\n",ans[i]); 123 } 124 } 125 return 0; 126 }
标签:
原文地址:http://www.cnblogs.com/nowandforever/p/4701833.html