HDU 3376--Matrix Again【最大費用最大流 && 經典建圖】,hdu3376--matrix

來源:互聯網
上載者:User

HDU 3376--Matrix Again【最大費用最大流 && 經典建圖】,hdu3376--matrix

Matrix AgainTime Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3457    Accepted Submission(s): 1020


Problem DescriptionStarvae very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time starvae should to do is that choose a detour which from the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix starvae choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
 
InputThe input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)
 
OutputFor each test case output the maximal values starvae can get. 
Sample Input
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
 
Sample Output
284680
 

題意:

給你一個N*N的矩陣,每個元素代表該處的權值。要求每個點只能走一次,左上方和右下角可以走兩次但該處的權值只能擷取一次。問你從左上方走到右下角(只能向下或右移動),再從右下角回到左上方(只能向上或左移動)所能得到的最大權值。

解析:

題目中問從左上方走到右下角,再從右下角回到左上方所能得到的最大權值,我們可以轉化為從【左上方起點】到【右下角終點】走兩次所獲的最大權值。題目要求起點終點可以走多次,其他定只能走一次。按這種思路的話起點和終點的權值我們多算了一次,最後結果要減去。

把圖看成 n * n個點,相互可到達的點之間建邊, 可走次數作為邊的容量,權值當做邊的費用。問題就變成了最大費用最大流。

建圖過程:

把每個每個點 i 拆成左點 i 和右點 i + n * n。虛擬一個超級源點 outset = 0, 超級匯點 inset = 2 * n * n + 1。

(1)當i 為起點或終點時,左點向右點建邊,邊的容量為2, 因為起點終點可以走兩次, 邊的費用為點的權值。

(若起點和終點拆成容量為1的邊,那我們在起點的時候就只有一個選擇:向上走或者向下走,在終點時只能選擇是由上面的點傳到終點還是由左邊的點傳入終點,這樣的話就相當於求走一次的最大權值和,顯然這樣建圖是不對的。)

(2)當i 不為起點或終點時,左點向右點建邊,邊的容量為1, 因為只能走一次,邊的費用為點的權值。

(3)相互可到達的點之間建邊,如 u --> v,建邊時 右點u 和左點 v 建邊,邊的容量最小為1,邊的費用為0。這點要想明白!!!

(4)源點向起點的左點建邊,邊的容量為2,邊的費用為0,終點的右點向匯點建邊,邊的容量為2,費用為0。


這一類的題給你一個N*M的矩陣,對應N*M個點,且每個點都有一定的點權。當第一次到達某個位置時,我們可以獲得該位置的點權且只能擷取這一次。

這裡可以分為兩種情況:

(1)要求是每個點只能走一次(除了起點和終點可以走多次),問你從左上方到右下角走兩次所擷取的最大權值和。

(2)要求是每個點可以走多次,問你從左上方到右下角走K次所擷取的最大權值和。

情況(1)應該都可以用這種方式建圖。

情況(2)有POJ 3422題,POJ3422。


建圖步驟有什麼不理解的地方,可以看這篇部落格:小比的部落格。裡面還講了情況(2)。(自己畫了一上午圖才想明白,早知道看小比的部落格了,能省不少時間,有巨人的肩膀可以站就是好)。

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define INF 0x3f3f3f3f#define maxn 800000 + 1000#define maxm 4000000 + 1000using namespace std;int n;int outset;int inset;struct node {    int u, v, cap, flow, cost, next;};node edge[maxm];int head[maxn], cnt;int per[maxn];int dist[maxn], vis[maxn];int map[660][660];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w, int c){    node E1 = {u, v, w, 0, c, head[u]};    edge[cnt] = E1;    head[u] = cnt++;    node E2 = {v, u, 0, 0, -c, head[v]};    edge[cnt] = E2;    head[v] = cnt++;}int change(int x, int y){    return (x - 1) * n + y;}void getmap(){    int t = n * n;    outset = 0;    inset = n * n * 2 + 1;    for(int i = 1; i <= n; ++i){        for(int j = 1; j <= n; ++j){            scanf("%d", &map[i][j]);            if(i == 1 && j == 1 || i == n && j == n)                add(change(i, j), change(i, j) + t, 2, map[i][j]);            else                add(change(i, j), change(i, j) + t, 1, map[i][j]);            if(i + 1 <= n)                add(change(i, j) + t, change(i + 1, j), 1, 0);            if(j + 1 <= n)                add(change(i, j) + t, change(i, j + 1), 1, 0);        }    }    add(outset, 1, 2, 0);    add(change(n, n) + t, inset, 2, 0);}bool SPFA(int st, int ed){    queue<int>q;    for(int i = 0; i <= inset; ++i){        dist[i] = -INF;        vis[i] = 0;        per[i] = -1;    }    dist[st] = 0;    vis[st] = 1;    q.push(st);    while(!q.empty()){        int u = q.front();        q.pop();        vis[u] = 0;        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(dist[E.v] < dist[u] + E.cost && E.cap > E.flow){                dist[E.v] = dist[u] + E.cost;                per[E.v] = i;                if(!vis[E.v]){                    vis[E.v] = 1;                    q.push(E.v);                }            }        }    }    return per[ed] != -1;}void MCMF(int st, int ed, int &cost, int &flow){    flow = 0;    cost = 0;    while(SPFA(st, ed)){//每次尋找花銷最小的路徑        int mins = INF;        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            mins = min(mins, edge[i].cap - edge[i].flow);        }         //增廣        for(int i = per[ed]; i != -1; i = per[edge[i ^ 1].v]){            edge[i].flow += mins;            edge[i ^ 1].flow -= mins;            cost += edge[i].cost * mins;        }        flow += mins;    }}int main (){    while(scanf("%d", &n) != EOF){        init();        getmap();        int cost, flow;        MCMF(outset, inset, cost, flow);        cost = cost - map[1][1] - map[n][n];        printf("%d\n", cost);    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.