hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2350 Accepted Submission(s): 1241
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.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
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
46
80
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
int flag[N][N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
pre[i] = -;
}
queue<int> q;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = ,minflow,flow=;
while(spfa(s,t,n))
{
minflow=INF+;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int n;
int graph[][];
int P(int x,int y){
return (x-)*n+y;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
int src = ,des = *n*n+;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(P(i,j)!=&&P(i,j)!=n*n){
addEdge(P(i,j),P(i,j)+n*n,,-graph[i][j],tot);
if(i!=n) addEdge(P(i,j)+n*n,P(i+,j),,,tot);
if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+,,,tot);
}else{
if(P(i,j)==){
addEdge(P(i,j),P(i+,j),,,tot);
addEdge(P(i,j),P(i,j)+,,,tot);
}
}
}
}
addEdge(src,,,,tot);
addEdge(n*n,des,,,tot);
int min_cost = MCMF(src,des,*n*n+);
printf("%d\n",-min_cost+graph[][]+graph[n][n]);
}
}
hdu 3376开大一点就OK
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = ;
const int N = ;
const int M = ;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int head[N],tot,low[N],pre[N];
int total ;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
edge[k].u=u,edge[k].v=v,edge[k].cap = cap,edge[k].cost = cost,edge[k].next = head[u],head[u] = k++;
edge[k].u=v,edge[k].v=u,edge[k].cap = ,edge[k].cost = -cost,edge[k].next = head[v],head[v] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
low[i] = INF;
pre[i] = -;
}
queue<int> q;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(edge[k].cap>&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = ,minflow,flow=;
while(spfa(s,t,n))
{
minflow=INF+;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}
int n;
int graph[][];
int P(int x,int y){
return (x-)*n+y;
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
int src = ,des = *n*n+;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(P(i,j)!=&&P(i,j)!=n*n){
addEdge(P(i,j),P(i,j)+n*n,,-graph[i][j],tot);
if(i!=n) addEdge(P(i,j)+n*n,P(i+,j),,,tot);
if(j!=n) addEdge(P(i,j)+n*n,P(i,j)+,,,tot);
}else{
if(P(i,j)==){
addEdge(P(i,j),P(i+,j),,,tot);
addEdge(P(i,j),P(i,j)+,,,tot);
}
}
}
}
addEdge(src,,,,tot);
addEdge(n*n,des,,,tot);
int min_cost = MCMF(src,des,*n*n+);
printf("%d\n",-min_cost+graph[][]+graph[n][n]);
}
}
hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)的更多相关文章
- hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***
题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙, 每个逮捕队伍在每个城市可以选 ...
- HDU 6118 度度熊的交易计划(最小费用最大流)
Problem Description度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个 ...
- HDU 3435 A new Graph Game(最小费用最大流)&HDU 3488
A new Graph Game Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu-3376-Matrix Again(最小费用最大流)
题意: 给一个矩形,从左上角走到右下角,并返回左上角(一个单元格只能走一次,左上角和右下角两个点除外) 并且从左上到右下只能往右和下两个方向.从右下返回左上只能走上和左两个方向! 分析: 拆点,最小费 ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- hdu 4494 Teamwork 最小费用最大流
Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
随机推荐
- JavaScript去除数组中的重复值
用原型函数(prototype)可以定义一些很方便的自定义函数,实现各种自定义功能. Javascript 中的原型函数(prototype)的工作原理,在 javascript 中每次声明新函数的过 ...
- ImageNet: what is top-1 and top-5 error rate?
https://stats.stackexchange.com/questions/156471/imagenet-what-is-top-1-and-top-5-error-rate Your cl ...
- HDU 5641
King's Phone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- ubuntu14安装tensorflow并测试
1.ubuntu版本的选择:看了很多博文,建议使用ubuntu14,稳定兼容性好. 2.tensorflow的安装: http://wiki.jikexueyuan.com/project/tenso ...
- [samba]samba设置指定用户权限
步骤: 1.在系统中添加用户 批量添加用户和密码的方法(因为samba用户要求必须在系统中存在): for name in a b c d;do useradd $name ; echo " ...
- syslog大小限制
位置 /etc/logrotate.d/rsyslog 相关配置信息察看man logrotate size k/M/G /var/log/syslog { rotate daily missingo ...
- POJ 3984 BFS
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20665 Accepted: 12100 Descriptio ...
- jQuery对象初始化的传参方式
jQuery对象初始化的传参方式包括: 1.$(DOMElement) 2.$(' ... '), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第 ...
- PHP 截取字符串,多余部分用 ........ 代替
/** * 参数说明 * $string 欲截取的字符串 * $sublen 截取的长度 * $start 从第几个字节截取,默认为0 * $code 字符编码,默认UTF-8 */ function ...
- 在Linux系统里运行shutdown.sh命令关闭Tomcat时出现错误提示
服务器:linnux 5.5 64位,已安装好 jdk: Tomcat版本:apache-tomcat-7.0.53 操作软件:Xshell 4(Free for Home / School) 刚开始 ...