Cow's travel (Floyd application)

Insert picture description here

Idea: According to the meaning of the question, we ask what is the shortest and longest in the pasture. First, if there is only one pasture, then we only need to run the shortest path. Then, find the shortest and longest distance between each point, we use mxd[i] Indicates the longest and shortest distance from point i to other points. If we need to connect pastures, we need to enumerate the connection points between pastures. When dis[i][j]==inf, it means that the edges can be connected. At this time, we find the edges as small as possible to make mxd[i] +dis[i][j]+mxd[j] should be as small as possible, and finally see which of the two is larger is the required diameter.

#pragma GCC optimize(2)
#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
typedef pair<int,PII> PIII;
const int mod=100003;
const int N=2e5+5;
const int inf=0x7f7f7f7f;

int gcd(int a,int b)
{
    
    
    return b==0?a:gcd(b,a%b);
}
 
ll lcm(ll a,ll b)
{
    
    
    return a*(b/gcd(a,b));
}
 
template <class T>
void read(T &x)
{
    
    
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    
    
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    
    
    ll res=1%p;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
PII node[200];
char ch[200][200];
double dis[200][200],mxd[200];
int n;

double get_dis(PII x,PII y)
{
    
    
    double a=x.first-y.first;
    double b=x.second-y.second;
    return sqrt(a*a+b*b);
}
int main()
{
    
    
  cin>>n;
  for(int i=0;i<n;i++)
  {
    
    
      cin>>node[i].first>>node[i].second;

  }
  for(int i=0;i<n;i++) cin>>ch[i];
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)
   {
    
    
       if(i!=j)
       {
    
    
           if(ch[i][j]=='1') dis[i][j]=get_dis(node[i],node[j]);
           else dis[i][j]=inf;
       }
   }

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

     for(int i=0;i<n;i++)
       for(int j=0;j<n;j++)
         {
    
    
             if(dis[i][j]<inf)
             mxd[i]=max(mxd[i],dis[i][j]);
         }

      double res1=0;
      for(int i=0;i<n;i++)res1=max(res1,mxd[i]);

      double res2=inf;
      for(int i=0;i<n;i++)
       for(int j=0;j<n;j++)
        {
    
    
            if(dis[i][j]>=inf)
            {
    
    
                res2=min(res2,mxd[i]+get_dis(node[i],node[j])+mxd[j]);
            }
        }
    printf("%lf\n",max(res1,res2));
   return 0;

}


Guess you like

Origin blog.csdn.net/qq_43619680/article/details/113096645