HPU 2686--Matrix【最大費用最大流 && 經典建圖】,hpu2686--matrix

來源:互聯網
上載者:User

HPU 2686--Matrix【最大費用最大流 && 經典建圖】,hpu2686--matrix

MatrixTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2062    Accepted Submission(s): 1074


Problem DescriptionYifenfei 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 yifenfei should to do is that choose a detour which frome 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 yifenfei 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 yifenfei can not pass the same area of the Matrix except the start and end.
 
InputThe input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
 
OutputFor each test case output the maximal values yifenfei 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
 

和HPU3376一模一樣, 但是資料比較弱,果斷水了。

具體的解析請看這裡 :HPU3376

#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.