您的位置:首页 > 其它

51nod 1366 贫富差距(flody)

2017-12-14 20:20 253 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1366
题意:



思路:

如果不是一个连通块的话,肯定是无穷大的。

用floyd求出两两之间的最短路,然后在这些最短路中选最长的*d即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;

int n,k;
int d[55][55];
char mp[55][55];

int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(d,0x3f,sizeof(d));
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%s",mp[i]+1);
for(int j=1;j<=n;j++)
{
if(mp[i][j]=='Y')  d[i][j] = 1;
}
}

for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
}
}

int ans = 0;
bool flag = true;
for(int i=1;i<=n;i++)
{
if(!flag)  break;
for(int j=i+1;j<=n;j++)
{
if(d[i][j]==inf)  {flag=false;break;}
ans = max(ans,d[i][j]);
}
}
if(flag)  printf("%d\n",ans*k);
else puts("-1");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: