网络流/最大权闭合子图


  胡伯涛论文里有讲……

  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. 两种获取connectionString的方式

    两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...

  2. 下拉框的选择跳转事件(jqure)

    <select id="coupontype"> <option value="@CouponType.全部" href="@Url ...

  3. PHP的final、抽象类和方法

    final关键字的用法 final class Computer{ //无法继承的类 final public function run(){ //无法继承的方法 } } class NoteBook ...

  4. ionic2 Navigation实现报错:No component factory found for "MyComponent"

    ionic2 写的代码里面,跳转的时候报了一个 No component factory found for "RechargeSucceed" recharge() { let ...

  5. hdu 1318 Palindromes(回文词)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1318 题意分析:输入每行包含一个字符串,判断此串是否为回文串或镜像串. 表面上看这道题有些复杂,如果能 ...

  6. Socket 广播

    1.广播端口 Socket中的广播端口是什么意思,是谁对应谁的? 这个广播端口 指定 客户端接收广播消息时要使用的端口号. 参考: 1.快速Python 原型 2.receive UDP broadc ...

  7. 【风马一族_Python】 决策树

    <机器学习实战>第三章 决策树 ------------------------------------- #1 trees.py 计算给定数据集的香农熵 ---------------- ...

  8. 支持IE6的树形节结构TreeTable

    关于TreeTable实际应用的案例:http://www.cnblogs.com/qigege/p/5213689.html treeTable是跨浏览器.性能很高的jquery的树表组件,它使用非 ...

  9. 批处理bat命令--获取当前盘符和当前目录和上级目录

    批处理bat命令--获取当前盘符和当前目录和上级目录 批处理命令获取当前盘符和当前目录%~d0 是当前盘符%cd% 是当前目录可以用echo %cd%进行打印测试 以下例子是命令行编译Visual S ...

  10. RxJava 平常使用

    本文转载自: http://blog.csdn.net/theone10211024/article/details/50435325 一.Scheduler线程切换 这种场景经常会在“后台线程取数据 ...