![:D](./images/smilies/icon_biggrin.gif)
Eduard.
Moderator: Board moderators
Code: Select all
conected = 1;
....
// The graph and the closure have been updated by the Floyd-Warshall
// algorithm up to this point.
for(i=0;i<n;i++)
for(j=0;j<n;j++)
conected = conected && closure[i][j];
if(!conected)
return -1.0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(graph[i][j]>max&&(i!=j))
max = graph[i][j];
Code: Select all
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(!clausura[i][j])
clausura[i][j] = clausura[i][k]&&clausura[k][j];
if(grafo[i][k] >= 0 && grafo[k][j] >= 0 && grafo[i][j] < 0)
grafo[i][j] = grafo[i][k] + grafo[k][j];
if(grafo[i][k] >= 0 && grafo[k][j] >= 0 && grafo[i][j] >= 0)
if(grafo[i][k] + grafo[k][j] < grafo[i][j])
grafo[i][j] = grafo[i][k] + grafo[k][j];
}
Code: Select all
grafo[k-1][i][j] = grafo[k-1][i][k] + grafo[k-1][k][j];
Code: Select all
#include <cstdio>
#include <climits>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
double sqr(int n)
{
return double(n*n);
}
int sqri(int n)
{
return n*n;
}
double dist(pair<int, int> a, pair<int, int> b)
{
if (sqri(a.first - b.first) + sqri(a.second - b.second) <= 100)
return sqrt(sqr(a.first - b.first) + sqr(a.second - b.second));
else
return INT_MAX;
}
int main()
{
int cases;
double graph[500][500];
scanf("%i", &cases);
vector<pair<int, int> > towns;
for (int c = 1; c <= cases; c++)
{
printf("Case #%i:\n", c);
towns.clear();
int n;
scanf("%i", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
graph[i][j] = INT_MAX;
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%i %i", &x, &y);
towns.push_back(pair<int, int>(x, y));
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
graph[j][i] = graph[i][j] = dist(towns[i], towns[j]);
/*
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
printf("%15.2lf", graph[i][j]);
printf("\n");
}
*/
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
{
double tmp = graph[j][i] + graph[i][k];
if (graph[j][k] > tmp)
graph[j][k] = tmp;
}
double max = 0;
bool found = false;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (!(abs(graph[i][j] - INT_MAX) < 1e-7) && max < graph[i][j])
{
max = graph[i][j];
found = true;
}
if (!found)
printf("Send Kurdy\n");
else
printf("%.4lf\n", max);
printf("\n");
}
return 0;
}
The code assigns INT_MAX to a `double,' so there won't be any overflows.Abednego wrote:Your "infinity" value (INT_MAX) is too big.What happens when inside the Floyd-Warshall, you add INT_MAX and, say, 5?
Code: Select all
1
2
0 0
10 0
Code: Select all
Case #1:
10.0000