UVA1184 Air Raid
最小路径覆盖板子题。
建二分图,然后跑Dinic(因为我不会匈牙利),然后ans = n - maxflow()。
主要是发一下用链前存图的写法。(好像比vector短一点)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, t;
struct Edge
{
int nxt, from, to, cap, flow;
}e[maxn << ];
int head[maxn << ], ecnt = ; //初值一定是奇数,否则^1不是他的反向边!
void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], x, y, , };
head[x] = ecnt;
e[++ecnt] = (Edge){head[y], y, x, , };
head[y] = ecnt;
} int dis[maxn];
bool bfs()
{
Mem(dis, ); dis[] = ;
queue<int> q; q.push();
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i; i = e[i].nxt)
if(!dis[e[i].to] && e[i].cap > e[i].flow)
{
dis[e[i].to] = dis[now] + ;
q.push(e[i].to);
}
}
return dis[t];
}
int cur[maxn];
int dfs(int now, int res)
{
if(now == t || res == ) return res;
int flow = , f;
if(!cur[now]) cur[now] = head[now];
for(int &i = cur[now]; i; i = e[i].nxt)
{
if(dis[e[i].to] == dis[now] + && (f = dfs(e[i].to, min(res, e[i].cap - e[i].flow))) > )
{
e[i].flow += f; e[i ^ ].flow -= f;
flow += f; res -= f;
if(res == ) break;
}
}
return flow;
} int maxflow()
{
int flow = ;
while(bfs())
{
Mem(cur, );
flow += dfs(, INF);
}
return flow;
} void init()
{
Mem(head, ); ecnt = ;
} int main()
{
int T = read();
while(T--)
{
n = read(); m = read(); t = n + n + ;
init();
for(int i = ; i <= n; ++i) addEdge(, i), addEdge(i + n, t);
for(int i = ; i <= m; ++i)
{
int x = read(), y = read();
addEdge(x, y + n);
}
write(n - maxflow()); enter;
}
return ;
}
UVA1184 Air Raid的更多相关文章
- Air Raid[HDU1151]
Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1151 二分图(无回路有向图)的最小路径覆盖 Air Raid
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 【网络流24题----03】Air Raid最小路径覆盖
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu-----(1151)Air Raid(最小覆盖路径)
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 1151 Air Raid(二分图最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS Memory Limit: 10000K To ...
- HDOJ 1151 Air Raid
最小点覆盖 Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Air Raid(最小路径覆盖)
Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7511 Accepted: 4471 Descript ...
- POJ1422 Air Raid 【DAG最小路径覆盖】
Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6763 Accepted: 4034 Descript ...
- POJ 1422 Air Raid(二分图匹配最小路径覆盖)
POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...
随机推荐
- java连接redis使用jedis带密码
一.引入jedis的Maven配置文件 <!-- redis连接客户端jedis --> <dependency> <groupId>redis.clients&l ...
- 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解
介绍 昨天发的<大叔手记(19):你真懂JavaScript吗?>里面的5个题目,有很多回答,发现强人还是很多的,很多人都全部答对了. 今天我们来对这5个题目详细分析一下,希望对大家有所帮 ...
- MongoDB集群怎样去访问?
上一章节简单介绍了MONGODB的集群搭建.相信大家都已经很熟悉了.集群搭建完接下来应该考虑我们的程序应该怎样去访问他. 怎么读写数据等操作.下面把我在工作中的一些用法列出来供大家作为参考. 官网的链 ...
- 使用Access作数据库
import java.sql.*; public class ConnectAccess { public static void main(String args[]){ ConnectAcces ...
- Popularize what is heart of mobile phone?
From: http://tech.sina.com.cn/mobile/n/2014-09-29/08399657494.shtml 只谈核数没意义 带你重新认识手机SoC 2014年09月29日 ...
- Recsys2018 music recomendation
http://www.recsyschallenge.com/2018/ January 2018 Release of the "One Million Playlists" d ...
- Mirco F-measure and Macro F-measure
- stopPropagation()阻止事件向父容器传递
topPropagation()函数用于阻止当前事件在DOM树上冒泡. 根据DOM事件流机制,在元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈元素上,如果这些祖辈元素上也绑定了相应的事件处理函数 ...
- 序列化Serializable
public interface Serializable 类的序列化由实现java.io.Serializable接口的类启用. 不实现此接口的类将不会使任何状态序列化或反序列化. 可序列化类的所有 ...
- .NET部分知识点整理
.Net学习 Visual Studio2018 企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 开发工具常用V ...