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

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

先建一个超级根,它连向所有点,边权为所有边的边权和加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. angular select ng-change实时获取value

    <select ng-model="vm.selectVal" ng-options="o.id as o.title for o in vm.optionsDat ...

  2. aarch64_g2

    ghc-cryptonite-devel-0.21-1.fc26.aarch64.rpm 2017-02-28 01:28 3.1M fedora Mirroring Project ghc-css- ...

  3. IDL界面程序直接调用envi菜单对应功能

    参考自http://blog.sina.com.cn/s/blog_764b1e9d010115qu.html 参考文章的方法是构建一个button控件,通过单击实现,这种方法比较复杂,不是我们经常能 ...

  4. 五、springcloud之客户端负载均衡Ribbon

    一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...

  5. Kaggle案例分析1--Bestbuy

    1. 引言 Kaggle是一个进行数据挖掘和数据分析在线竞赛网站, 成立于2010年. 与Kaggle合作的公司可以提供一个数据+一个问题, 再加上适当的奖励, Kaggle上的计算机科学家和数据科学 ...

  6. java基础45 IO流技术(输入字符流/缓冲输入字符流)

    一.输入字符流 1.1.输入字符流体系 ------| Reader:输入字符流的基类(抽象类)  ----------| FileReader:向指定文件读取数据的输入字符流(把硬盘上的数据读取到程 ...

  7. 基于gRpc的远程服务框架

    作为一个新搭建的软件团队,底层技术尤为重要.为了以后更好的面向不同的项目需求,满足不断变化的需求,决定着手搭建一套RPC系统.为了更好的兼容以后部门其他语言的使用,选择了开源框架gRpc. gRpc ...

  8. 洛谷P1342请柬

    传送门啦 核心思想:两遍最短路. 1号点去各地的时间直接套最短路模板,各地到1号点时间用逆向思维,视为求1号点沿反边到各地的时间即可. #include <iostream> #inclu ...

  9. Flask form

    用户登录 #!/usr/bin/env python # -*- coding:utf- -*- from flask import Flask, render_template, request, ...

  10. 配置toad远程连接oracle

    在oracle服务器上: C:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN目录 文件:listener.ora(10.144.118 ...