标签:des style blog http color io os ar for
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 859 | Accepted: 280 |
Description
Input
Output
Sample Input
2 2 1 3 1 3 5 2 10 2 4 1 1 1 0 1 4
Sample Output
9 Impossible
Source
1 /* 2 * Author: Joshua 3 * Created Time: 2014年10月06日 星期一 14时25分35秒 4 * File Name: poj2901.cpp 5 */ 6 #include<cstdio> 7 #include<algorithm> 8 #include<cstring> 9 #include<iostream> 10 using namespace std; 11 #define maxn 515 12 #define inf 0x3f3f3f3f 13 typedef long long LL; 14 int T,n,m,r,c,ans; 15 int b[maxn],p[maxn],f[maxn][maxn]; 16 17 void init() 18 { 19 scanf("%d%d%d%d",&n,&m,&r,&c); 20 for (int i=1;i<=r;++i) 21 scanf("%d%d",&b[i],&p[i]); 22 } 23 24 void updata(int&x ,int y) 25 { 26 if (y<x) x=y; 27 } 28 29 void solve() 30 { 31 int tb,tp,tans,tk; 32 ans=inf; 33 for (int i=0;i<=n+10;++i) 34 memset(f[i],0x3f,(m+10)<<2); 35 f[0][0]=0; 36 for (int i=1;i<=r;++i) 37 { 38 tb=b[i];tp=p[i]; 39 for (int t1=n+5;t1>=0;t1--) 40 for (int t2=m+5;t2>=0;t2--) 41 { 42 if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp); 43 if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp); 44 } 45 } 46 for (int i=n;i<=n+5;++i) 47 for (int j=m;j<=m+5;++j) 48 ans=min(ans,f[i][j]); 49 if (c%2==0) return; 50 tk=0; 51 b[0]=6;p[0]=inf; 52 for (int i=1;i<=r;++i) 53 if (b[i]>=2 && (p[i]<=p[tk] || (p[i]==p[tk] && b[i]<b[tk]))) 54 tk=i; 55 if (!tk) return; 56 n--;m--; 57 for (int i=0;i<=n+10;++i) 58 memset(f[i],0x3f,(m+10)<<2); 59 f[0][0]=0; 60 for (int i=1;i<=r;++i) 61 { 62 if (i==tk) continue; 63 tb=b[i];tp=p[i]; 64 for (int t1=n+5;t1>=0;t1--) 65 for (int t2=m+5;t2>=0;t2--) 66 { 67 if (tb<=t1) updata(f[t1][t2],f[t1-tb][t2]+tp); 68 if (tb<=t2) updata(f[t1][t2],f[t1][t2-tb]+tp); 69 } 70 } 71 for (int i=n;i<=n+5;++i) 72 for (int j=m;j<=m+5;++j) 73 ans=min(ans,f[i][j]+p[tk]); 74 } 75 76 int main() 77 { 78 scanf("%d",&T); 79 for (int i=1;i<=T;++i) 80 { 81 init(); 82 solve(); 83 if (ans!=inf) printf("%d\n",ans); 84 else printf("Impossible\n"); 85 } 86 return 0; 87 }
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/code-painter/p/4009545.html