hdu 3987 Harry Potter and the Forbidden Forest
【分析】
网络流好题
【代码】
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e16
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1000005;
queue <int> q;
ll ans;
int s,t,n,m,T,cnt;
int x[mxn],y[mxn],c[mxn];
struct edge {int to,next,flow,from;} f[mxn<<1];
int head[mxn],dis[mxn];
inline void add(int u,int v,int flow)
{f[++cnt].to=v,f[cnt].from=u,f[cnt].next=head[u],f[cnt].flow=flow,head[u]=cnt;f[++cnt].to=u,f[cnt].from=v,f[cnt].next=head[v],f[cnt].flow=0,head[v]=cnt;
}
inline bool bfs()
{memset(dis,-1,sizeof dis);q.push(s);dis[s]=0;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i;i=f[i].next){int v=f[i].to;if(dis[v]==-1 && f[i].flow>0)dis[v]=dis[u]+1,q.push(v);}}return dis[t]>0;
}
inline ll find(int u,ll low)
{if(u==t) return low;ll sum=0,a=0;for(int i=head[u];i;i=f[i].next){int v=f[i].to;ll flow=f[i].flow;if( dis[v]==dis[u]+1 && flow>0 && (a=find(v,min(flow,low-sum))) ){sum+=a;f[i].flow-=a;if(i&1) f[i+1].flow+=a;else f[i-1].flow+=a;}}if(!sum) dis[u]=-1;return sum;
}
int main()
{int i,j,u,r,v,c,d;scanf("%d",&T);fo(r,1,T){scanf("%d%d",&n,&m);s=0,t=n-1,cnt=ans=0;M(head),M(dis),M(f);fo(i,1,m){scanf("%d%d%d%d",&u,&v,&c,&d);add(u,v,c);if(d) add(v,u,c);}while(bfs()) find(s,inf);for(i=1;i<=cnt;i+=2)if(f[i].flow==0)f[i].flow=1,f[i+1].flow=0;elsef[i].flow=1e9+7,f[i+1].flow=0;while(bfs()) ans+=find(s,inf);printf("Case %d: %lld\n",r,ans);}return 0;
}
hdu 3987 Harry Potter and the Forbidden Forest相关推荐
- 【最小割】HDU 3987 Harry Potter and the Forbidden Forest
得到的最小割得到sum sum/E 为 最小割 sum%E 为最小割的边数 #include <stdio.h> #include <string.h> #include &l ...
- HDU - 3987 Harry Potter and the Forbidden Forest(最小割最少边数)
题目链接:点击查看 题目大意:给出一个由n个点和m条边组成的图,求最小割的最小边数 题目分析:和hdu6214大同小异,都是模板题,这个题目用第一种方法,也就是先跑一遍最大流,然后修改一下残余网络上的 ...
- Forbidden Forest 错落森林
乔治温斯顿10首经典钢琴曲交响乐版-Forbidden Forest 一贯以来,许多古典音乐改编为新世纪音乐的专辑,除了保留原有的色彩和高贵内 涵,更拥有了灵气和动感.而现在让我们去感受一种用交响音乐 ...
- hdu 3987(最小割的边数)
题意:给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路,使得从点0无法到达点n-1.破坏每条路都有一个代价.求在代价最小的前提下,最少需要破坏多少条道路.(就是说求在最小割的前提下,最 ...
- ACM比赛经验、刷题记录及模板库总结(更新中)
前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...
- 网络流题集【夏天的风】
[HDU] 1532Drainage Ditches(基础) [最大流] 3549 Flow Problem(基础) [最大流] 3572 Task Schedule [最大流]任务 ...
- 杭电OJ分类题目(4)-Graph
原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...
- 大数据正式京淘附加爬虫
大数据正式京淘附加爬虫 爬虫技术 httpClient:抓取整个页面 htmlUnit:可以二次提交 jsoup:可以获取以上两个技术的所有内容 jsoup 爬取整个页面 爬取整个网站 爬取页面中的某 ...
- BUAA(2021春)实验:树的构造与遍历——根据提示循序渐进(可惜提示有问题Ծ‸Ծ)
BUAA数据结构第五次编程题 --实验:树的构造与遍历 看前须知 第五次上机题汇总 实验目的与要求 实验内容 Huffman编码文件压缩 问题描述 实验准备 实验步骤 [步骤1] [步骤2] [步骤3 ...
- 《哈利·波特:霍格沃茨之谜》游戏特推出万圣节内容致敬黑魔法
--(美国商业资讯)--Jam City: 本新闻稿包含多媒体.此处查看新闻稿全文: https://www.businesswire.com/news/home/20181008005012/zh- ...
最新文章
- xampp mysql 端口被占用_xampp安装后apache 80端口被占用的解决方法
- 蓝桥杯-合并石子(java)
- 九章算法班L3 Dynamic Programming Ⅰ
- 程序员吐槽_某程序员吐槽一程序员大佬竟然放弃百度offer,回老家进烟草公司!是不是脑子有坑?网友:你才脑子有坑!...
- 深度学习目标检测之 YOLO v3
- 怎么建立计算机共享文件夹,如何在局域网内建立共享文件夹
- php编辑页面代码,0069 PHP编程实现后台网页:新闻创建和编辑
- mac 切换鼠标滚轮方向
- 【Git】Git commit push的时候出现了两个分支
- python基础教程免费视频-零基础小白python从入门到精通视频(全60集)
- NRF24L01 无线模块
- 盘点互联网大厂的元宇宙布局
- 不忘初心,不负韶华,17款迈巴赫S400升级20款迈巴赫S680包围
- 19-图片标签注意点
- OCR文字识别软件如何操作?
- FPGA学习之路(一)之D类触发器(Data Flip-Flop/Delay Flip-Flop)搭建
- 电脑键盘部分按键失灵_电脑键盘失灵解决方法 电脑键盘部分失灵解决方法
- 12.2烤仔建工×MetaEstate | K歌之王在元宇宙的秘密据点
- 辽河十八汊最牛X男爷们
- Indian Summer