nyoj677 谍战
本题能够说是最小割入门级题目。
假设能想到是最小割问题。那么建图思路便是水到渠成的事了。
加入一个源点S和汇点T;
把S与每一个间谍相连。容量为无穷大;
把城市N(即飞机场的位置)与汇点T相连。容量为无穷大;
之间有道路的城市相连。容量为1,注意这里是双向的边;
建图完后,依据最大流最小割定理。那么直接求最大流就可以。
闲话少说,上代码:
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm> #define INS 1<<30
#define CLR(arr,v) memset(arr,v,sizeof(arr)) #define MaxV 3000
#define MaxE 100000 class MaxFlow{
public:
void Clear(){
CLR(h,-1); CLR(cnt,0); CLR(vis,0);
flag = false;
pos = top = head = total = maxflow = 0;
}
void add(int u,int v,int flow){
num[pos] = v;
sur[pos] = flow;
next[pos] = h[u];
h[u] = pos++; num[pos] = u;
sur[pos] = 0;
next[pos] = h[v];
h[v] = pos++;
}
int GetMaxFlow(int s,int t){
init(t);
stk[top] = s;
while(!flag){
minres = INS;
if(top < 0) top = 0;
if(!dfs(stk[top],-1,t,minres)) continue;
maxflow += minres;
while(dis != -1){
sur[dis] -= minres;
sur[dis^1] += minres;
dis = pre_e[dis];
}
top = 0;
}
return maxflow;
}
private:
int h[MaxV],num[MaxE],sur[MaxE],next[MaxE],gap[MaxV],cnt[MaxV],pre_e[MaxE],stk[MaxV],que[MaxV];
int pos,top,head,total,maxflow,minres,dis;
bool vis[MaxV],flag;
void init(int n){
que[total++] = n;
vis[n] = true;
while(head < total){
int p = que[head++];
if(head >= MaxV) head -= MaxV;
for(int i = h[p]; i != -1 ;i = next[i]){
if(!vis[ num[i] ]){
vis[ num[i] ] = true;
gap[ num[i] ] = gap[p] + 1;
cnt[ gap[ num[i] ] ]++;
que[total++] = num[i];
if(total >= MaxV) total -= MaxV;
}
}
}
}
bool dfs(int p,int father,int n,int &minres){
int m = minres;
for(int i = h[p]; i != -1 ;i = next[i]){
if(sur[i] > 0 && gap[p] - gap[ num[i] ] == 1){
minres = min(minres,sur[i]);
pre_e[i] = father;
if(num[i] != n) stk[++top] = num[i];
if(num[i] == n || dfs(num[i],i,n,minres)) {
if(num[i] == n) dis = i;
return true;
}
minres = m;
}
}
cnt[ gap[p] ]--;
cnt[ gap[p] + 1]++;
top--;
if(cnt[ gap[p] ] == 0) flag = true;
gap[p] += 1;
return false;
}
}T; int main()
{
int t;
scanf("%d",&t);
int cnt = 0;
while (t--)
{
int n,m,p;
scanf("%d%d%d",&n,&m,&p); int N = n + 1;
T.Clear(); int x;
for (int i = 1; i <= p; ++ i)
scanf("%d",&x),T.add(0,x,INS); for (int i = 1; i <= m; ++ i)
{
int u,v;
scanf("%d%d",&u,&v);
T.add(u,v,1);
T.add(v,u,1);
} T.add(n,N,INS); printf("Case #%d: %d\n",++cnt,T.GetMaxFlow(0,N));
}
return 0;
}
nyoj677 谍战的更多相关文章
- 黑客炼金术士 Seeker:可以攻破 4G 摸到你短信,还要为朝阳群众提供谍战工具
在北京上地的一家咖啡馆里,我在等待黑客 Seeker 的到来. 我对黑客 Seeker 颇有期待.他曾在黑客大会 KCon 上演讲<伪基站高级利用技术——彻底攻破短信验证码>,介绍利用 L ...
- 使用C#处理基于比特流的数据
使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...
- 关于试用jquery的jsonp实现ajax跨域请求数据的问题
我们在开发过程中遇到要获取另一个系统数据时,就造成跨域问题,这就是下文要说的解决办法: 先我们熟悉下json和jsonp的区别: 使用AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交 ...
- [转]说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
本文转自:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两 ...
- json jsonp的区别
前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...
- JSON和JSONP (含jQuery实例)(share)
来源:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两个问 ...
- 【转载】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可 ...
- json和jsonp(json是目的,jsonp是手段)
自己理解:JSON是一种数据交换格式,而JSONP是一种依靠开发人员的聪明才智创造出的一种非官方跨域数据交互协议.我们拿最近比较火的谍战片来打个比方,JSON是地下党们用来书写和交换情报的" ...
- 说说JSON和JSONP,也许你会豁然开朗(转)
前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...
随机推荐
- [Android]Volley源代码分析(二)Cache
Cache作为Volley最为核心的一部分,Volley花了重彩来实现它.本章我们顺着Volley的源代码思路往下,来看下Volley对Cache的处理逻辑. 我们回忆一下昨天的简单代码,我们的入口是 ...
- 经典算法——Jump Game(II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- vlan 介绍
简介 在Linux中安装了802.1Q标签VLAN功能.VLAN是虚拟分配以太网的功能. 使用VLAN ID从物理上将一个以太网分割开.在VLAN环境下,具有相同VLAN ID 就可以相互通 ...
- Unity异常警告错误处理方法
原地址:http://www.haogongju.net/art/2591936 1. The AnimationClip 'cube1_anim' used by the Animation co ...
- LoadRunner测试AJAX
什么是AJAX? Ajax, shorthand for Asynchronous JavaScript and XML, is a web development technique for cre ...
- JavaScript | 对象与属性
———————————————————————————————————————————— 对象:JavaScript是基于原型的语言,没有Class,所以将函数作为类 - - - - - - - - ...
- Oracle学习笔记(5)——查询
基本查询语句 SELECT [DISTINCT] column_name1,...|* FROM table_name [WHERE conditions] 在SQL*PLUS中设置格式 更改显示字段 ...
- 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
上一篇文章中.我们使用location.hash来模拟ajax的前进后退功能.使用location.hash存在以下几个问题: 1.使用location.hash会导致地址栏的url发生变化.用户体验 ...
- Jquery判断其中任意一个文本框的值是否被修改
<!doctype html><html lang="en"> <head> <meta charset="UTF-8&quo ...
- typeof()关键字
typeof是GNU c标准的关键字. typeof()的作用是自动推导出括号中表达式的数据类型. #include <stdio.h> void func1(void) { ; type ...