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. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  2. Discuz DB层跨库映射关系表名前缀BUG修复后产生的新bug

    新的逻辑引入了新的bug,会导致在跨多库连接时,产生表名前缀映射混乱,需要再做逻辑上的修复. function table_name($tablename) { if(!empty($this-> ...

  3. Servlet-中文乱码

    背景 从Tomcat5.x开始,GET,POST方法提交信息,Tomcat采用不同的方式来处理编码. 对于GET请求,Tomcat不会考虑使用request.setCharacterEncoding( ...

  4. mybatis mapper association collection

    1.Question Description: sometimes, POJO bean contains another bean or collection as property, it's s ...

  5. IIS减少工作线程阻塞的方法

    IIS的工作进程(w3wp.exe)只提供了有限的工作线程(Work Thread)来处理请求.如果这些线程都因为要等待长时间运行的任务而阻塞,则运行时会将新来的请求排队,而不是立即执行,Web服务器 ...

  6. 硅谷新闻4--解决页签手指按下从左到右滑动的bug

    有一种方法可以阻止父层的View截获touch事件,就是调用 getParent().requestDisallowInterceptTouchEvent(true);方法.一旦底层View收到tou ...

  7. Java中的GOF23(23中设计模式)--------- 工厂模式(Factory)

    Java中的GOF23(23中设计模式)--------- 工厂模式(Factory) 在给大家介绍工厂模式之前,我想和大家聊聊面向对象的那点事,在这里,引入三个概念. 开闭原则(Open Close ...

  8. .net经验积累

    希望对.net编程者有所帮助 1.学会配置环境变量  1.我的电脑-属性-环境变量-双击下面的path-粘贴路径  2.ctrl+r 输入软件名字按回车 2.常用vs2010快捷键  代码格式化:ct ...

  9. Java 进程占用 VIRT 虚拟内存超高的问题研究

    1. 现象 最近发现线上机器 java 8 进程的 VIRT 虚拟内存使用达到了 50G+,如下图所示: 2. 不管用的 -Xmx 首先第一想到的当然使用 java 的 -Xmx 去限制堆的使用.但是 ...

  10. UML类图相关实践

    最近看了下设计模式,其中无可避免会设计很多类图,UML类图对于学习设计模式很重要,关于设计模式,我也会在这里写上一写,这一篇关于UML类图的就先当个铺垫. 1.先上一个简单的类图来简单说明下: 1). ...