USACO 2010 FEB Silver题解

P1:Tea Time

     水题,并查集暴力水之

P2:Buying Feed

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

P3:Chesse Towers

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

/*
	Name: tea time
	Author: yyx
	Date: 03/02/14 21:57
	Source: USACO 2010 JAN Silver
	Algorithm: 
	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: yyx
	Date: 03/02/14 22:44
	Source: USACO 2010 JAN Silver 
	Algorithm: greedy
	Data 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_pair
PII 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 towers
	Author: yyx
	Date: 04/02/14 21:06
	Source: USACO 2010 JAN Silver
	Algorithm: 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值