POJ 2139 Six Degrees of Cowvin Bacon(floyd)
考完试放假有一段时间了,好久没有做题了,本来说好寒假要好好做acm,可是一星期过去了,连题目都没看。。。。isap都快写不出来了,手真的生了很多,做个题热热身。 /* 又是牛,如果cow_i 和cow_j干同一件工作,那么cow_i和cow_j的分离度就是1,如果cow_i和cow_j分别与cow_k工作,但是cow_i和cow_j不干同样的工作,那么cow_i和cow_j的分离度就是2,也就是说这种关系是可传递的,并且一次递增,要求找出一个cow_res,他和其他的cow的分离度的和是所有其他cow的分离度和的最小值,mmin = min{dist[cow_i]},注意结果的形式int(res)才可以,看了discuss才知道,算法就明了了,就是求所有的cow_i和cow_j的距离——floyd算法。*/ |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
using namespace std;
#define INF 0x7ffffff
#define MAXN 600
int nv,ne,m,n;
int g[MAXN][MAXN],dist[MAXN][MAXN],index[MAXN];
int floyd()
{
int i,j,k;
for(k = 1; k <= n; ++k)
for(i = 1; i <= n; ++i)
for(j = 1; j <= n; ++j)
if(g[i][k]!=-1&&g[k][j]!=-1&&(g[i][j]==-1||
g[i][j]>g[i][k]+g[k][j]))
g[i][j] = g[i][k]+g[k][j];
return 0;
}
int main()
{
int i,k,j,h,tmp,sum;
memset(g,-1,sizeof(g));
scanf("%d%d",&n,&m);
for(i = 0;i < m; ++i)
{
scanf("%d",&tmp);
for(j = 0;j < tmp; ++j)
scanf("%d",&index[j]);
for(j = 0;j < tmp; ++j)
for( k = 0; k < j; ++k)
g[index[j]][index[k]] = g[index[k]][index[j]] = 1;
}
floyd();
memset(index,0,sizeof(index));
int mmin = INF;
for(i = 1;i <= n; ++i)
{
sum = 0;
for(j = 1; j <= n; ++j)
if(i != j)
sum += g[i][j];
mmin = sum<mmin?sum:mmin;
}
printf("%d\n",int(mmin*100/(n-1)));
return 0;
}
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
using namespace std;
#define INF 0x7ffffff
#define MAXN 600
int nv,ne,m,n;
int g[MAXN][MAXN],dist[MAXN][MAXN],index[MAXN];
int floyd()
{
int i,j,k;
for(k = 1; k <= n; ++k)
for(i = 1; i <= n; ++i)
for(j = 1; j <= n; ++j)
if(g[i][k]!=-1&&g[k][j]!=-1&&(g[i][j]==-1||
g[i][j]>g[i][k]+g[k][j]))
g[i][j] = g[i][k]+g[k][j];
return 0;
}
int main()
{
int i,k,j,h,tmp,sum;
memset(g,-1,sizeof(g));
scanf("%d%d",&n,&m);
for(i = 0;i < m; ++i)
{
scanf("%d",&tmp);
for(j = 0;j < tmp; ++j)
scanf("%d",&index[j]);
for(j = 0;j < tmp; ++j)
for( k = 0; k < j; ++k)
g[index[j]][index[k]] = g[index[k]][index[j]] = 1;
}
floyd();
memset(index,0,sizeof(index));
int mmin = INF;
for(i = 1;i <= n; ++i)
{
sum = 0;
for(j = 1; j <= n; ++j)
if(i != j)
sum += g[i][j];
mmin = sum<mmin?sum:mmin;
}
printf("%d\n",int(mmin*100/(n-1)));
return 0;
}