hdu3987 Harry Potter and the Forbidden Forest 最小割边数
来源:互联网 发布:hadoop wordcount源码 编辑:程序博客网 时间:2024/06/06 16:39
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=3987
题意:
给一个网络,边有有向边和无向边两种,挑选一些边破坏,使得从0无法到达n-1,每条边都有一个破环的成本,求在成本最低的情况下需要破坏的最少边数
思路:
明显最小成本就是最小割,求边数的话,扩大边权然后取余即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1000 + 10;const ll INF = 0x3f3f3f3f3f3f3f3f;struct edge{ int to, next; ll cap;}g[N*N*2];int level[N], cur[N], pre[N], gap[N];int cnt, head[N];int nv;void init(){ cnt = 0; memset(head, -1, sizeof head);}void add_edge(int v, int u, ll cap){ g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++; g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;}ll sap(int s, int t){ memset(level, 0, sizeof level); memset(gap, 0, sizeof gap); memcpy(cur, head, sizeof head); gap[0] = nv; int v = pre[s] = s; ll flow = 0, aug = INF; while(level[s] < nv) { bool flag = false; for(int &i = cur[v]; i != -1; i = g[i].next) { int u = g[i].to; if(g[i].cap > 0 && level[v] == level[u] + 1) { flag = true; pre[u] = v; v = u; aug = min(aug, g[i].cap); if(v == t) { flow += aug; while(v != s) { v = pre[v]; g[cur[v]].cap -= aug; g[cur[v]^1].cap += aug; } aug = INF; } break; } } if(flag) continue; int minlevel = nv; for(int i = head[v]; i != -1; i = g[i].next) { int u = g[i].to; if(g[i].cap > 0 && level[u] < minlevel) minlevel = level[u], cur[v] = i; } if(--gap[level[v]] == 0) break; level[v] = minlevel + 1; gap[level[v]]++; v = pre[v]; } return flow;}int main(){ int t, n, m, cas = 0; scanf("%d", &t); while(t--) { init(); scanf("%d%d", &n, &m); int a, b, c, d; for(int i = 1; i <= m; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); a++, b++; add_edge(a, b, 1LL*c*(m+1) + 1); if(d) add_edge(b, a, 1LL*c*(m+1) + 1);//边权扩大(m+1)倍再+1 } int ss = 1, tt = n; nv = n; printf("Case %d: %lld\n", ++cas, sap(ss, tt) % (m+1)); } return 0;}
阅读全文
0 0
- hdu3987 Harry Potter and the Forbidden Forest 最小割边数
- HDU3987:Harry Potter and the Forbidden Forest(最小割边数)
- hdu3987 Harry Potter and the Forbidden Forest 最小割割边最少
- HDU3987 Harry Potter and the Forbidden Forest(最小割)
- HDU3987 Harry Potter and the Forbidden Forest最小割最少边Dinic
- 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] 求最小割集中的最小边数
- Harry Potter and the Forbidden Forest(割边最小的最小割)
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
- HDU 3987 Harry Potter and the Forbidden Forest (求割边最少的最小割)
- HDOJ 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 (最小割)
- PostgreSQL 连接串URI配置(libpq兼容配置)
- html 编写的简单计算器
- 电机的MCPWM控制
- C++学习笔记 23
- MediaPlayer的使用
- hdu3987 Harry Potter and the Forbidden Forest 最小割边数
- ORACLE分区表的概念及操作
- Oracle坏块问题处理 Oracle坏块修复 Oracle坏块怎么办
- 获取object类型里的键值
- linux grep查看指定内容上下几行
- 功能最全面 体验更极致 小兴看看mini升级版9月19日首发
- Elasticsearch JAVA API开发记录
- post方式与get方式提交到Jsp页面中文乱码的处理
- 程序员编程生涯中常会犯的7个错误