view code//hdu 3987
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const ll INF = 1LL<<59;
const ll E = 100001;
const int N = 1010;
int _, n, m, pre[N], cur[N], d[N], s, t;
bool vis[N]; struct edge
{
int u, v;
ll cap, flow;
int next;
edge() {}
edge(int u, int v, ll w, ll f, int p):u(u),v(v),cap(w),flow(f),next(p) {}
}e[N*400];
int ecnt; void addedge(int u, int v, int w)
{
e[ecnt] = edge(u, v, w*E+1, 0, pre[u]);
pre[u] = ecnt++;
e[ecnt] = edge(v, u, 0, 0, pre[v]);
pre[v] = ecnt++;
} bool BFS()
{
memset(vis, 0, sizeof(vis));
queue<int >q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
for(int i=pre[x]; ~i; i = e[i].next)
{
int v =e[i].v;
if(!vis[v] && e[i].cap>e[i].flow)
{
d[v] = d[x] + 1;
vis[v] = 1;
q.push(v);
}
}
}
return vis[t];
} ll DFS(int x, ll c)
{
if(x==t || c==0) return c;
ll flow = 0, f;
for(int &i = cur[x]; ~i; i=e[i].next)
{
int v = e[i].v;
if(d[x] + 1 == d[v] && (f=DFS(v, min(c, e[i].cap-e[i].flow)))>0)
{
e[i].flow += f;
e[i^1].flow -= f;
flow += f;
c -= f;
if(c==0) break;
}
}
return flow;
} ll Maxflow()
{
s = 0, t = n-1;
ll flow = 0;
while(BFS())
{
for(int i=s; i<=t; i++) cur[i] = pre[i];
flow += DFS(s, INF);
}
return flow;
} void solve()
{
scanf("%d%d", &n, &m);
int u, v, w, flag;
memset(pre, -1, sizeof(pre));
ecnt = 0;
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d", &u, &v, &w, &flag);
addedge(u, v, w);
if(flag) addedge(v, u, w);
}
static int cas=1;
printf("Case %d: ", cas++);
cout<<Maxflow()%E<<endl;
} int main()
{
// freopen("in.txt", "r", stdin);
cin>>_;
while(_--) solve();
return 0;
} /*
题意: 求最小割,但因为最小割是不唯一的,题目要求得到最小割的条件下,使得割边最少,输出最少割边数 思路: 有两种做法,但本质是一样的 第一种:
建边的时候每条边权 w = w * (E + 1) + 1;
这样得到最大流 maxflow / (E + 1) ,最少割边数 maxflow % (E + 1) 道理很简单,如果原先两类割边都是最小割,那么求出的最大流相等
但边权变换后只有边数小的才是最小割了 乘(E+1)是为了保证边数叠加后依然是余数,不至于影响求最小割的结果 第二种: 建图,得到最大流后,图中边若满流,说明该边是最小割上的边 再建图,原则:满流的边改为容量为 1 的边,未满流的边改为容量 INF 的边,然后最大流即答案
*/

hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割的更多相关文章

  1. HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)

    Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/ ...

  2. 【hdu 3987】Harry Potter and the Forbidden Forest

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=3987 [Description] 给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路 ...

  3. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. HDU3987 Harry Potter and the Forbidden Forest(边数最少的最小割)

    方法1:两遍最大流.一遍最大流后,把满流边容量+1,非满流边改为INF:再求最小割即为答案. 我大概想了下证明:能构成最小割的边在第一次跑最大流时都满流,然后按那样改变边容量再求一次最小割,就相当于再 ...

  5. HDU 6214 Smallest Minimum Cut(最少边最小割)

    Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...

  6. hdu 3657 最大点权独立集变形(方格取数的变形最小割,对于最小割建图很好的题)

    转载:http://blog.csdn.net/cold__v__moon/article/details/7924269 /* 这道题和方格取数2相似,是在方格取数2的基础上的变形. 方格取数2解法 ...

  7. HDU - 3035 War(对偶图求最小割+最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3035 题意 给个图,求把s和t分开的最小割. 分析 实际顶点和边非常多,不能用最大流来求解.这道题要用 ...

  8. HDU 6214 Smallest Minimum Cut 最小割,权值编码

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...

  9. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

随机推荐

  1. Ubuntu系统操作快捷键

    Ubuntu操作基本快捷键* 打开主菜单 = Alt + F1* 运行 = Alt + F2* 显示桌面 = Ctrl + Alt + d* 最小化当前窗口 = Alt + F9* 最大化当前窗口 = ...

  2. sql server聚合函数sum计算出来为空,怎样返回0

    通常我们计算数据库中表的数据有几个常用的聚合函数 1.count : 计数 2.sum: 计算总和 3.avg: 取平均值 4.max: 取最大值 5.min: 取最小值 6.isnull: 当返回数 ...

  3. vs2012中怎样设为起始页,怎样取消

    把你要设为起始页的文件右键选择为起始页,如下:

  4. jsp iframe example

    1. jsp中用iframe的方式在body中展示列表, 可以通过父元素的宽.高来设定iframe的宽高. <div class="wrapper" style=" ...

  5. redis 慢日志 slowlog

    1 slowlog是什么 redis的slowlog是redis用于记录记录慢查询执行时间的日志系统.由于slowlog只保存在内存中,因此slowlog的效率很高,完全不用担心会影响到redis的性 ...

  6. [转]MVC、MVP、MVVM

    界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras  作者:戴嘉华 [字体:大 中 小]   前言 做客户端开发.前端开发 ...

  7. [.NET] 使用C#开发SQL Function来提供服务 - 简讯发送

    [.NET] 使用C#开发SQL Function来提供服务 - 简讯发送 范例下载 范例程序代码:点此下载 问题情景 在「使用C#开发SQL Function来提供数据 - 天气预报」这篇文章中,介 ...

  8. ASP.NET本质论第一章网站应用程序学习笔记1

    1.统一资源标示符 1) 格式:协议://主机[.端口号][绝对路径[?参数]],在Http://www.kencery.com/hyl/index/login中,http表示协议的名称,www.ke ...

  9. log4net学习笔记

    一直想找一个好用的日子类,今天偶然的机会看到了log4net这个类库,过来学习一下. log4net是.NET框架下的一个日子类库,官网是http://logging.apache.org/log4n ...

  10. CSS3中的calc()

    什么是calc()? calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能; MDN的解释为可以用在任何长度,数值,时间,角度,频率等处; /* property: calc ...