2426: [HAOI2010]工厂选址
代码:
1 /* 2 贪心: 3 奇妙!!!!! 4 因为所有的煤矿不是给新厂,就是给旧厂(而且旧厂的得到b) 5 为了使费用最小,感性的理解,那么一个煤矿给哪个厂,取决于到哪个厂近。 6 所以开始假设所有的煤都给了新厂,然后取出b的给旧厂,取哪个煤矿呢? 7 当然是这个煤矿给了旧厂的费用比给了新厂的费用少(c[0][i]-c[x][i]),所以按照这个排序,依次取出。 8 */ 9 #include<bits/stdc++.h> 10 using namespace std; 11 typedef long long LL; 12 13 inline int read() { 14 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 15 for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 16 } 17 18 #define rep(i,a,b) for(int i=(a); i<=(b); ++i) 19 #define pa pair<int,int> 20 const int M = 50010; // 煤矿 21 const int N = 55; // 发电厂 22 23 int a[M],c[N][M],h[N]; 24 int m,b,n,ans1,ans2 = 1e9; 25 pa T[M]; 26 27 void solve(int x) { 28 int Cost = h[0] + h[x], B = b; 29 rep (i,1,m) { 30 Cost += c[x][i] * a[i]; 31 T[i].first = (c[0][i]-c[x][i]); 32 T[i].second = i; 33 } 34 sort(T+1,T+m+1); 35 rep (i,1,m) { //--m,1 36 int j = T[i].second; 37 if (B >= a[j]) { 38 Cost += T[i].first * a[j]; // -- Cost-=... 39 B -= a[j]; 40 } 41 else { 42 Cost += T[i].first * B; 43 break; 44 } 45 } 46 if (Cost < ans2) 47 ans2 = Cost,ans1 = x; 48 } 49 50 int main() { 51 m = read(),b = read(),h[0] = read(),n = read(); 52 rep (i,1,m) a[i] = read(); 53 rep (i,1,n) h[i] = read(); 54 rep (i,0,n) rep (j,1,m) c[i][j] = read(); // -- rep(i,1,n) 55 rep (i,1,n) solve(i); // -- rep(i,0,n) 56 cout << ans1 << " " << ans2; 57 return 0; 58 }