USACO 2010 FEB Silver题解

来源:互联网 发布:web编程博客设计 编辑:程序博客网 时间:2024/04/29 21:50

P1:Tea Time

     水题,并查集暴力水之

P2:Buying Feed

     贪心,每个商店是独立的,每个商店设个权值,为在此商店买一份食物并运到终点所需的钱(价格+运费)

P3:Chesse Towers

     DP背包,分两种情况,一个是没有大奶酪的,一个是有大奶酪的

/*Name: tea timeAuthor: yyxDate: 03/02/14 21:57Source: USACO 2010 JAN SilverAlgorithm: Data Structure: union-find-set*/#include <cstdio>#include <cctype>#include <iostream>#include <algorithm>#include "quickin.h"using namespace std;const int N_MAX=1000+10,M_MAX=2000+10;int N,M,Q;class Tufs{int fa[N_MAX];int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}public:void init(int size){for (int i=0; i<size; ++i) fa[i]=i;}void con(int u,int v){int fa1=find(u),fa2=find(v);if (fa1==fa2) return;fa[fa1]=fa2;}bool same(int u,int v){int fa1=find(u),fa2=find(v);if (fa1==fa2) return 1;return 0;}}set;namespace Ninit{void init(){set.init(N=READ.Int());M=READ.Int();Q=READ.Int();for (int i=0; i<M; ++i){int u=READ.Int()-1,v=READ.Int()-1;set.con(u,v);}}}namespace Nwork{void main(){for (int i=0; i<Q; ++i){int u=READ.Int()-1,v=READ.Int()-1;puts(set.same(u,v)?"Y":"N");}}}int main(){freopen("data.in","r",stdin);freopen("data.out","w",stdout);READ.Init();Ninit::init();Nwork::main();return 0;}

/*Name: buying feed Author: yyxDate: 03/02/14 22:44Source: USACO 2010 JAN Silver Algorithm: greedyData Structure: */#include <cstdio>#include <cctype>#include <algorithm>using namespace std;class DREAD{    friend int fread();    static const int buff_max=20000000+10;    char buf[buff_max],*pt;    void clear(){ for(; isspace(*pt); ++pt); }    public:        void Init(){ buf[fread(buf,1,buff_max,stdin)]=EOF;pt=buf; }        bool eoln(){ return *pt=='\n'; }        inline bool eof(){ return *pt==EOF; }        int Int(){            bool pos=1;int res=0;            for (; !isdigit(*pt) && *pt!='-' && !eof(); ++pt);if (*pt=='-') pos=0,++pt;            for (; isdigit(*pt); ++pt) res=res*10+(*pt-'0');            return pos?res:-res;        }        long long LL(){            bool pos=1;long long res=0;            for (; !isdigit(*pt) && *pt!='-' && !eof(); ++pt);if (*pt=='-') pos=0,++pt;            for (; isdigit(*pt); ++pt) res=res*10+(*pt-'0');            return pos?res:-res;        }        double Double(){            bool pos=1;double res=0,val=0.1;            for (; !isdigit(*pt) && *pt!='-' && !eof(); ++pt);if (*pt=='-') pos=0,++pt;            for (; isdigit(*pt); ++pt) res=res*10+(*pt-'0');            if (*pt=='.')                for (++pt; isdigit(*pt) && !eof(); val/=10,++pt) res+=val*(*pt-'0');            return pos?res:-res;        }        char Char(){ clear();return *pt++; }        char Next(){ return *pt++; }        char Now(){ return *pt; }}READ;const int N_MAX=100+10;int N,M,K;typedef pair<int,int> PII;#define MP make_pairPII a[N_MAX];namespace Ninit{void init(){K=READ.Int();M=READ.Int();N=READ.Int();for (int i=0; i<N; ++i){int p=READ.Int(),f=READ.Int(),c=READ.Int();a[i]=MP(c+M-p,f);}}}namespace Nwork{void main(){sort(a,a+N);int ans=0;for (int i=0; i<M; ++i){int tmp=min(K,a[i].second);K-=tmp;ans+=a[i].first*tmp;if (!K) break;}printf("%d\n", ans);}}int main(){freopen("data.in","r",stdin);freopen("data.out","w",stdout);READ.Init();Ninit::init();Nwork::main();return 0;}
/*Name: cheese towersAuthor: yyxDate: 04/02/14 21:06Source: USACO 2010 JAN SilverAlgorithm: DP背包 Data Structure: */#include <cstdio>#include <cctype>#include <cstring>#include "quickin.h"using namespace std;#define max(a,b) ((a)>(b)?(a):(b))const int N_MAX=100+10,M_MAX=1000+10,inf=~0u>>1;int N,M,K,h[N_MAX],v[N_MAX];namespace Ninit{void init(){N=READ.Int();M=READ.Int();K=READ.Int();for (int i=1; i<=N; ++i)v[i]=READ.Int(),h[i]=READ.Int();}}namespace Nwork{int f[M_MAX],g[M_MAX]; void main(){memset(f,0xff,sizeof(f));memset(g,0xff,sizeof(g));f[0]=0;for (int i=1; i<=N; ++i)if (h[i]<K)for (int j=h[i]; j<=M; ++j)if (f[j-h[i]]>=0)f[j]=max(f[j],f[j-h[i]]+v[i]);for (int i=1; i<=N; ++i){if (h[i]>=K) g[h[i]]=max(g[h[i]],v[i]);h[i]=h[i]*4/5;}for (int i=1; i<=N; ++i)for (int j=h[i]; j<=M; ++j)if (g[j-h[i]]>=0)g[j]=max(g[j],g[j-h[i]]+v[i]);int ans=0;for (int i=0; i<=M; ++i)ans=max(ans,max(g[i],f[i]));printf("%d\n", ans);}}int main(){freopen("data.in","r",stdin);freopen("data.out","w",stdout);READ.Init();Ninit::init();Nwork::main();return 0;}



0 0
原创粉丝点击