将一个无向图分成许多回路,回路点交集为空,点幷集为V。幷最小化回路边权和。

 #include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define maxn 2010
#define oo 0x3f3f3f3f
using namespace std; struct Edge {
int u, v, c, f;
Edge( int u, int v, int c, int f ):u(u),v(v),c(c),f(f){}
};
struct Mcmf {
int n, src, dst;
vector<Edge> edge;
vector<int> g[maxn];
int dis[maxn], pth[maxn], ext[maxn]; void init( int n, int src, int dst ) {
this->n = n;
this->src = src;
this->dst = dst;
for( int u=; u<=n; u++ )
g[u].clear();
edge.clear();
}
void add_edge( int u, int v, int c, int f ) {
g[u].push_back( edge.size() );
edge.push_back( Edge(u,v,c,f) );
g[v].push_back( edge.size() );
edge.push_back( Edge(v,u,-c,) );
}
bool spfa( int &flow, int &cost ) {
queue<int> qu;
memset( dis, 0x3f, sizeof(dis) );
qu.push( src );
dis[src] = ;
pth[src] = -;
ext[src] = true;
while( !qu.empty() ) {
int u=qu.front();
qu.pop();
ext[u] = false;
for( int t=; t<g[u].size(); t++ ) {
Edge &e = edge[g[u][t]];
if( e.f && dis[e.v]>dis[e.u]+e.c ) {
dis[e.v] = dis[e.u]+e.c;
pth[e.v] = g[u][t];
if( !ext[e.v] ) {
ext[e.v] = true;
qu.push( e.v );
}
}
}
}
if( dis[dst]==oo ) return false;
int flw = oo;
for( int eid=pth[dst]; eid!=-; eid=pth[edge[eid].u] )
flw = min( flw, edge[eid].f );
for( int eid=pth[dst]; eid!=-; eid=pth[edge[eid].u] ) {
edge[eid].f -= flw;
edge[eid^].f += flw;
}
flow += flw;
cost += flw*dis[dst];
return true;
}
bool mcmf( int &flow, int &cost ) {
flow = cost = ;
while(spfa(flow,cost));
return true;
}
}; int n, m;
Mcmf M; int main() {
int T;
scanf( "%d", &T );
for( int cas=; cas<=T; cas++ ) {
printf( "Case %d: ", cas );
scanf( "%d%d", &n, &m );
M.init( n+n+, n+n+, n+n+ );
for( int i=,u,v,w; i<=m; i++ ) {
scanf( "%d%d%d", &u, &v, &w );
M.add_edge( u, v+n, w, );
M.add_edge( v, u+n, w, );
}
for( int u=; u<=n; u++ ) {
M.add_edge( M.src, u, , );
M.add_edge( u+n, M.dst, , );
}
int flow, cost;
M.mcmf( flow, cost );
if( flow!=n ) printf( "NO\n" );
else printf( "%d\n", cost );
}
}

hdu 3435 图回路分割的更多相关文章

  1. 【HDU 3435】 A new Graph Game (KM|费用流)

    A new Graph Game Problem Description An undirected graph is a graph in which the nodes are connected ...

  2. [Ctsc2014]图的分割

    [Ctsc2014]图的分割 阅读理解好题 翻译一下: M(C)就是C这个诱导子图最小生成树最大边权 结论: 按照w进行sort,如果满足w<=Ci,Cj表示u,v的连通块的诱导子图 并且Ci! ...

  3. hdu 3435 A new Graph Game

    http://acm.hdu.edu.cn/showproblem.php?pid=3435 #include <cstdio> #include <iostream> #in ...

  4. HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用 ...

  5. HDU 2050(折线分割平面)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2050 折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    ...

  6. HDU 2050:折线分割平面

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. HDU 3435 KM A new Graph Game

    和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...

  8. HDU 1249 三角形的分割

    可以将三角形的三条边一条一条加进图形中观察 假设添加第n个三角形 前n-1个三角形将区域划分为sum[n-1] 第n个三角形每条边最多能经过前n-1个三角形每条三角形的两条边 , 一条边切完增加了 2 ...

  9. BZOJ3559 : [Ctsc2014]图的分割

    考试的时候看少了一行,导致暴力都写错额… 贾教说他出的这题水,但是我觉得并不水,那个结论还是很神的. 首先M(i)就是i的最小生成树的最大边, 设f[i]表示i属于哪个集合 我们把边按权值从小到大排序 ...

随机推荐

  1. LeetCode之数据流中第一个唯一的数字

    使用一个Map维护数字出现的次数,使用一个链表维护只出现一次的数,使用一个变量记录是否找到过终止数字. AC代码: public class Solution { /* * @param : a co ...

  2. WAMP允许外部访问的修改方法

    apache配置文件httpd.conf里的 "Require local"改" Require all granted"

  3. WordPress手机端插件——WPtouch

    戒微博之后,把更多的精力开始转投回网站上来:今天用nexus7访问@Bee君 的博客时,发现博客的界面与电脑上访问的界面不相同,顺藤摸瓜之后发现原来bee君使用的是WPtouch-pro插件来实现移动 ...

  4. [转载]Android中Bitmap和Drawable

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  5. python基础===数据伪造模块faker

    介绍文档: https://pypi.org/project/Faker/ https://faker.readthedocs.io/en/latest/ https://faker.readthed ...

  6. ubuntu sudoers改坏了。

    pkexec bash 通过如上命令,可以进入root模式,然后恢复sudoers.前提是root账户没被禁用. precise (1) pkexec.1.gz Provided by: policy ...

  7. angular项目文件概览

    在Mac上打开终端,输入ng new b-app  如下: 然后在webstorm中打开 src文件夹 你的应用代码位于src文件夹中. 所有的Angular组件.模板.样式.图片以及你的应用所需的任 ...

  8. fedroa20 没法开启ntpd服务器

    1现象:ntpd老是没法开启,ntpd -d显示有个进程占用123端口. [root@vd13crmtb01 ~]# systemctl enable ntpd.service         //开 ...

  9. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  10. pymongo的一些操作

    参考:http://www.yiibai.com/mongodb/mongodb_drop_collection.html http://www.cnblogs.com/zhouxuchen/p/55 ...