朱刘算法求无根最小树形图

可以任意选一个根,求最小的权和以及当时的根。

先建一个超级根,它连向所有点,边权为所有边的边权和加1(即sumw+1),然后求以它为根的最小树形图,再根据树形图权和与2*(sumw+1)的关系判断是否存在解(如果大于等于就不存在,否则存在)。

至于求对应的原图中的根,我们发现自始自终,超级根都不可能在一个环中,并且在最有一个状态,一定是一个没有环的树形图,该图中与前趋为超级根的点,就是原图中的根所在的环缩成的点,怎么得到具体是哪一个点呢,我们可以记下那条边在最开始指向的是哪个点,那个点就是原图中的根(可以根据缩点的正确性证明它的正确性)。

 #include <cstdio>
#define oo 0x7FFFFFFF
#define N 1010
#define M 11010 struct Edge {
int u, v, w;
int sv;
Edge(){}
Edge( int u, int v, int w ):u(u),v(v),w(w),sv(v){}
}; int n, m;
int inw[N], inc[N], pre[N], idx[N], vis[N], rsv;
Edge edge[M]; int directed_mst( int root ) {
int rt = ;
while() {
// inw pre
for( int i=; i<=n; i++ )
inw[i] = oo;
for( int i=; i<=m; i++ ) {
Edge &e = edge[i];
if( inw[e.v]>e.w ) {
inw[e.v]=e.w;
pre[e.v]=e.u;
if( e.u==root ) rsv=e.sv;
}
}
// inc idx
int cnt = ;
for( int i=; i<=n; i++ )
idx[i] = vis[i] = ;
for( int i=,u,v; i<=n; i++ ) {
if( i==root ) continue;
for( u=pre[i]; u!=root && !idx[u] && vis[u]!=i; u=pre[u] )
vis[u]=i;
if( u==root || idx[u] ) continue;
cnt++;
for( v=pre[u]; v!=u; v=pre[v] ) {
idx[v] = cnt;
inc[v] = true;
rt += inw[v];
}
idx[u] = cnt;
inc[u] = true;
rt += inw[u];
}
if( cnt== ) {
for( int i=; i<=n; i++ )
if( i!=root )
rt += inw[i];
break;
} else {
for( int i=; i<=n; i++ )
if( !idx[i] ) {
idx[i] = ++cnt;
inc[i] = false;
}
}
// edge
int j=;
for( int i=; i<=m; i++ ) {
Edge &e = edge[i];
if( inc[e.v] ) e.w-=inw[e.v];
e.u = idx[e.u];
e.v = idx[e.v];
if( e.u!=e.v ) edge[++j]=edge[i];
}
root = idx[root];
n = cnt;
m = j;
}
return rt;
} int main() {
while( scanf("%d%d",&n,&m)== ) {
int mm=, sw=;
for( int i=,u,v,w; i<=m; i++ ) {
scanf( "%d%d%d", &u, &v, &w );
if( u==v ) continue;
u++, v++;
edge[++mm] = Edge(u,v,w);
sw+=w;
}
for( int i=; i<=n; i++ )
edge[++mm] = Edge(n+,i,sw+);
m = mm;
n = n+;
int ans = directed_mst(n)-sw-;
if( ans>=sw+ ) printf( "impossible\n" );
else printf( "%d %d\n", ans, rsv- );
printf( "\n" );
}
}

hdu 2121的更多相关文章

  1. hdu 2121 , hdu 4009 无定根最小树形图

    hdu 2121 题目:给出m条有向路,根不确定,求一棵最小的有向生成树. 分析:增加一个虚拟节点,连向n个节点,费用为inf(至少比sigma(cost_edge)大).以该虚拟节点为根求一遍最小树 ...

  2. HDU 2121 Ice_cream’s world II 最小树形图 模板

    开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  3. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. HDU - 2121 Ice_cream’s world II 无根最小树形图

    HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...

  5. hdu 2121 Ice_cream’s world II

    Ice_cream’s world II http://acm.hdu.edu.cn/showproblem.php?pid=2121 Time Limit: 3000/1000 MS (Java/O ...

  6. hdu 2121 Ice_cream’s world II (无定根最小树形图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...

  7. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. HDU 2121 Ice_cream’s world II 最小树形图

    这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...

  9. hdu 2121无根最小树形图要建一个虚拟节点

    #include<stdio.h> #include<string.h> #define inf 999999999 #define N 1100 struct node { ...

随机推荐

  1. Add Two Numbers I & II

    Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...

  2. FPGA quartus开发中常见的错误处理

    1.Warning: An incorrect timescale is selected for the Verilog Output (.VO) file of this PLL design. ...

  3. Linux移植随笔:对tslib库的ts_test测试程序代码的一点分析【转】

    转自:http://www.latelee.org/embedded-linux/porting-linux-tstest-code.html 本文是作者对tslib库的ts_test.c文件进行分析 ...

  4. nodejs 接收上传的图片

    1.nodejs接收上传的图片主要是使用formidable模块,服务器是使用的express搭建. 引入formidable var formidable = require('./node_mod ...

  5. 使用html+css+js实现魔性的舞蹈

    使用html+css+js实现魔性的舞蹈,让我们燥起来!!! 效果图: 代码如下,复制代码即可使用: <!DOCTYPE html> <html > <head> ...

  6. 命令行执行Django脚本

    命令行执行Django脚本 - #效仿manage.py加入的环境变量在脚本的文件加入 - #手动注册django所有的APP import sys,os ---------------------- ...

  7. CVE-2012-4792Microsoft Internet Explorer 释放后使用漏洞

    Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器. Microsoft Internet Explorer 6至8版本中存在释放后使用漏洞.通 ...

  8. 12 打印1到最大的n位数

    输入数字 n,按顺序打印出从 1 最大的 n 位十进制数.比如输入 3,则打印出 1.2.3 一直到最大的 3 位数即 999.由于 n 可能会非常大,因此不能直接用 int 表示数字,而是用 cha ...

  9. CCF CSP 201512-3 画图

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201512-3 画图 问题描述 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ...

  10. bzoj 4551

    4551 思路: 乱搞: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...