zoukankan      html  css  js  c++  java
  • Kitchen Measurements UVALive

    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(" "); } 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; }
  • 相关阅读:
    20155339 Exp9 Web安全基础
    20155339 Exp8 Web基础
    20155339 Exp7 网络欺诈防范
    20155339 Exp6 信息搜集与漏洞扫描
    20155339 Exp5 MSF基础应用
    20155339 Exp4 恶意代码分析
    20155339 Exp3 免杀原理与实践
    20155339平措卓玛 Exp2 后门原理与实践
    20155339平措卓玛 Exp1 PC平台逆向破解(5)M
    20155339 第16周课堂实践加分作业
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7499316.html
Copyright © 2011-2022 走看看