Kitchen Measurements UVALive - 7354

https://wenku.baidu.com/view/9b4a1bd476a20029bd642dd4.html
//这道题学了很多东西- -
#include <iostream> #include <algorithm> #include <stdio.h> #include <set> #include <queue> #include <cstring> #include <string> #include <limits.h> #include <string.h> #include <vector> #include <map> #define LL long long #define INF 2100000000 #define fi first #define se second #define lowbit(x) (x&(-x)) #define eps 5e-7 using namespace std; const int maxn=(int)5010; const int maxm=(int)2510; const LL MOD=(LL)1e9+7; int n,meow; int v[10]; int vis[61*65*65*65+3*65*65+10]; //标记这个的时候 设a[0]权值最小 a[4]权值最大 那么最大的数组为 0 0 3 61 0 只需要hash前4位 第5位因为总和一样所以是确定的 //map TLE unordMAP 1500ms 数组 760ms
#define base 65 struct nod{ int a[5]; int val; nod(int b[5],int val=0):val(val){for(int i=0;i<n;i++)a[i]=b[i];} bool operator <(const nod&a)const{return val>a.val;} void copyy(int b[5]){for(int i=0;i<n;i++) b[i]=a[i];} }; int gethash(int a[]){ int s=0,b=1; for(int i=0;i<n-1;i++)s+=a[i]*b,b*=base; return s; } void out(int a[]){ for(int i=0;i<n;i++)printf("%d ",a[i]);printf("\n"); } int bfs(){ int s[5]={0}; priority_queue<nod> q; //使用队列会导致遍历所有情况 取ans最小值 优先队列会快一些 s[0]=v[0];vis[gethash(s)]=-1; q.push(nod(s,0)); while(!q.empty()){ nod t=q.top();q.pop(); if(t.a[0]==meow) return t.val; t.copyy(s); // out(s); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ if(i==j||!s[i]&&!s[j])continue; int w=min(s[i],v[j]-s[j]); s[i]-=w;s[j]+=w; int ha=gethash(s); if(!vis[ha]||vis[ha]&&vis[ha]>t.val+w){ q.push(nod(s,t.val+w)); vis[ha]=t.val+w; } s[i]+=w;s[j]-=w; } } return -1; } int main() { #ifdef shuaishuai freopen("C:\\Users\\hasee\\Desktop\\a.txt","r",stdin); //freopen("C:\\Users\\hasee\\Desktop\\b.txt","w",stdout); #endif while(~scanf("%d",&n)){ for(int i=0;i<n;i++)scanf("%d",v+i); scanf("%d",&meow); memset(vis,0,sizeof vis); int ans=bfs(); if(ans!=-1)cout<<ans<<endl; else cout<<"impossible"<<endl; } return 0; }

 

转载于:https://www.cnblogs.com/MeowMeowMeow/p/7499316.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值