网络流/最大权闭合子图


  胡伯涛论文里有讲……

  sigh……细节处理太伤心了,先是count和ans输出弄反了,改过来顺序时又忘了必须先算出来ans!要是不执行一下Dinic的话count就无意义了……然后就是long long的问题……傻逼题白白WA了6次……sigh果然不能晚上搞……

 Source Code
Problem: User: sdfzyhy
Memory: 2664K Time: 344MS
Language: G++ Result: Accepted Source Code //POJ 2987
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define pb push_back
using namespace std;
inline int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,M=,INF=~0u>>;
typedef long long LL;
/******************tamplate*********************/
int n,m;
LL ans;
struct edge{
int from,to;
LL v;
};
struct Net{
edge E[M];
int head[N],next[M],cnt;
void add(int x,int y,LL v){
E[++cnt]=(edge){x,y,v};
next[cnt]=head[x]; head[x]=cnt;
E[++cnt]=(edge){y,x,};
next[cnt]=head[y]; head[y]=cnt;
}
int s,t,cur[N],d[N],Q[N];
void init(){
n=getint();m=getint();
ans=;cnt=;
s=;t=n+;
LL x,y;
F(i,,n){
x=getint();
if (x>=) {add(s,i,x);ans+=x;}
else add(i,t,-x);
}
F(i,,m){
x=getint(); y=getint();
add(x,y,INF);
}
}
bool mklevel(){
memset(d,-,sizeof d);
d[s]=;
int l=,r=-;
Q[++r]=s;
while(l<=r){
int x=Q[l++];
for(int i=head[x];i;i=next[i])
if (d[E[i].to]==- && E[i].v){
d[E[i].to]=d[x]+;
Q[++r]=E[i].to;
}
}
return d[t]!=-;
}
LL dfs(int x,LL a){
if (x==t||a==) return a;
LL flow=;
for(int &i=cur[x];i && flow<a;i=next[i])
if (d[E[i].to]==d[x]+ && E[i].v){
LL f=dfs(E[i].to,min(a-flow,E[i].v));
E[i].v-=f;
E[i^].v+=f;
flow+=f;
}
if (!flow) d[x]=-;
return flow;
}
LL Dinic(){
LL flow=;
while(mklevel()){
F(i,s,t) cur[i]=head[i];
flow+=dfs(s,INF);
}
return flow;
}
bool vis[N];
void dfs(int x){
vis[x]=;
for(int i=head[x];i;i=next[i])
if (!vis[E[i].to] && E[i].v) dfs(E[i].to);
}
void solve(){
ans=ans-Dinic();
int count=;
memset(vis,,sizeof vis);
dfs(s);
F(i,,n) if (vis[i]) count++;
printf("%d %lld\n",count,ans);
}
}G1;
int main(){
#ifndef ONLINE_JUDGE
freopen("2987.in","r",stdin);
freopen("2987.out","w",stdout);
#endif
G1.init();
G1.solve();
return ;
}

【POJ】【2987】Firing的更多相关文章

  1. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  2. 【POJ 1459 power network】

    不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...

  3. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  4. 【POJ 2976 Dropping tests】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...

  5. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  6. 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)

    1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...

  7. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  8. 【POJ 2823 Sliding Window】 单调队列

    题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...

  9. 【POJ 2406 Power Strings】

    Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...

  10. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

随机推荐

  1. 微软的COM中GUID和UUID、CLSID、IID

    摘自:http://blog.csdn.net/zhongguoren666/article/details/6711396 当初微软设计com规范的时候,有两种选择来保证用户的设计的com组件可以全 ...

  2. C#判断奇偶数的函數

    // 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...

  3. 将slider滑块从横着变为竖着的时候坐标变换的计算

    ////  ViewController.m//  imageview添加按钮////  Created by hehe on 15/9/22.//  Copyright (c) 2015年 wang ...

  4. python基础:自定义函数

    一.背景 在学习函数之前,一直遵循:面向过程编程,即:根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复制到现需功能处,如下 ...

  5. HTTP Status 500 - An exception occurred processing JSP page /WEB-INF

    HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/test/showCountry.jsp at line 11 ...

  6. JavaScript选项卡

    实现js选项卡 html的代码如下: <div class="tabdiv"> <ul class="tabs" id="oTab& ...

  7. SpringMvc入门二----HelloWorld

    1. 导入需要的架包: 2. 配置web.xml,添加Servlet <servlet> <servlet-name>springmvc</servlet-name> ...

  8. HttpWebResponse取不到Cookie?原来是因为被跳转了

    今天做模拟登陆的时候,发现HttpWebResponse的Cookie都为空,但是Fiddler看是有的...后来看见是302状态,才知道请求这个的时候,Response回来已经是跳转了...这样Co ...

  9. 《iptables详解 》RHEL6

          iptables详解    Iptables原理 现在防火墙主要分以下三种类型:包过滤.应用代理.状态检测 包过滤防火墙:现在静态包过滤防火墙市面上已经看不到了,取而代之的是动态包过滤技术 ...

  10. WP开发笔记——字符串 转 MD5 加密

    将字符串进行MD5加密,返回加密后的字符串. 从这里下载Md5.cs文件:http://pan.baidu.com/s/1hq3gpnu. 添加到Windows Phone 7项目中,在代码里面这样调 ...