将一个无向图分成许多回路,回路点交集为空,点幷集为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. 自定义li项目符号

    使用background-image属性 先清除ul的默认list-style ul{ list-style:none } li{ background-image:url('./image/symb ...

  2. html 中的列表

    html 中列表可以分为 1.  无序列表(ul--li 的形式) 2.  有序列表(ol li的形式) 3.  定义列表(dl 的形式) 下面来看几种列表的具体内容: 1.无序列表. 无序列表的格式 ...

  3. Traffic-Server配置(待补充和更新)

    Server 5.3.2 测试1.裸盘:remap.configmap http://192.168.227.131 http://192.168.227.131:8080 #traffic_serv ...

  4. 分享6款国内、外开源PHP轻论坛CMS程序

    第一.Startbbs Startbbs,一款国产个人兴趣分享的轻论坛程序,采用PHP+MYSQL架构,目前版本是V1.1.5,之前我也 有搭建使用过功能还是比较简单的,默认风格比较让普通用户接受,这 ...

  5. Deep Learning基础--随时间反向传播 (BackPropagation Through Time,BPTT)推导

    1. 随时间反向传播BPTT(BackPropagation Through Time, BPTT) RNN(循环神经网络)是一种具有长时记忆能力的神经网络模型,被广泛用于序列标注问题.一个典型的RN ...

  6. js获取系统时间

    //------------------------------------获取系统日期时间 var oDate=new Date(); //alert(oDate.getFullYear());// ...

  7. ajax刷新输出实时数据

    setInterval('shuaxin()',3000); function shuaxin(){ $.ajax({//股票 url:"http://apimarkets.wallstre ...

  8. 非常粗糙的react网页ppt

    import React, {Component} from 'react'; import './slide.css'; class Page extends Component { constru ...

  9. email的传输协议与格式(资源链接)

    以下链接为转载. 传输协议: 发:SMTP 收:POP3, IMAP 格式: MIME

  10. mac环境下使用brew安装kafka

    1.安装kafka brew install kafka note: ·kafka使用zookeeper管理,安装过程会自动安装zookeeper ·安装目录:/usr/local/Cellar/ka ...