HDU 3395 Special Fish(拆点+最大费用最大流)
Special Fish
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2367 Accepted Submission(s): 878
A fish will spawn after it has been attacked. Each fish can attack one other fish and can only be attacked once. No matter a fish is attacked or not, it can still try to attack another fish which is believed to be female by it.
There is a value we assigned to each fish and the spawns that two fish spawned also have a value which can be calculated by XOR operator through the value of its parents.
We want to know the maximum possibility of the sum of the spawns.
The last test case is followed by a zero, which means the end of the input.
题目链接:HDU 3395
这题跟卖啤酒那差不多,一条鱼只能攻击一次也只能被攻击一次,因此把鱼拆成攻击点1~n和被攻击点n+1~2*n,然后加入源汇点连边即可,但是最大的值不一定是最大流的情况下出现的,因此要在找dis[T]大于等于0的时候停止寻找即可。这题还有一个最大的坑!负号-和异或号^的优先级不一样,记得加括号…………
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
const int M=N+N+N*N;
struct edge
{
int to,nxt,cap,cost;
edge(){}
edge(int _to,int _nxt,int _cap,int _cost):to(_to),nxt(_nxt),cap(_cap),cost(_cost){}
};
edge E[M<<1];
int head[N<<1],tot;
int dis[N<<1],pre[N<<1],path[N<<1];
bitset<N<<1>vis;
char Mat[N][N];
int mc,mf,val[N]; void init()
{
CLR(head,-1);
tot=0;
mc=mf=0;
}
inline void add(int s,int t,int cap,int cost)
{
E[tot]=edge(t,head[s],cap,cost);
head[s]=tot++;
E[tot]=edge(s,head[t],0,-cost);
head[t]=tot++;
}
int SPFA(int s,int t)
{
CLR(dis,INF);
vis.reset();
queue<int>Q;
dis[s]=0;
vis[s]=1;
Q.push(s);
while (!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for (int i=head[u]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(dis[v]>dis[u]+E[i].cost&&E[i].cap>0)
{
dis[v]=dis[u]+E[i].cost;
pre[v]=u;
path[v]=i;
if(!vis[v])
{
vis[v]=1;
Q.push(v);
}
}
}
}
return dis[t]<0;
}
void MCMF(int s,int t)
{
while (SPFA(s,t))
{
int Mf=INF;
for (int i=t; i!=s; i=pre[i])
Mf=min(Mf,E[path[i]].cap);
for (int i=t; i!=s; i=pre[i])
{
E[path[i]].cap-=Mf;
E[path[i]^1].cap+=Mf;
}
mf+=Mf;
mc+=Mf*dis[t];
}
}
int main(void)
{
int n,i,j;
while (~scanf("%d",&n)&&n)
{
init();
for (i=1; i<=n; ++i)
scanf("%d",&val[i]);
int S=0,T=n+n+1;
for (i=1; i<=n; ++i)
{
scanf("%s",Mat[i]+1);
add(S,i,1,0);//n
add(i+n,T,1,0);//n
for (j=1; j<=n; ++j)
{
if(Mat[i][j]=='1')
add(i,n+j,1,-(val[i]^val[j]));//n*n
}
}
MCMF(S,T);
printf("%d\n",-mc);
}
return 0;
}
HDU 3395 Special Fish(拆点+最大费用最大流)的更多相关文章
- [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)
Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...
- HDU 3395 Special Fish 最“大”费用最大流
求最大费用能够将边权取负以转化成求最小费用. 然而此时依旧不正确.由于会优先寻找最大流.可是答案并不一定出如今满流的时候.所以要加一些边(下图中的红边)使其在答案出现时满流. 设全部边的流量为1,花费 ...
- UVA 1658 海军上将(拆点法+最小费用限制流)
海军上将 紫书P375 这题我觉得有2个难点: 一是拆点,要有足够的想法才能把这题用网络流建模,并且知道如何拆点. 二是最小费用限制流,最小费用最大流我们都会,但如果限制流必须为一个值呢?比如这题限制 ...
- Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负
/** 题目:Acme Corporation UVA - 11613 拆点法+最大费用最大流(费用取相反数)+费用有正负 链接:https://vjudge.net/problem/UVA-1161 ...
- LOJ 2321 清华集训2017 无限之环 拆点+最小费用最大流
题面:中文题面,这里不占用篇幅 分析: 看到题面,我就想弃疗…… 但是作为任务题单,还是抄了题解…… 大概就是将每个格子拆点,拆成五个点,上下左右的触点和一个负责连源汇点的点(以下简称本点). 这个这 ...
- hdu 2485 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- poj3422 拆点法x->x'建立两条边+最小费用最大流
/** 题目:poj3422 拆点法+最小费用最大流 链接:http://poj.org/problem?id=3422 题意:给定n*n的矩阵,含有元素值,初始sum=0.每次从最左上角开始出发,每 ...
- 【bzoj2661】[BeiJing wc2012]连连看 最大费用最大流
题目描述 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- vue列表过渡效果
<transition-group></transition-group> ① 列表 <transition-group> </transition-grou ...
- SpringBoot学习4:springboot整合listener
整合方式一:通过注解扫描完成 Listener 组件的注册 1.编写listener package com.bjsxt.listener; import javax.servlet.ServletC ...
- c++引用与指针的区别
c++引用与指针的区别 ★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. 指针的权威定义: In a declaration T D where ...
- select函数实例代码
select函数简解: selct 称之为多路复用IO,使用它可以让程序阻塞在select上,而非实际IO函数上. int select(int nfds, fd_set *readfds, fd_s ...
- linux几种文件传输方式
本文记录linux系统中文件传输的多种方式,留作备忘.linux中文件传输的方式有ftp,scp,rsync,rz,sz等,但各个工具的功能又有所区别: FTP : FTP是文件服务器,可实现文件的上 ...
- PHP namespace、abstract、interface、trait使用介绍
小菜鸟一枚,一直搞不懂 namespace.abstract.interface.trait 这些关系,就抽出几天时间研究,做个总结,不足之处希望大家指正交流. namespace 命名空间 介绍:顾 ...
- 微信在浏览器打开前的提示页面Android与IOS判断
直接在网上扒一个页面,分分钟搞定! 先看一下效果 这是用微信开发工具打开的样式,直接上完整代码 <!DOCTYPE html> <html lang="en"& ...
- Thinkphp5中的Validate验证器的使用
更多笔记: http://note.youdao.com/noteshare?id=e97a5df64888f27d912b3e966b9ec297&sub=web1520841813815 ...
- A1083 List Grades (25)(25 分)
A1083 List Grades (25)(25 分) Given a list of N student records with name, ID and grade. You are supp ...
- 动态规划:POJ2576-Tug of War(二维费用的背包问题)
Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...