UVA10603Fill

来源:互联网 发布:淘宝美工是干嘛的 编辑:程序博客网 时间:2024/06/05 23:11
#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<queue>using namespace std;const int MAXN = 210;int a, b, c, d;struct Node{int v[3], dis;bool operator < (const Node& rhs) const {return dis > rhs.dis;}};int vis[MAXN][MAXN];int cup[3];int ans[MAXN];void update_ans(const Node& n) {for(int i = 0; i < 3; i++) {int d = n.v[i];if(ans[d] < 0 || ans[d] > n.dis) ans[d] = n.dis; }}void solve(int a, int b, int c, int d) {Node n0;cup[0] = a; cup[1] = b; cup[2] = c;n0.v[0] = n0.v[1] = 0; n0.v[2] = c; n0.dis = 0;update_ans(n0);priority_queue<Node> q;q.push(n0);vis[0][0] = 1;    while(!q.empty()) {    if(ans[d] >= 0) break;    Node n = q.top(); q.pop();    update_ans(n);    for(int i = 0; i < 3; i++) {    for(int j = 0; j < 3; j++) {    if(i != j) {    if(n.v[i] == 0 || n.v[j] == cup[j]) continue;    int amount = min(cup[j] - n.v[j], n.v[i]);    Node n1;    memcpy(&n1, &n, sizeof(n));n1.v[i] -= amount;n1.v[j] += amount;if(vis[n1.v[0]][n1.v[1]]) continue;vis[n1.v[0]][n1.v[1]] = true;n1.dis = n.dis + amount;q.push(n1);  }}}}while(d >= 0) {if(ans[d] >= 0) { printf("%d %d\n", ans[d], d); return ;}    d--;}}int main() {int T;scanf("%d", &T);while(T--) {memset(ans, -1, sizeof(ans));memset(vis, 0, sizeof(vis));scanf("%d%d%d%d", &a, &b, &c, &d);solve(a, b, c, d);}return 0;}/*22 3 4 296 97 199 62*/

原创粉丝点击