1072.Gas Station (30)

1072.Gas Station (30)

pat-al-1072

2017-02-02

  • 本质是dijkstra算法,只是调用了多次,然后比较每次的结果,选出能给所有住宅区提供服务中距离住宅区最短距离最长的、平均距离最短的、序号最小的(有一个规则有点绕,要仔细读题)
  • 坑:注意,计算平均距离和最短距离时只对住宅区进行计算,不算和其他备选站的距离;但是在算单源最短路径时要包含计算备选站,因为可以经过备选站来到住宅区
  • 一个坑:我自己这是3.2,但是提交之后是「答案正确」,也就是说是3.3,所以不要去特意搞成四舍五入什么的
/**
 * pat-al-1072
 * 2017-02-01
 * C version
 * Author: fengLian_s
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 0x3f3f3f3f
#define MAX 1012
int main()
{
  freopen("in.txt", "r", stdin);
  int n, m, k, d;
  int dist[MAX], e[MAX][MAX], visited[1012];
  scanf("%d%d%d%d", &n, &m, &k, &d);
  memset(e, 0x3f, sizeof(e));
  for(int i = 0;i < k;i++)
  {
    char tmpP1[6], tmpP2[6];
    int a, b, tmpDist;
    scanf("%s %s %d", tmpP1, tmpP2, &tmpDist);
    //printf("%s %s %d\n", tmpP1, tmpP2, tmpDist);
    if(tmpP1[0] == 'G')
      a = n + (int)atof(tmpP1+1);
    else
      a = (int)atof(tmpP1);
    if(tmpP2[0] == 'G')
      b = n + (int)atof(tmpP2+1);
    else
      b = (int)atof(tmpP2);
    e[a][b] = e[b][a] = tmpDist;
    //printf("a = %d, b = %d\n", a, b);
  }
  //solve:
  int resultID;
  double resultMaxDist = -1, resultAver;
  for(int i = n+1;i <= n+m;i++)
  {
    double minDist = INF, aver = 0;
    memset(dist, 0x3f, sizeof(dist));
    memset(visited, 0, sizeof(visited));
    dist[i] = 0;
    while(1)
    {
      int u, minD = INF;
      for(int j = 1;j <= n+m;j++)
      {
        if(visited[j] == 0 && dist[j] < minD)
        {
          minD = dist[j];
          u = j;
        }
      }
      visited[u] = 1;
      if(minD == INF)
        break;
      for(int j = 1;j <= n+m;j++)
      {
        if(visited[j] == 0 && dist[j] > dist[u] + e[u][j])
          dist[j] = dist[u] + e[u][j];
      }
    }
    for(int j = 1;j <= n;j++)//注意j的范围,因为是到每个住宅区的距离,所以最大是n
    {
      if(dist[j] > d)//超过了能服务的范围
      {
        minDist = -1;
        break;
      }
      if(dist[j] < minDist)
        minDist = dist[j];
      aver += dist[j] * 1.0;
    }
    if(minDist == -1)
      continue;
    aver = aver / n;
    //printf("aver = %.1lf\n", aver);
    if(minDist > resultMaxDist)//因为从序号小的先开始计算和保存结果,所以保证了最后留下来的一定是序号小的,因此不必特殊判断一下了
    {
      //printf("minDist = %.1lf, resultMaxDist = %.1lf\n", minDist, resultMaxDist);
      resultMaxDist = minDist;
      resultID = i;
      resultAver = aver;
    }
    else if(minDist == resultMaxDist && aver < resultAver)
    {
      resultID = i;
      resultAver = aver;
    }
  }
  if(resultMaxDist == -1)
    printf("No Solution\n");
  else
    printf("G%d\n%.1lf %.1lf\n", resultID-n, resultMaxDist, resultAver);
}

-FIN-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值