FZU 1844 Earthquake Damage(最大流最小割)
Problem Description
Open Source Tools help earthquake researchers stay a step ahead. Many geological research facilities around the world use or are in the process of developing open source software and applications designed to interpret and share information with other researchers. For example, OpenSees is an open source software framework for developing apps that help understand what happens to structures during and after earthquakes to help engineers design stronger buildings. Researchers are also using OpenSees to understand the potential ill-effects of seismic activity on viaducts and bridges.

China has had an earthquake that has struck Sichuan Province on Monday 12 May 2008!
The earthquake has damaged some of the cities so that they are unpassable. Remarkably, after repairing by Chinese People's Liberation Army, all the driveways between cities were fixed.
As usual, Sichuan Province is modeled as a set of P (1 <= P <= 3,000) cities conveniently numbered 1..P which are connected by a set of C (1 <= C <= 20,000) non-directional driveways conveniently numbered 1..C. Driveway i connects city a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P). Driveway might connect a_i to itself or perhaps might connect two cities more than once. The Crisis Center is located in city 1.
A total of N (1 <= N <= P) survivors (in different cities) sequentially contacts Crisis Center via moobile phone with an integer message report_j (2 <= report_j <= P) that indicates that city report_j is undamaged but that the calling survivor is unable to return to the Crisis Center from city report_j because he/she could not find a path that does not go through damaged city.
After all the survivors report in, determine the minimum number of cities that are damaged.
Input
Input consists of several testcases. The format of each case as follow:
- Line 1: Three space-separated integers: P, C, and N
- Lines 2..C+1: Line i+1 describes cowpath i with two integers: a_i and b_i
- Lines C+2..C+N+1: Line C+1+j contains a single integer: report_j
Output
For each testcase, output a line with one number, the minimum number of damaged cities.
用最大流最小割定理,最大流=最小割,很久以前做的,题目已忘,我是来放模版的
DINIC算法+当前弧优化+有容量上限,78MS
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 6010
#define MAXM 100010 #define INF 0x7fffffff struct Dinic {
int n, m, st, ed, ecnt;
int vis[MAXN], head[MAXN];
int cur[MAXN], d[MAXN];
int to[MAXM], next[MAXM], flow[MAXM], cap[MAXM]; void init(int ss, int tt){
memset(head,,sizeof(head));
ecnt = ;
st = ss; ed = tt;
} void addEdge(int u,int v,int c) {
//flow[ecnt] = c
to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = ; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cap[ecnt] = ; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} bool bfs() {
memset(vis, , sizeof(vis));
queue<int> que; que.push(st);
d[st] = ; vis[st] = true;
while(!que.empty()){
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]){
int v = to[p];
if (!vis[v] && cap[p] > flow[p]){//flow[p] > 0
vis[v] = ;
d[v] = d[u] + ;
que.push(v);
if(v == ed) return true;
}
}
}
return vis[ed];
} int dfs(int u, int a) {
if(u == ed || a == ) return a;
int outflow = , f;
for(int &p = cur[u]; p; p = next[p]){
int v = to[p];
if(d[u] + == d[v] && (f = dfs(v, min(a, cap[p] - flow[p]))) > ){//min(a, flow[p])
flow[p] += f;//flow[p] -= f;
flow[p ^ ] -= f;//flow[p ^ 1] += f;
outflow += f;
a -= f;
if(a == ) break;
}
}
return outflow;
} int Maxflow() {
int ans = ;
while(bfs()){
for(int i = ; i <= ed; ++i) cur[i] = head[i];
ans += dfs(st, INF);
}
return ans;
}
} G; int vis[MAXN]; int main() {
int ss, tt, N, C, P;
while(scanf("%d%d%d",&P,&C,&N)!=EOF){
ss = ; tt = *P+;
G.init(ss, tt);
while(C--){
int a, b;
scanf("%d%d",&a,&b);
G.addEdge(a + P, b, INF);
G.addEdge(b + P, a, INF);
}
memset(vis, , sizeof(vis));
while(N--){
int x;
scanf("%d",&x);
G.addEdge(x, tt, INF);
vis[x] = ;
}
for(int i = ; i <= P; ++i){
if(i != && !vis[i]) G.addEdge(i, i + P, );
else G.addEdge(i, i + P, INF);
}
G.n = tt;
printf("%d\n",G.Maxflow());
}
return ;
}
DINIC算法+当前弧优化+只有余量,62MS
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 6010
#define MAXM 100010 #define INF 0x7fffffff struct Dinic {
int n, m, st, ed, ecnt;
int vis[MAXN], head[MAXN];
int cur[MAXN], d[MAXN];
int to[MAXM], next[MAXM], flow[MAXM]; void init(int ss, int tt){
memset(head,,sizeof(head));
ecnt = ;
st = ss; ed = tt;
} void addEdge(int u,int v,int f) {
to[ecnt] = v; flow[ecnt] = f; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} bool bfs() {
memset(vis, , sizeof(vis));
queue<int> que; que.push(st);
d[st] = ; vis[st] = true;
while(!que.empty()){
int u = que.front(); que.pop();
for(int p = head[u]; p; p = next[p]){
int v = to[p];
if (!vis[v] && flow[p] > ){
vis[v] = ;
d[v] = d[u] + ;
que.push(v);
if(v == ed) return true;
}
}
}
return vis[ed];
} int dfs(int u, int a) {
if(u == ed || a == ) return a;
int outflow = , f;
for(int &p = cur[u]; p; p = next[p]){
int v = to[p];
if(d[u] + == d[v] && (f = dfs(v, min(a, flow[p]))) > ){
flow[p] -= f;
flow[p ^ ] += f;
outflow += f;
a -= f;
if(a == ) break;
}
}
return outflow;
} int Maxflow() {
int ans = ;
while(bfs()){
for(int i = ; i <= ed; ++i) cur[i] = head[i];
ans += dfs(st, INF);
}
return ans;
}
} G; int vis[MAXN]; int main() {
int ss, tt, N, C, P;
while(scanf("%d%d%d",&P,&C,&N)!=EOF){
ss = ; tt = *P+;
G.init(ss, tt);
while(C--){
int a, b;
scanf("%d%d",&a,&b);
G.addEdge(a + P, b, INF);
G.addEdge(b + P, a, INF);
}
memset(vis, , sizeof(vis));
while(N--){
int x;
scanf("%d",&x);
G.addEdge(x, tt, INF);
vis[x] = ;
}
for(int i = ; i <= P; ++i){
if(i != && !vis[i]) G.addEdge(i, i + P, );
else G.addEdge(i, i + P, INF);
}
G.n = tt;
printf("%d\n",G.Maxflow());
}
return ;
}
ISAP算法,78MS(把注释删掉再交一次又变成了93MS,OJ的时间果然信不过o(╯□╰)o)//不能有点0
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; #define MAXN 6010
#define MAXM 100010
#define INF 0x7fffffff struct SAP {
int vis[MAXN], head[MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int to[MAXM], flow[MAXM], next[MAXM];
int ecnt, st, ed; void init(int ss, int tt) {
memset(head, , sizeof(head));
ecnt = ;
st = ss; ed = tt;
} void addEdge(int u,int v,int f) {
to[ecnt] = v; flow[ecnt] = f; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} int Max_flow() {
int ans = , minFlow = INF, n = ed, u;
for (int i = ; i <= n; ++i){
cur[i] = head[i];
gap[i] = dis[i] = ;
}
u = pre[st] = st;
gap[] = n;
while (dis[st] < n){
bool flag = false;
for (int &p = cur[u]; p; p = next[p]){
int v = to[p];
if (flow[p] > && dis[u] == dis[v] + ){
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed){
ans += minFlow;
while (u != st){
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow;
}
minFlow = INF;
}
break;
}
}
if (flag) continue;
int minDis = n-;
for (int p = head[u]; p; p = next[p]){
int v = to[p];
if (flow[p] && dis[v] < minDis){
minDis = dis[v];
cur[u] = p;
}
}
if (--gap[dis[u]] == ) break;
gap[dis[u] = minDis+]++;
u = pre[u];
}
return ans;
}
} G; bool vis[MAXN]; int main() {
int ss, tt, N, C, P;
while(scanf("%d%d%d",&P,&C,&N)!=EOF) {
ss = ; tt = *P+;
G.init(ss, tt);
while(C--) {
int a, b;
scanf("%d%d",&a,&b);
G.addEdge(a + P, b, INF);
G.addEdge(b + P, a, INF);
}
memset(vis, , sizeof(vis));
while(N--) {
int x;
scanf("%d",&x);
G.addEdge(x, tt, INF);
vis[x] = ;
}
for(int i = ; i <= P; ++i) {
if(i != && !vis[i]) G.addEdge(i, i + P, );
else G.addEdge(i, i + P, INF);
}
printf("%d\n",G.Max_flow());
}
return ;
}
ISAP算法+BFS初始化,62MS(SGU 438把我这模板跑跪了,现在换掉了>_<)//不能有点0
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; #define MAXN 6010
#define MAXM 100010
#define INF 0x7fffffff struct SAP {
int head[MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int to[MAXM], flow[MAXM], next[MAXM];
int ecnt, st, ed, n; void init(int ss, int tt, int nn) {
memset(head, , sizeof(head));
ecnt = ;
st = ss; ed = tt; n = nn;
} void addEdge(int u,int v,int f) {
to[ecnt] = v; flow[ecnt] = f; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; next[ecnt] = head[v]; head[v] = ecnt++;
} void bfs() {
memset(dis, 0x3f, sizeof(dis));
queue<int> que; que.push(ed);
dis[ed] = ;
while(!que.empty()) {
int u = que.front(); que.pop();
++gap[dis[u]];
for(int p = head[u]; p; p = next[p]) {
int v = to[p];
if (dis[v] > ed && flow[p ^ ] > ) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
} int Max_flow() {
int ans = , minFlow = INF, u;
for (int i = ; i <= n; ++i){
cur[i] = head[i];
gap[i] = dis[i] = ;
}
u = pre[st] = st;
//gap[0] = n;
bfs();
while (dis[st] < n){
bool flag = false;
for (int &p = cur[u]; p; p = next[p]){
int v = to[p];
if (flow[p] > && dis[u] == dis[v] + ){
flag = true;
minFlow = min(minFlow, flow[p]);
pre[v] = u;
u = v;
if(u == ed){
ans += minFlow;
while (u != st){
u = pre[u];
flow[cur[u]] -= minFlow;
flow[cur[u] ^ ] += minFlow;
}
minFlow = INF;
}
break;
}
}
if (flag) continue;
int minDis = n-;
for (int p = head[u]; p; p = next[p]){
int v = to[p];
if (flow[p] && dis[v] < minDis){
minDis = dis[v];
cur[u] = p;
}
}
if (--gap[dis[u]] == ) break;
gap[dis[u] = minDis+]++;
u = pre[u];
}
return ans;
}
} G; bool vis[MAXN]; int main() {
int ss, tt, N, C, P;
while(scanf("%d%d%d",&P,&C,&N)!=EOF) {
ss = ; tt = *P+;
G.init(ss, tt, tt);
while(C--) {
int a, b;
scanf("%d%d",&a,&b);
G.addEdge(a + P, b, INF);
G.addEdge(b + P, a, INF);
}
memset(vis, , sizeof(vis));
while(N--) {
int x;
scanf("%d",&x);
G.addEdge(x, tt, INF);
vis[x] = ;
}
for(int i = ; i <= P; ++i) {
if(i != && !vis[i]) G.addEdge(i, i + P, );
else G.addEdge(i, i + P, INF);
}
printf("%d\n",G.Max_flow());
}
return ;
}
FZU 1844 Earthquake Damage(最大流最小割)的更多相关文章
- bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着 ...
- hiho 第116周,最大流最小割定理,求最小割集S,T
小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? 小Ho:我记得!网络流就是给定了一张图G=(V,E),以及源点s和汇点t.每一条边e(u,v)具有容量c ...
- hihocoder 网络流二·最大流最小割定理
网络流二·最大流最小割定理 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? ...
- [HihoCoder1378]网络流二·最大流最小割定理
思路: 根据最大流最小割定理可得最大流与最小割相等,所以可以先跑一遍EdmondsKarp算法.接下来要求的是经过最小割切割后的图中$S$所属的点集.本来的思路是用并查集处理所有前向边构成的残量网络, ...
- HDU 1569 方格取数(2)(最大流最小割の最大权独立集)
Description 给你一个m*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取数所在的2个格子不能相邻,并且取出的数的和最大. ...
- 【codevs1907】方格取数3(最大流最小割定理)
网址:http://codevs.cn/problem/1907/ 题意:在一个矩阵里选不相邻的若干个数,使这些数的和最大. 我们可以把它看成一个最小割,答案就是矩阵中的所有数-最小割.先把矩阵按国际 ...
- 洛谷 P2932 [USACO09JAN]地震造成的破坏Earthquake Damage
P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 题目描述 Wisconsin has had an earthquake that has struck Farm ...
- 紫书 例题 11-12 UVa 1515 (最大流最小割)
这道题要分隔草和洞, 然后刘汝佳就想到了"割"(不知道他怎么想的, 反正我没想到) 然后就按照这个思路走, 网络流建模然后求最大流最小割. 分成两部分, S和草连, 洞和T连 外围 ...
- HDU-4289-Control(最大流最小割,拆点)
链接: https://vjudge.net/problem/HDU-4289 题意: You, the head of Department of Security, recently receiv ...
随机推荐
- 搭建Extjs框架(二)
搭建Extjs 框架 二.编写入口文件 app.js,配置extjs 组件\视图文件路径 并将app.js引入index.html 在app.js中指定一些文件的路径,Extjs页面的起始 ...
- 2018 CVTE 前端校招笔试题整理
昨天晚上(7.20)做了CVTE的前端笔试,总共三十道题,28道多选题,2道编程题 .做完了之后觉得自己基础还是不够扎实,故在此整理出答案,让自己能从中得到收获,同时给日后的同学一些参考. 首先说一下 ...
- 解决MacOS升级后出现xcrun: error: invalid active developer path, missing xcrun的问题
升级了系统 命令行不能用了 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), mis ...
- Docker环境搭建以及基本操作
Docker环境搭建以及基本操作 Docker环境基本搭建: 基础环境:Centos 7.4 IP:192.168.30.117 [root@docker ~]# cat /etc/re ...
- Python入门 —— 01简介
Python 历史 python 的创始人为荷兰人吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ...
- nuxt 优化项:禁用js的预加载
这里有个nuxt和vue不同的地方,这个地方很有意思,官方的中文文档说得蜜汁自信 ------------------------------- In production, nuxt.js uses ...
- 帝国cms教程父栏目和子栏目都能在当前栏目高亮
首先在/e/class/userfun.php这个文件里面加上下面代码.上面父栏目的,下面子栏目的.红色代表css样式.自定义吧 function currentPage($classid,$this ...
- Python学习:6.python内置函数
Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...
- Python入门 (三)
迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器 ...
- Git Status 中文乱码解决
现象: jb@H39:~/doc$ git statusOn branch masterYour branch is up-to-date with 'origin/master'. Untracke ...