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

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

先建一个超级根,它连向所有点,边权为所有边的边权和加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. Python缓存技术,装x新高度。

    一段非常简单代码 普通调用方式 def console1(a, b): print("进入函数") return (a, b) print(console1(3, 'a')) pr ...

  2. 【转】WCF光芒下的Web Service

    WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客 ...

  3. 列表选择Spinner

    1.只用XML配置来显示列表 在res\values中添加一个arrays.xml 1 <?xml version="1.0" encoding="utf-8&qu ...

  4. unity 优秀开源项目

    ihaiu.GUIDRef (查看项目资源使用情况) http://blog.ihaiu.com/unity-GUIDRef Ihaiu.PoolManager (对象池) http://github ...

  5. 转载:Github项目解析(七)-->防止按钮重复点击

    不错的东西,记录下... http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/qq_23547831/article/deta ...

  6. idea中JDK失效

    [问题] 在没有改变任何东西的情况下,突然间IDEA里面所有的代码都标红,无法找到JDK [解决方法] [File]->[Invalidate Caches],然后就好了

  7. vue总结 08状态管理vuex

      状态管理 类 Flux 状态管理的官方实现 由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex:我们有受到 Elm 启发的状态管 ...

  8. HTML5 localStorage、sessionStorage 作用域

    一.localStorage localStorage有效期:永不失效,除非web应用主动删除. localStorage作用域:localStorage的作用域是限定在文档源级别的.文档源通过协议. ...

  9. Centos之压缩和解压缩命令

    常用压缩格式:.zip .gz .bz2 常用压缩格式:.tar.gz  .tar.bz2 zip格式压缩 zip压缩文件名 源文件 压缩文件 zip -r 压缩文件名 源目录 压缩目录 [root@ ...

  10. MongoDB查询用法大全

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...