hdoj 4925 Apple tree 【最小割】

來源:互聯網
上載者:User

標籤:std   條件   mem   clear   sea   for   scan   最小   ble   

題目:hdoj 4925 Apple tree 


來源:2014 Multi-University Training Contest 6


題意:給出一個矩陣,然後每一個格子中的數是2^(相鄰格子的個數),然後要求不能取相鄰的數,讓取得數最大。


分析:這個題目有兩種解法,一共是通解。網路流,還有一種是找規律,因為題目中資料是有規律的,所以能夠找規律。非常多人是這樣做的。

以下給出網路流的解法,事實上就是一個方格取數問題。

就是hdoj 1569 點擊開啟連結 的版本號碼,僅僅只是資料範圍增大了。只是資料水了。下來一樣的效果。同樣的代碼能夠ac。

ans = sum - 最小割

具體思路見上面連結:點擊開啟連結

AC代碼:

#include <cstdio>#include <cstring>#include <iostream>#include <string>#include <algorithm>#include <vector>#include <queue>using namespace std;#define Del(a,b) memset(a,b,sizeof(a))const int N = 10200;const int inf = 0x3f3f3f3f;int n,m;struct Node{    int from,to,cap,flow;};vector<int> v[N];vector<Node> e;int vis[N];  //構建層次圖int cur[N];void add_Node(int from,int to,int cap){    e.push_back((Node)    {        from,to,cap,0    });    e.push_back((Node)    {        to,from,0,0    });    int tmp=e.size();    v[from].push_back(tmp-2);    v[to].push_back(tmp-1);}bool bfs(int s,int t){    Del(vis,-1);    queue<int> q;    q.push(s);    vis[s] = 0;    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=0; i<v[x].size(); i++)        {            Node tmp = e[v[x][i]];            if(vis[tmp.to]<0 && tmp.cap>tmp.flow)  //第二個條件保證            {                vis[tmp.to]=vis[x]+1;                q.push(tmp.to);            }        }    }    if(vis[t]>0)        return true;    return false;}int dfs(int o,int f,int t){    if(o==t || f==0)  //最佳化        return f;    int a = 0,ans=0;    for(int &i=cur[o]; i<v[o].size(); i++) //注意前面 ’&‘,非常重要的最佳化    {        Node &tmp = e[v[o][i]];        if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)        {            tmp.flow+=a;            e[v[o][i]^1].flow-=a; //存圖方式            ans+=a;            f-=a;            if(f==0)  //注意最佳化                break;        }    }    return ans;  //最佳化}int dinci(int s,int t){    int ans=0;    while(bfs(s,t))    {        Del(cur,0);        int tm=dfs(s,inf,t);        ans+=tm;    }    return ans;}int solve(int i,int j){    int ans=1;    if(i>1)        ans*=2;    if(j>1)        ans*=2;    if(i<n)        ans*=2;    if(j<m)        ans*=2;    return ans;}int id(int i,int j){    return (i-1)*m+j;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        int s=0,t=m*n+1,x,sum=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                x=solve(i,j);                sum+=x;                if((i+j)%2)                {                    add_Node(s,id(i,j),x);                    if(i>1)                        add_Node(id(i,j),id(i-1,j),inf);                    if(j>1)                        add_Node(id(i,j),id(i,j-1),inf);                    if(i<n)                        add_Node(id(i,j),id(i+1,j),inf);                    if(j<m)                        add_Node(id(i,j),id(i,j+1),inf);                }                else                    add_Node(id(i,j),t,x);            }        }        printf("%d\n",sum-dinci(s,t));        for(int i=0;i<=t;i++)            v[i].clear();        e.clear();    }    return 0;}


hdoj 4925 Apple tree 【最小割】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.