链接:http://poj.org/problem?id=2139
思路:构建cost[MAX][MAX]数组,依次找任意两点之间的最短距离。
/*样本输入
4 2
3 1 2 3
2 3 4
样本输出
100*/
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
//(Floyd-Wrashall算法)(求任意两点之间的最小距离)
const int MAX=310,INF=1e9+1;
int cost[MAX][MAX];//cost[i][j]表示顶点i到顶点j的权值
int num[MAX];
int d[MAX];//顶点s出发的最短路径
bool used[MAX];//已经访问过的点
int V;//顶点数
void floyd(){
for(int k=1;k<=V;k++) {
for (int i=1;i<=V;i++) {
for (int j=1;j<=V; j++) {
cost[i][j]=min(cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
}
void init(){
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++){
if(i==j)
cost[i][j]=0;//到自身的距离是0
else
cost[i][j]=INF;
}
}
int main(){
init();
int n,m;
cin>>n>>m;
int t;
for(int i=0;i<m;i++){
memset(num,0,sizeof(num));
cin>>t;
for(int i=0;i<t;i++)
cin>>num[i];
for(int j=0;j<t;j++)
for(int k=j+1;k<t;k++){
cost[num[j]][num[k]]=1;
cost[num[k]][num[j]]=1;
}
}
V=n;
floyd();//调用函数
int cnt=INF;//双重for循环的作用就是将从1,2,..,n个点出发到1,2,3,...,n的最短距离求出来,不用打表
for(int i=1;i<=n;i++){
int sum=0;
for(int j=1;j<=n;j++)
sum+=cost[i][j];
if(sum<cnt)
cnt=sum;
}
cout<<100*cnt/(n-1)<<endl;
}