• Uva-10603 Fill


      1 #include<bits/stdc++.h>
      2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
      3 #pragma GCC optimize(2)
      4 
      5 using namespace std;
      6 
      7 struct Node
      8 {
      9     int v[3],dist,fa;
     10     int from,to;
     11 };
     12 
     13 const int maxn = 200 + 5;
     14 int vis[maxn][maxn],cap[3],ans[maxn];
     15 vector<Node> nodes;
     16 
     17 void update_ans(const Node& u)
     18 {
     19     _for(i,0,3)
     20     {
     21         int d = u.v[i];
     22         if(ans[d] < 0 || u.dist < ans[d]) ans[d] = u.dist;
     23     }
     24 }
     25 
     26 struct cmp
     27 {
     28     bool operator () (const int a,const int b) const
     29     {
     30         return nodes[a].dist > nodes[b].dist;
     31     }
     32 };
     33 
     34 int final = 0;
     35 void solve(int a,int b,int c, int &d)
     36 {
     37     cap[0] = a,cap[1] = b,cap[2] = c;
     38     memset(vis,0,sizeof(vis));
     39     memset(ans,-1,sizeof(ans));
     40     nodes.clear();
     41     priority_queue<int,vector<int>,cmp> q;
     42     
     43     Node start;
     44     start.dist = 0;
     45     //c is full in the begining
     46     start.v[0] = 0,start.v[1] = 0,start.v[2] = c,start.fa = 0;
     47     start.from = start.to = -1;
     48     q.push(0);
     49     nodes.push_back(start);
     50     
     51     vis[0][0] = 1;
     52     while(!q.empty())
     53     {
     54         int u = q.top();q.pop();
     55         update_ans(nodes[u]);
     56         if(ans[d] >= 0)    {break;}
     57         _for(i,0,3)// i.water -> j.water
     58         {
     59             _for(j,0,3)
     60             {
     61                 // i is j or i is no-water or j is full
     62                 if(i==j || nodes[u].v[i]==0 || nodes[u].v[j] == cap[j]) continue;
     63                 //all of i in or a part of i in
     64                 int amount = min(cap[j],nodes[u].v[i]+nodes[u].v[j]) - nodes[u].v[j];
     65                 Node u2;
     66                 memcpy(&u2,&nodes[u],sizeof(Node));
     67                 u2.dist = nodes[u].dist+amount;
     68                 u2.v[i] -= amount;
     69                 u2.v[j] += amount;
     70                 u2.from = i;
     71                 u2.to = j;
     72                 if(!vis[u2.v[0]][u2.v[1]])
     73                 {
     74                     vis[u2.v[0]][u2.v[1]] = 1;
     75                     u2.fa = u;
     76                     nodes.push_back(u2);
     77                     q.push(nodes.size()-1);
     78                 }
     79             }
     80         }
     81     }
     82     while(d >= 0)
     83     {
     84         if(ans[d]>=0)
     85         {
     86             cout << "minimum water & goal" << endl;
     87             printf("%d %d
    
    ",ans[d],d);
     88             return ;
     89         }
     90         d --;
     91     }
     92 }
     93 
     94 void print_ans(int d)
     95 {
     96     int start;
     97     _for(i,0,nodes.size())
     98     {
     99         if(ans[d]==nodes[i].dist) {start = i;break;}
    100     }
    101     vector<vector<int>> rnt;
    102     vector<int> tmp;
    103     while(start)
    104     {
    105         tmp.clear();
    106         tmp.push_back(nodes[start].v[0]);
    107         tmp.push_back(nodes[start].v[1]);
    108         tmp.push_back(nodes[start].v[2]);
    109         tmp.push_back(nodes[start].from+1);
    110         tmp.push_back(nodes[start].to+1);
    111         rnt.push_back(tmp);
    112         start = nodes[start].fa;
    113     }
    114     cout << "Begin:" << endl;
    115     cout << nodes[0].v[0] << " " << nodes[0].v[1] << " " << nodes[0].v[2] << endl << endl;
    116     for(int i = rnt.size()-1;i >= 0;i --)
    117     {
    118         cout << rnt[i][3] << "th -> " << rnt[i][4] << "th" << endl;
    119         for(int j = 0;j < 3;j ++)
    120         {
    121             cout << rnt[i][j] << " ";
    122         }
    123         cout << endl << endl;
    124     }
    125     cout << "Question solved" << endl << endl << endl << endl;
    126     cout << "**********" << endl;
    127 }
    128 
    129 int main()
    130 {
    131 //    freopen("Miku.txt","w",stdout);
    132     int T,a,b,c,d;
    133     scanf("%d",&T);
    134     while(T --)
    135     {
    136         scanf("%d%d%d%d",&a,&b,&c,&d);
    137         solve(a,b,c,d);
    138         print_ans(d);
    139     }
    140     return 0;
    141 }

    可以打印路径,基本上照抄刘汝佳

  • 相关阅读:
    Linux crontab 命令格式与举例
    my sql 两个 索引 时的 union 与 or 的比较
    网络通信5层传输
    算法 韩信点兵 循环左移数组元素
    sql 提升查询效率 group by option hash group
    微信引流活动:生成带参二维码、发送海报、
    PMP十大知识领域整理
    iis 站点中文乱码 解决方案
    pdb文件及引发的思考
    TFS 创建团队成员及管理
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10060930.html
Copyright © 2020-2023  润新知