201409-4 最优配餐 100

http://118.190.20.162/view.page?gpid=T13
运行超时 10分

//#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<map>
using namespace std;
int n,m,k,d;
//0表示通路
//-1表示障碍
//1表示站点 
int a[1001][1001];
int movex[]={0,0,-1,1};
int movey[]={-1,1,0,0};
bool visit[1001][1001];
struct Point{
	int xx, yy;
	bool operator < (const Point &a) const{
		return xx<a.xx?true :yy<a.yy? true: false;
	}
}p;
struct lev{
	Point pt;
	int level;
};
bool In(int x,int y){
	if(x>=1&&x<=n&&y>=1&&y<=n)
	return true;
	else return false;
}
void v(Point a){
	visit[a.xx][a.yy]=true;
}
long long  least(int cx,int cy,int c){
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++)
		    visit[i][j]=false;
	}
	queue <lev> q;
	lev pc,pre;
	pc.pt.xx=cx;
	pc.pt.yy=cy;
	pc.level=0;
	v(pc.pt);
	q.push(pc); 
	
	long long re=1;
	while(!q.empty()){
		pre=q.front();
		q.pop();
		int tx,ty;
		for(int mov=0;mov<4;mov++){
			tx=pre.pt.xx+movex[mov];
			ty=pre.pt.yy+movey[mov];
			if( In(tx,ty) &&visit[tx][ty]==false ){
				if(a[tx][ty]==1){
					re=pre.level+1;
					re*=c;
					return re;
				}
				else if( a[tx][ty]==-1 ){
					continue;
				}
				else{
					pc.pt.xx=tx;
					pc.pt.yy=ty;
					pc.level=pre.level+1;
					q.push(pc);
				}
			}
		}
	}
	return re;
}
int main(){
	map <Point,int> client;
	cin>>n>>m>>k>>d;
	int x,y,c;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++)
		    a[i][j]=0;
	}
	for(int i=0;i<m;i++){
		cin>>x>>y;
		a[x][y]=1;
	}
	for(int i=0;i<k;i++){
		cin>>x>>y>>c;
		p.xx=x;p.yy=y;
		client[p] = client[p]+c;
	}
	for(int i=0;i<d;i++){
		cin>>x>>y;
		a[x][y]=-1;
	}
	long long sum=0;
	for( map <Point,int>::iterator it=client.begin();it!=client.end();it++){
		sum+=least(it->first.xx,it->first.yy,it->second);
		//cout<<it->first.xx<<" "<<it->first.yy<<" "<<it->second<<endl;
	}
	cout<<sum;
    return 0;
}

运行超时 60分

//#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<map>
using namespace std;
int n,m,k,d;
//0表示通路
//-1表示障碍
//1表示站点 
//2表示顾客 
struct Point{
	int xx, yy;
	bool operator < (const Point &a) const{
		return this->xx<a.xx?true :this->yy<a.yy? true: false;
	}
}p;
struct lev{
	Point pt;
	int level;
};
int a[1001][1001];
int movex[]={0,0,-1,1};
int movey[]={-1,1,0,0};
bool visit[1001][1001];
queue <lev> q;
map <Point,int> client;
bool In(int x,int y){
	if(x>=1&&x<=n&&y>=1&&y<=n)
	return true;
	else return false;
}
void v(Point a){
	visit[a.xx][a.yy]=true;
}
long long  bfs(){
	lev pc,pre;
	long long re=0;
	int cnt=0;
	while(!q.empty()){
		pre=q.front();
		q.pop();
		int tx,ty;
		for(int mov=0;mov<4;mov++){
			tx=pre.pt.xx+movex[mov];
			ty=pre.pt.yy+movey[mov];
			if( In(tx,ty) && visit[tx][ty]==false ){
				if(a[tx][ty]==1||a[tx][ty]==-1){
					continue;
				}
				else{
					pc.pt.xx=tx;
					pc.pt.yy=ty;
					pc.level=pre.level+1;
					v(pc.pt);
					q.push(pc);
				}
				if( a[tx][ty]==2 ){
					p.xx=tx;
					p.yy=ty;
					cnt++;
					//cout<<"point:"<<tx<<" "<<ty<<endl; 
					map<Point,int>::iterator it;
					it=client.find(p);
					int mul=-1;
					for(it=client.begin();it!=client.end();it++){
						if(it->first.xx==tx&&it->first.yy==ty)
						mul=it->second;
					}
					re += (pre.level+1)*mul;
					//cout<<"c:"<<mul<<endl;
					if(cnt == client.size() ){
							return re;
					}
				
				}
			}
		}
	}
	return 5;
}
int main(){

	cin>>n>>m>>k>>d;
	int x,y,c;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++)
		    a[i][j]=0;
	}
	lev fendian;
	for(int i=0;i<m;i++){
		cin>>x>>y;
		a[x][y]=1;
		fendian.pt.xx=x;
		fendian.pt.yy=y;
		fendian.level=0;
		q.push(fendian);
		v(fendian.pt);
	}
	for(int i=0;i<k;i++){
		cin>>x>>y>>c;
		p.xx=x;p.yy=y;
		if(client.find(p) == client.end()){
			client.insert(pair<Point,int>(p,c));
		}
		else {
			client[p] += c;
		}
		a[x][y]=2; 
	}
	
	for(int i=0;i<d;i++){
		cin>>x>>y;
		a[x][y]=-1;
	}
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++)
		    visit[i][j]=false;
	}
	long long sum=0;
	sum=bfs();
	cout<<sum;
    return 0;
}

100

//#include<bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<map>
using namespace std;
int n,m,k,d;
//0表示通路
//-1表示障碍
//-2表示站点 
//>0表示顾客 
struct Point{
	int xx, yy,cost;
	bool operator < (const Point &a) const{
		return this->xx<a.xx?true :this->yy<a.yy? true: false;
	}
}p;
struct lev{
	Point pt;
	int level;
};
int a[1001][1001];
int movex[]={0,0,-1,1};
int movey[]={-1,1,0,0};
bool visit[1001][1001];
queue <lev> q;
map <Point,int> client;
bool In(int x,int y){
	if(x>=1&&x<=n&&y>=1&&y<=n)
	return true;
	else return false;
}
void v(Point a){
	visit[a.xx][a.yy]=true;
}
long long  bfs(){
	lev pc,pre;
	long long re=0;
	int cnt=0;
	while(!q.empty()){
		pre=q.front();
		q.pop();
		int tx,ty;
		for(int mov=0;mov<4;mov++){
			tx=pre.pt.xx+movex[mov];
			ty=pre.pt.yy+movey[mov];
			if( In(tx,ty) && visit[tx][ty]==false ){
				if(a[tx][ty]==-2||a[tx][ty]==-1){
					continue;
				}
				else{
					pc.pt.xx=tx;
					pc.pt.yy=ty;
					pc.level=pre.level+1;
					v(pc.pt);
					q.push(pc);
				}
				if( a[tx][ty]>0 ){
					p.xx=tx;
					p.yy=ty;
					cnt++;
					re += (pre.level+1)*a[tx][ty];
					if( cnt ==k ){
					    return re;
					}
				}
			}
		}
	}
	return 5;
}
int main(){
	scanf("%d%d%d%d",&n,&m,&k,&d);
	int x,y,c;
	for(int i=0;i<1001;i++){
		for(int j=0;j<1001;j++){
			visit[i][j]=false;
			a[i][j]=0;
		}
	}
	lev fendian;
	for(int i=0;i<m;i++){
		scanf("%d%d",&x,&y);
		a[x][y]=-2;
		fendian.pt.xx=x;
		fendian.pt.yy=y;
		fendian.level=0;
		q.push(fendian);
		v(fendian.pt);
	}
	for(int i=0;i<k;i++){
	    scanf("%d%d%d",&x,&y,&c);
	    //if(a[x][y]>=0)
		   a[x][y]+=c; 
	}
	for(int i=0;i<d;i++){
		scanf("%d%d",&x,&y);
		a[x][y]=-1;
	}
	long long sum=0;
	sum=bfs();
	printf("%lld",sum);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值