题目链接http://118.190.20.162/view.page?gpid=T13
试题名称: | 最优配餐 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务。随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题。 输入格式 输入的第一行包含四个整数n, m, k, d,分别表示方格图的大小、栋栋的分店数量、客户的数量,以及不能经过的点的数量。 输出格式 输出一个整数,表示最优送餐方式下所需要花费的成本。 样例输入 10 2 3 3 样例输出 29 评测用例规模与约定 前30%的评测用例满足:1<=n <=20。 |
总结一下自己出过的问题 1.没有仔细观察题目里的图 所以认为x=0或者y=0的边界存在 2 在bfs时忘记队列的长度应该不变 3 没能想到这题的数据可能超过int范围 4 没想到vector用起来比数组慢很多,现在才明白为什么这么多人平时不用vector喜欢用数组
大概就这几个问题。我估计我9月16号的ccf是考不了多少分了,当初本来也没打算考多少,就当体验生活吧。
代码如下:
#include <iostream>
#include<queue>
using namespace std;
int n,m,k,d;
int dis[1001][1001];
bool map[1001][1001];
bool visted[1001][1001];
struct loc{
int x;
int y;
loc(int xx,int yy)
{
x=xx;
y=yy;
}
};
struct dingdan{
int x;
int y;
int count;
dingdan(int xx,int yy,int c)
{
x=xx;
y=yy;
count=c;
}
};
int main(int argc, char** argv) {
cin>>n>>m>>k>>d;
queue<loc> q;
vector<dingdan> dingdans;
int temp_x,temp_y,temp_count;
for(int i=0;i<m;++i)
{
cin>>temp_x>>temp_y;
q.push(loc(temp_x,temp_y));//把多个商铺作为bfs的起点
}
for(int i=0;i<k;++i)
{
cin>>temp_x>>temp_y>>temp_count;
dingdans.push_back(dingdan(temp_x,temp_y,temp_count));
}
for(int i=0;i<d;++i)
{
cin>>temp_x>>temp_y;
map[temp_x][temp_y]=1;
}
int step=0;
while(!q.empty())
{
int s=q.size();//进行bfs 注意这里s一定要在循环外先赋值
for(int j=0;j<s;++j)
{
loc temp_loc=q.front();//yuwencong i miss you
q.pop();
int x=temp_loc.x;
int y=temp_loc.y;
visted[x][y]=true;
dis[x][y]=step;
if(x+1<=n&&!visted[x+1][y]&&!map[x+1][y])
{
q.push(loc(x+1,y));
visted[x+1][y]=true;
}
if(y+1<=n&&!visted[x][y+1]&&!map[x][y+1])
{
q.push(loc(x,y+1));
visted[x][y+1]=true;
}
if(x-1>0&&!visted[x-1][y]&&!map[x-1][y])
{
q.push(loc(x-1,y));
visted[x-1][y]=true;
}
if(y-1>0&&!visted[x][y-1]&&!map[x][y-1])
{
q.push(loc(x,y-1));
visted[x][y-1]=true;
}
}
step++;
}
long long res=0;
for(int i=0;i<dingdans.size();++i)
{
res+=dis[dingdans[i].x][dingdans[i].y]*dingdans[i].count;
}
cout<<res;
return 0;
}