HDU1690 (Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690

Wa了久,终于解决了,现在放出代码。

代码如下:

#include <iostream>
using namespace std;

const long long INF = 99999999999LL;  //INF要开得非常大才可以过,一直Wa就因为这个原因
const int N = 101;
long long map[N][N];

void floyd(int n, int m, int r);
void output(int m, int r);

int main()
{
  int t, i, j, k, len;
  int l1, l2, l3, l4, c1, c2, c3, c4;
  int n, m;
  int place[N];
  scanf("%d", &t);
  for(k = 1; k <= t; k++)
  {
    scanf("%d%d%d%d%d%d%d%d", &l1, &l2, &l3, &l4, &c1, &c2, &c3, &c4);
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; i++)
    {
      scanf("%d", &place[i]);
    }
    for(i = 1; i <= n; i++)
    {
      for(j = 1; j <= n; j++)
      {
        if(i == j)
        {
          map[i][j] = 0;
        }
        else
        {
          map[i][j] = INF;
        }
      }
    }
    for(i = 1; i <= n; i++)
    {
      for(j = i + 1; j <= n; j++)
      {
        len = abs(place[i] - place[j]);
        if(0 < len && len <= l1)
        {
          map[i][j] = map[j][i] = c1;
        }
        else if(l1 < len && len <= l2)
        {
          map[i][j] = map[j][i] = c2;
        }
        else if(l2 < len && len <= l3)
        {
          map[i][j] = map[j][i] = c3;
        }
        else if(l3 < len && len <= l4)
        {
          map[i][j] = map[j][i] = c4;
        }
      }
    }
    floyd(n, m, k);
  }
  return 0;
}

void floyd(int n, int m, int r)
{
  int i, j, k;
  for(k = 1; k <= n; k++)
  {
    for(i = 1; i <= n; i++)
    {
      for(j = 1; j <= n; j++)
      {
        if(map[i][j] > map[i][k] + map[k][j])
        {
          map[i][j] = map[i][k] + map[k][j];
        }
      }
    }
  }
  output(m, r);
}

void output(int m, int r)
{
  int i, j;
  printf("Case %d:\n", r);
  while(m--)
  {
    scanf("%d %d", &i, &j);
    if(map[i][j] != INF)
    {
      printf("The minimum cost between station %d and station %d is %I64d.\n", i, j, map[i][j]);
    }
    else
    {
      printf("Station %d and station %d are not attainable.\n", i, j);
    }
  }
}

原文地址:https://www.cnblogs.com/10jschen/p/2621154.html