简单枚举+FLOYD
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string.h> #include <queue> #include <cmath> #include <vector> using namespace std; const int N=310; const int inf=(1<<30)-1; int dist[N][N]; int cow[N]; int n,m; int main(){ int c; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) dist[i][j]=0; else dist[i][j]=inf; } } for(int i=1;i<=m;i++){ scanf("%d",&c); for(int k=1;k<=c;k++) scanf("%d",&cow[k]); for(int p=1;p<=c;p++){ for(int q=p+1;q<=c;q++){ dist[cow[p]][cow[q]]=dist[cow[q]][cow[p]]=1; } } } for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) if(dist[i][j]>dist[i][k]+dist[k][j]) dist[i][j]=dist[i][k]+dist[k][j]; } } int ans=inf; for(int i=1;i<=n;i++){ int tmp=0; for(int j=1;j<=n;j++) tmp+=dist[i][j]; if(ans>tmp) ans=tmp; } printf("%d\n",int(ans*100.0/(n-1))); } return 0; }
原文:http://www.cnblogs.com/jie-dcai/p/4296748.html