嘟嘟嘟

最小路径覆盖板子题。

建二分图,然后跑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的更多相关文章

  1. Air Raid[HDU1151]

    Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. hdu1151 二分图(无回路有向图)的最小路径覆盖 Air Raid

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  3. 【网络流24题----03】Air Raid最小路径覆盖

    Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. hdu-----(1151)Air Raid(最小覆盖路径)

    Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  5. hdu 1151 Air Raid(二分图最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS   Memory Limit: 10000K To ...

  6. HDOJ 1151 Air Raid

    最小点覆盖 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Air Raid(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 Descript ...

  8. POJ1422 Air Raid 【DAG最小路径覆盖】

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6763   Accepted: 4034 Descript ...

  9. POJ 1422 Air Raid(二分图匹配最小路径覆盖)

    POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...

随机推荐

  1. java se系列(四) 函数、数组、排序算法、二分法、二维数组

    1 函数 1.1  数的概述 发现不断进行加法运算,为了提高代码的复用性,就把该功能独立封装成一段独立的小程序,当下次需要执行加法运算的时候,就可以直接调用这个段小程序即可,那么这种封装形形式的具体表 ...

  2. 记录: Win10+Ubuntu18.04双系统安装

    在重装windows系统的时候顺便将ubuntu也重装了. window 10 安装 制作USB启动盘 到"微软中国下载中心"(http://www.microsoft.com/z ...

  3. IntelliJ IDEA 使用 LiveEdit 插件实现实时可视化前端开发

    之前因为公司很多都是C#后台项目,所以一直用的Visual Studio开发.而在VS里会自带实时刷新功能,即:在IDE中修改的CSS代码会同步反映在页面上,而不用我们手动F5刷新. 先在因为在考虑做 ...

  4. vim脚本语言

    转自:http://man.chinaunix.net/newsoft/vi/doc/usr_41.html#usr_41.txt Vim 脚本语言在很多地方用到,包括 vimrc 文件, 语法文件, ...

  5. Unity 基础

    Unity 基础是unity入门的关键.他将讲解Unity的界面, 菜单项,使用资源,创设场景,并发布版本. 当你读完这段,你将理解unity是怎么工作的,如何有效地使用它,并且完成一个基本的游戏. ...

  6. Spring JdbcTemplate 使用总结

    1.查询Object public Classify queryClassifById(int id){ String sql="select * from t_classify where ...

  7. SQL 表定时同步

    1.创建存储过程 create proc [dbo].[sync_calendar] as truncate table dbo.CalendarEvents insert into Calendar ...

  8. artDialog组件应用学习(三)

    一.可以加载url的对话框 预览: 对话框编写代码 //弹出一个对话框,加载页面 function OpenBox(url, title, width, height) { seajs.use(['j ...

  9. Java Web基础——Action+Service +Dao三层的功能划分 (转载)

    原文:https://blog.csdn.net/inter_peng/article/details/41021727 仅供自己学习使用: 1. Action/Service/DAO简介: Acti ...

  10. urllib模块的使用

    Python3学习笔记(urllib模块的使用) 1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, ca ...