zoukankan      html  css  js  c++  java
  • 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);
        }
      }
    }

  • 相关阅读:
    10 种保护 Spring Boot 应用的绝佳方法
    Redis 如何分析慢查询操作?
    Spring Boot 主类及目录结构介绍
    Redis 再牛逼,也得设置密码!!
    Spring Data Redis 详解及实战一文搞定
    Spring Boot Redis Cluster 实战干货
    超详细的 Redis Cluster 官方集群搭建指南
    Redis Linux 安装运行实战全记录
    hdu 4790 Just Random (思路+分类计算+数学)
    poj 1328 Radar Installation(贪心)
  • 原文地址:https://www.cnblogs.com/10jschen/p/2621154.html
Copyright © 2011-2022 走看看