zoukankan      html  css  js  c++  java
  • LA 7277 Landscaping(最小割)

    https://vjudge.net/problem/UVALive-7277

    题意:

    给出一个n*m的地图,.代表低坡,#代表高坡。

    现在有n+m辆车分别从上端和左端出发,如果在行驶的过程中需要转换高低坡,需要额外支付A元,当然它也可以提前花费B元将任意的一块地地改成低坡或高坡。

    问最少花费。

    思路:

    利用最小割,将高坡都和源点相连,容量为B,将低坡和汇点相连,容量为A。

    地图上相邻的点相连,容量为A。

      1 #include<iostream>
      2 #include<string>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 #include<cstdio>
      7 using namespace std;
      8 
      9 const int INF=0x3f3f3f3f;
     10 const int maxn=5000+5;
     11 
     12 struct Edge
     13 {
     14     int from,to,cap,flow;
     15     Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
     16 };
     17 
     18 struct Dinic
     19 {
     20     int n,m,s,t;
     21     vector<Edge> edges;
     22     vector<int> G[maxn];
     23     bool vis[maxn];
     24     int cur[maxn];
     25     int d[maxn];
     26 
     27     void init(int n)
     28     {
     29         this->n=n;
     30         for(int i=0;i<n;++i) G[i].clear();
     31         edges.clear();
     32     }
     33 
     34     void AddEdge(int from,int to,int cap)
     35     {
     36         edges.push_back( Edge(from,to,cap,0) );
     37         edges.push_back( Edge(to,from,0,0) );
     38         m=edges.size();
     39         G[from].push_back(m-2);
     40         G[to].push_back(m-1);
     41     }
     42 
     43     bool BFS()
     44     {
     45         queue<int> Q;
     46         memset(vis,0,sizeof(vis));
     47         vis[s]=true;
     48         d[s]=0;
     49         Q.push(s);
     50         while(!Q.empty())
     51         {
     52             int x=Q.front(); Q.pop();
     53             for(int i=0;i<G[x].size();++i)
     54             {
     55                 Edge& e=edges[G[x][i]];
     56                 if(!vis[e.to] && e.cap>e.flow)
     57                 {
     58                     vis[e.to]=true;
     59                     d[e.to]=d[x]+1;
     60                     Q.push(e.to);
     61                 }
     62             }
     63         }
     64         return vis[t];
     65     }
     66 
     67     int DFS(int x,int a)
     68     {
     69         if(x==t || a==0) return a;
     70         int flow=0, f;
     71         for(int &i=cur[x];i<G[x].size();++i)
     72         {
     73             Edge &e=edges[G[x][i]];
     74             if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0)
     75             {
     76                 e.flow +=f;
     77                 edges[G[x][i]^1].flow -=f;
     78                 flow +=f;
     79                 a -=f;
     80                 if(a==0) break;
     81             }
     82         }
     83         return flow;
     84     }
     85 
     86     int Maxflow(int s,int t)
     87     {
     88         this->s=s; this->t=t;
     89         int flow=0;
     90         while(BFS())
     91         {
     92             memset(cur,0,sizeof(cur));
     93             flow +=DFS(s,INF);
     94         }
     95         return flow;
     96     }
     97 }DC;
     98 
     99 int n,m,a,b;
    100 char g[maxn][maxn];
    101 int dx[]={0,0,1,-1};
    102 int dy[]={1,-1,0,0};
    103 
    104 int main()
    105 {
    106     //freopen("D:\input.txt", "r", stdin);
    107     while(~scanf("%d%d%d%d",&n,&m,&a,&b))
    108     {
    109         for(int i=0;i<n;i++)
    110             scanf("%s",&g[i]);
    111         int src=0,dst=n*m+1;
    112         DC.init(dst+1);
    113         for(int i=0;i<n;i++)
    114         {
    115             for(int j=0;j<m;j++)
    116             {
    117                 int id=i*m+j+1;
    118                 if(g[i][j]=='#')  DC.AddEdge(src,id,b);
    119                 else DC.AddEdge(id,dst,b);
    120                 for(int k=0;k<4;k++)
    121                 {
    122                     int x=i+dx[k];
    123                     int y=j+dy[k];
    124                     if(x<0||x>=n||y<0||y>=m) continue;
    125                     //if(g[x][y]!=g[i][j]) 
    126                     DC.AddEdge(id,x*m+y+1,a);
    127                 }
    128             }
    129         }
    130         int ans=DC.Maxflow(src,dst);
    131         printf("%d
    ",ans);
    132     }
    133     return 0;
    134 }
  • 相关阅读:
    gzip格式解压缩
    震动效果
    用SDWebImage加载FLAnimatedImage
    用UIInterpolatingMotionEffect产生透视效果
    将CAGradientLayer当做mask使用
    UITableView加载网络数据的优化
    沿着path路径做动画
    vue入门案例
    springboot拦截器拦了静态资源css,js,png,jpeg,svg等等静态资源
    SpringBoot2.x|Thymeleaf页面不能正常载入css、js文件
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6878155.html
Copyright © 2011-2022 走看看