201409-4 最优配餐 100
2019-08-14 17:48
99 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43522854/article/details/99573521
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]运行超时 60分= 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; }
//#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]100+= 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; }
//#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; }
相关文章推荐
- ccf 201409-4 最优配餐
- CCF CSP 201409-4 最优配餐
- ccf 201409-4 最优配餐
- 201409-4 最优配餐
- CCF 201409-4 最优配餐
- CCF 201409-4 最优配餐(bfs)
- ccf 201409-4 最优配餐 (90分待更新) bfs
- CCF CSP 最优配餐 JAVA 201409_4 90分
- CCF考试——201409-4最优配餐
- (CCF 201409-4)最优配餐 [简单多源点BFS]
- CCF 201409-4 最优配餐(BFS)
- 201409-4 最优配餐 ccf
- CCF 201409-4 最优配餐
- CCF-201409-4 最优配餐(BFS)
- CCF 201409-4 最优配餐
- CCF-CSP 201409-4 最优配餐(BFS)
- CCF——最优配餐
- 最优配餐-CCF
- CCF201409-4 最优配餐(100分)
- 最优配餐 广度优先搜索