博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2
阅读量:6497 次
发布时间:2019-06-24

本文共 3434 字,大约阅读时间需要 11 分钟。

题目描述

Wisconsin has had an earthquake that has struck Farmer John's farm! The earthquake has damaged some of the pastures so that they are unpassable. Remarkably, none of the cowpaths was damaged.

As usual, the farm is modeled as a set of P (1 <= P <= 3,000)

pastures conveniently numbered 1..P which are connected by a set of C (1 <= C <= 20,000) non-directional cowpaths conveniently

numbered 1..C. Cowpath i connects pastures a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P). Cowpaths might connect a_i to itself or perhaps might connect two pastures more than once. The barn is located in pasture 1.

A total of N (1 <= N <= P) cows (in different pastures) sequentially contacts Farmer John via moobile phone with an integer message report_j (2 <= report_j <= P) that indicates that pasture report_j is undamaged but that the calling cow is unable to return to the barn from pasture report_j because she could not find a path that does not go through damaged pastures.

After all the cows report in, determine the minimum number of

pastures that are damaged.

地震袭击了威斯康星州,一些牧场被摧毁了.

一共有P个牧场.由C条双向路连接.两个牧场间可能有多条路.一条路也可能连接相同的牧场.牛棚坐落在牧场1.

N (1 <= N <= P) 只奶牛打来了求救电话,说她们的农场没有被摧毁,但是已经无法到达牛棚. 求出最少可能有多少牧场被摧毁.

输入输出格式

输入格式:

 

  • 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

 

输出格式:

 

  • Line 1: One number, the minimum number of damaged pastures.

 

输入输出样例

输入样例#1:
5 5 2 1 2 2 3 3 5 2 4 4 5 4 5
输出样例#1:
1

说明

Only pasture 2 being damaged gives such a scenario.

 

拆点!!!

#include
#include
#define N 3101#define M 21001using namespace std;const int inf=2e9;int n,c,p;int src,decc,ans;int front[N*2],to[M*5],nxt[M*5],cap[M*5],tot=1;int cnt[N*2],lev[N*2];queue
q;void add(int u,int v,int w){ to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w; to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; cap[tot]=0;}bool bfs(){ for(int i=0;i<=p*2+1;i++) lev[i]=-1,cnt[i]=front[i]; while(!q.empty()) q.pop(); q.push(src); lev[src]=0; int now; while(!q.empty()) { now=q.front(); q.pop(); for(int i=cnt[now];i;i=nxt[i]) if(lev[to[i]]==-1 && cap[i]) { lev[to[i]]=lev[now]+1; q.push(to[i]); if(to[i]==decc) return true; } } return false;}int dinic(int now,int flow){ if(now==decc) return flow; int delta,rest=0; for(int &i=cnt[now];i;i=nxt[i]) { if(cap[i] && lev[to[i]]>lev[now]) { delta=dinic(to[i],min(flow-rest,cap[i])); if(delta) { rest+=delta; cap[i]-=delta; cap[i^1]+=delta; if(rest==flow) break; } } } if(rest!=flow) lev[now]=-1; return rest;}int main(){ scanf("%d%d%d",&p,&c,&n); decc=1; int u,v; for(int i=2;i<=p;i++) add(i<<1|1,i<<1,1); for(int i=1;i<=c;i++) { scanf("%d%d",&u,&v); if(u==v) continue; if(u==1) add(v<<1,1,inf); else if(v==1) add(u<<1,1,inf); else { add(u<<1,v<<1|1,inf); add(v<<1,u<<1|1,inf); } } for(int i=1;i<=n;i++) { scanf("%d",&u); add(src,u<<1,inf); } while(bfs()) ans+=dinic(src,inf); printf("%d",ans);}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/7039726.html

你可能感兴趣的文章
一款基于jquery和css3的响应式二级导航菜单
查看>>
JMeter学习(二十三)关联
查看>>
【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好
查看>>
通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法
查看>>
sdut AOE网上的关键路径(spfa+前向星)
查看>>
C++编程思想重点笔记(上)
查看>>
【转发】什么时候该用委托,为什么要用委托,委托有什么好处
查看>>
[原]VS2012编译GLEW 1.11
查看>>
[AngularJS] Hijacking Existing HTML Attributes with Angular Directives
查看>>
关于android.view.WindowLeaked(窗体泄露)的解决方案
查看>>
微软职位内部推荐-Software Engineer II-News
查看>>
(转)I 帧和 IDR 帧的区别
查看>>
如何更快速加载你的JS页面
查看>>
解决oracle11g安装导致数据库无法自动搜集统计信息-转
查看>>
Unix_Linux系统定时器的应用(案例)
查看>>
[Java基础] Java如何实现条件编译
查看>>
【转】ubuntu 12.04 下 Vim 插件 YouCompleteMe 的安装
查看>>
设置网页标题图标
查看>>
mysql通过查看跟踪日志跟踪执行的sql语句
查看>>
Android_CodeWiki_01
查看>>