设为首页 加入收藏

TOP

HDU2686_Matrix(最大流/费用流)
2015-07-20 17:51:11 来源: 作者: 【 】 浏览:2
Tags:HDU2686_Matrix 最大 费用

解题报告

题目传送门

题意:

给出一个矩阵,每个点都有一个权值。找出一条路径:从最左上角的点到最右下角的点,再从最右下角的点到最左上角的点,使途中经过的点的权值总和最大。并且除了最左上角和最右下角的点,每个点只能经过一次,最左上角的点和最右下角的点的权值只算一次。

思路:

拆点,把矩阵里的每一个点拆成两个点为x和x'。每个x连一条边到x',费用为该点的权值,容量为1。

每个点的x'向右面和下面的点y各连一条边,费用为0,容量为1。

源点到1的容量inf,费用为0,n×n的点到汇点容量为inf,费用为0;

1到1'和2×n×n到汇点 再连一条费用为0,容量为inf的边

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #define inf 0x3f3f3f3f using namespace std; struct node { int v,cost,cap,next; } edge[101000]; int head[10000],dis[10000],pre[10000],vis[10000],f[10000],mmap[100][100],cnt,s,t,n,m,k,flow,cost; void add(int u,int v,int cost,int cap) { edge[cnt].v=v; edge[cnt].cost=cost; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cost=-cost; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } int _spfa() { for(int i=s; i<=t; i++) { dis[i]=-1; pre[i]=f[i]=vis[i]=0; } dis[s]=0; f[s]=inf; pre[s]=-1; vis[s]=1; queue
      
       Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(edge[i].cap&&dis[v]
       
        =0&&x
        
         =0&&y
         
          

?

Matrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1669 Accepted Submission(s): 904 Problem Description Yifenfei 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. Input The input contains multiple test cases. Each case first line given the integer n (2 Than n lines,each line include n positive integers.(<100) Output For each test case output the maximal values yifenfei can get. Sample Input
2
10 3
5 10
3
10 3 3
2 5 3
6 7 10
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

Sample Output
28
46
80

Author yifenfei
Source ZJFC 2009-3 Programming Contest
Recommend yifenfei

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU-4972 篮球赛比分 下一篇poj3422--Kaka's Matrix Trav..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++ 语言社区-CSDN社 (2025-12-24 17:48:24)
·CSDN问答专区社区-CS (2025-12-24 17:48:22)
·C++中`a = b = c`与` (2025-12-24 17:48:19)
·C语言结构体怎么直接 (2025-12-24 17:19:44)
·为什么指针作为c语言 (2025-12-24 17:19:41)