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

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

先建一个超级根,它连向所有点,边权为所有边的边权和加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. 全网最全JS正则表达式 校验数字

    Js代码 <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+ ...

  2. Flip Bits

    Determine the number of bits required to flip if you want to convert integer n to integer m. Notice ...

  3. 使用Netcat进行攻击

    https://www.freebuf.com/column/135007.html 在网上找到了一个开启了ftp服务的服务: http://static.vhdong.com/Upload/Temp ...

  4. Linux多线程的使用一:互斥锁

    多线程经常会在Linux的开发中用到,我想把平时的使用和思考记录下来,一是给自己做个备忘,二是分享给可能会用到的人. POSIX标准下互斥锁是pthread_mutex_t,与之相关的函数有: 1 i ...

  5. 【C语言学习笔记】字符串拼接的3种方法 .

    昨天晚上和@buptpatriot讨论函数返回指针(malloc生成的)的问题,提到字符串拼接,做个总结. #include<stdio.h> #include<stdlib.h&g ...

  6. Gradle教程链接

    Gradle教程:https://www.yiibai.com/gradle/ https://www.cnblogs.com/wxishang1991/p/5532006.html

  7. http://s22.app1105796624.qqopenapp.com/

    http://s22.app1105796624.qqopenapp.com/ http://121.43.114.69/xiyou/app/js/ac_tx.js http://hiyouba.co ...

  8. Mac ssh

    mac的终端默认在打开一个新的tab/window的时候需要重新输入ssh的密码, 很不方便.本文完成在mac中设置,实现secureCRT/xshell里的克隆会话功能, 即新开一个terminal ...

  9. wpf 在Popup内的TextBox 输入法 不能切换

    切换输入法 输入不了中文 [DllImport("User32.dll")] public static extern IntPtr SetFocus(IntPtr hWnd); ...

  10. Unix IPC之Posix消息队列(1)

    部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...