hdu 3987 Harry Potter and the Forbidden Forest
来源:互联网 发布:冰川网络 游戏 编辑:程序博客网 时间:2024/05/18 09:03
【分析】
网络流好题
【代码】
#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; else f[i].flow=1e9+7,f[i+1].flow=0; while(bfs()) ans+=find(s,inf); printf("Case %d: %lld\n",r,ans); } return 0;}
1 0
- HDU 3987 Harry Potter and the Forbidden Forest 最小割
- hdu 3987 Harry Potter and the Forbidden Forest
- hdu 3987 Harry Potter and the Forbidden Forest
- 【HDU】3987 Harry Potter and the Forbidden Forest 最小割
- 【最小割】HDU 3987 Harry Potter and the Forbidden Forest
- hdu 3987 Harry Potter and the Forbidden Forest 最小割
- HDU 3987 Harry Potter and the Forbidden Forest
- hdu 3987 Harry Potter and the Forbidden Forest
- HDU 3987 Harry Potter and the Forbidden Forest
- HDU--3987[Harry Potter and the Forbidden Forest] 求最小割集中的最小边数
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
- HDU 3987 Harry Potter and the Forbidden Forest (求割边最少的最小割)
- hdu 3987 Harry Potter and the Forbidden Forest【网路流最小割模型】
- HDU 3987 Harry Potter and the Forbidden Forest(最小割中的最少割边)经典
- hdu 3987 Harry Potter and the Forbidden Forest (最小割)
- HDU 3987 Harry Potter and the Forbidden Forest(最小割的最少割边数)
- HDU-3987 Harry Potter and the Forbidden Forest(最大流)
- hdu3987 Harry Potter and the Forbidden Forest 最小割割边最少
- PGM格式图像详解
- JavaWeb forward与sendRedirect区别
- Spring事务之一(JDBC简介)
- ubuntu 安装 libpcap、libnet、libnids
- [机器学习](九)cs229之独立分量分析
- hdu 3987 Harry Potter and the Forbidden Forest
- DataGridView添加行号
- SpringMVC拦截器实现登录控制
- Python进阶(四十三)-Python模块学习:glob 文件路径查找
- 堆排序 Heap Sort
- poj3041_匈牙利算法_二分图最小覆盖点(最大匹配数)
- Link 标签 rel=Stylesheet的实际作用
- Python在工作中的应用
- java播放音频文件2017.4.20绝对有效 粘贴运行!