• csp20140904最优配餐_Solution


    Solution

    经典bfs,所有的点到店的最短距离

    其中一开始队列的长度为店的数目

    一个点可能有多个订单

    关于数据大小:

    1.
    1000*(1000*1000)*2000=2,0000,0000,0000
    订餐量*客户的数量*距离
    总数用__int64
    2.
    1000*(1000*1000)=10,0000,0000
    订餐量*客户的数量
    一个数总数用long

    Code:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <stdbool.h>
     4 #define maxn 1000
     5 
     6 /*
     7 1.
     8 1000*(1000*1000)*2000=2,0000,0000,0000
     9 订餐量*客户的数量*距离
    10 总数用__int64
    11 2.
    12 1000*(1000*1000)=10,0000,0000
    13 订餐量*客户的数量
    14 一个数总数用long
    15 */
    16 
    17 struct node
    18 {
    19     long x,y;
    20 };
    21 
    22 long dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
    23 long amount[maxn+1][maxn+1],dist[maxn+1][maxn+1];
    24 __int64 ans=0;
    25 struct node que[maxn*maxn+1];
    26 
    27 int main()
    28 {
    29     long head,tail,n,m,k,d,s,i,j,x,y,xx,yy;
    30     scanf("%ld%ld%ld%ld",&n,&m,&k,&d);
    31     head=0;
    32     tail=0;
    33     for (i=1;i<=n;i++)
    34         for (j=1;j<=n;j++)
    35             dist[i][j]=-2;
    36     for (i=1;i<=m;i++)
    37     {
    38         scanf("%ld%ld",&x,&y);
    39         dist[x][y]=0;
    40         tail++;
    41         que[tail].x=x;
    42         que[tail].y=y;
    43     }
    44     for (i=1;i<=n;i++)
    45         for (j=1;j<=n;j++)
    46             amount[i][j]=0;
    47     for (i=1;i<=k;i++)
    48     {
    49         scanf("%ld%ld%ld",&x,&y,&s);
    50         amount[x][y]+=s;
    51     }
    52     for (i=1;i<=d;i++)
    53     {
    54         scanf("%ld%ld",&x,&y);
    55         dist[x][y]=-1;
    56     }
    57     while (head<tail)
    58     {
    59         head++;
    60         for (i=0;i<4;i++)
    61         {
    62             xx=que[head].x+dx[i];
    63             yy=que[head].y+dy[i];
    64             if (dist[xx][yy]==-2)
    65             {
    66                 dist[xx][yy]=dist[que[head].x][que[head].y]+1;
    67                 tail++;
    68                 que[tail].x=xx;
    69                 que[tail].y=yy;
    70             }
    71         }
    72     }
    73     for (i=1;i<=n;i++)
    74         for (j=1;j<=n;j++)
    75             ans+=amount[i][j]*dist[i][j];
    76     printf("%I64d
    ",ans);
    77     return 0;
    78 }
  • 相关阅读:
    《构建之法》心得体会
    简单工厂模式加减乘除器
    个人简介
    单元测试和代码覆盖率工具的使用
    Bookstore系统缺陷报告
    《构建之法读后感》
    3137102432_ 施少兵_ lab3
    3137102432_施少兵_实验2
    个人简介
    第六次作业:购物系统缺陷
  • 原文地址:https://www.cnblogs.com/cmyg/p/6863990.html
Copyright © 2020-2023  润新知