给出一个有向图,求最小树形图和它的最小的根。

分析

这个题又写了一晚上~我之前的朱刘算法写法是我乱想的,只有那道题可以过……所以去找了一份代码来看,发现我的写法超级麻烦啊,所以就学习了一下那种写法,非常漂亮,但是有一些判断要考虑。

这是一个不定根问题,经典解决方法是添加一个超级根,向每个点连一条长度大于所有边权和的边,设它为\(sum\),然后以超级根为根求最小树形图。如果最后最小树形图的答案超过了\(2sum\),那么就说明超级根至少连出去两条边,即原图不连通。如果原图连通,那么把\(sum\)减掉即可。

关键在于如何求最小的根。可以发现,如果我们从小到大把超级根到所有点的边加进去,然后在算法过程中从小到大遍历边,如果有多个\(in\)最小且从root过来的,那么我们会找到最小的那个。但是缩点怎么办呢?朱刘算法的这种写法的一个很大的好处就是保留了所有的点,只是改了它们的id,所以我们可以以边为关键搜索,如果找到第\(i\)条边为\((root,v,w)\),它是最小的,那么就把最小根更新为\(i\),那么最终的最小根就是\(i-m\),其中\(m\)为原图上的边数。非常巧妙!

代码

为什么全改giant还比只改用到的快200ms啊。

代码的几个需要注意的地方:

  • 找环的时候不能把已经有id的点再给一次id
  • 在内部找环的时候从上一个开始找
  • 只有当原来的边的两个点不在同一个环中才能减它的边权
  • 前面在处理in的时候当\(u=v\)的时候直接跳过这条边
  • 现在这个写法中的无环条件为col=1
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long giant;
giant read() {
giant x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const giant maxn=2e3+10;
const giant maxm=4e4+10;
const giant inf=1e8+7;
struct edge {
giant u,v,w,nxt;
};
struct graph {
edge e[maxm];
giant h[maxn],tot,in[maxn],from[maxn],id[maxn],tic[maxn],n,real,vis[maxn];
void clear(giant _n) {
memset(h,0,sizeof h),tot=0,n=_n;
}
void add(giant u,giant v,giant w) {
e[++tot]=(edge){u,v,w,h[u]};
h[u]=tot;
}
giant MTG(giant root) {
giant ret=0;
while (true) {
fill(in+1,in+n+1,inf);
memset(id,0,sizeof id);
memset(from,0,sizeof from);
memset(vis,0,sizeof vis);
for (giant i=1;i<=tot;++i) if (e[i].u!=e[i].v && in[e[i].v]>e[i].w) {
in[e[i].v]=e[i].w,from[e[i].v]=e[i].u;
if (e[i].u==root) real=i;
}
giant col=1,j;
for (giant i=1;i<=n;++i) if (i!=root) {
ret+=in[i];
for (j=i;j!=root && !id[j];j=from[j]) if (vis[j]!=i) vis[j]=i; else break;
if (j!=root && !id[j]) {
for (giant k=from[j];k!=j;k=from[k]) id[k]=col;
id[j]=col++;
}
}
if (col==1) break;
for (giant i=1;i<=n;++i) if (!id[i]) id[i]=col++;
for (giant i=1;i<=tot;++i) {
giant u=e[i].u,v=e[i].v;
e[i].u=id[u],e[i].v=id[v];
if (e[i].u!=e[i].v) e[i].w-=in[v];
}
root=id[root],n=col-1;
}
return ret;
}
} A;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
giant n,m;
while (~scanf("%lld%lld",&n,&m)) {
A.clear(n+1);
giant sum=1;
for (giant i=1;i<=m;++i) {
giant u=read()+1,v=read()+1,w=read();
A.add(u,v,w);
sum+=w;
}
for (giant i=1;i<=n;++i) A.add(n+1,i,sum);
giant ans=A.MTG(n+1);
if ((ans-=sum)>=sum) puts("impossible\n"); else
printf("%lld %lld\n\n",ans,A.real-m-1);
}
}

hdu2121-Ice_cream’s world II的更多相关文章

  1. hdu2121 Ice_cream's world II

    hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...

  2. HDU2121 Ice_cream’s world II —— 最小树形图 + 不定根 + 超级点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream’s world II Time Limit: 3000/1000 MS (J ...

  3. hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...

  4. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

  5. HDU2121 Ice_cream’s world II (最小树形图)

    在建图的时候对原图进行加边 建立一个超级源点~ #include<cstdio> #include<algorithm> #include<cstring> usi ...

  6. HDU2121:Ice_cream’s world II (虚根+有向图最小生成树)

    Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  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. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

    称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...

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

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

  10. 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 ...

随机推荐

  1. Java:xxx is not an enclosing class

    1. 错误原因 该错误一般出现在对内部类进行实例化时,例如 public class A{ public class B{ } } 此时B是A的内部类,如果我们要使用如下语句实例化一个B类的对象: A ...

  2. SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

    添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...

  3. CentOS 7.2安装11g数据库软件

      Preface       Yesterday I've installed the 11g GI software on CentOS 7.2.But I still encounter som ...

  4. Selenium 入门到精通系列:一

    Selenium 入门到精通系列 PS:控制浏览器窗口大小.前进.后退.刷新 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 20 ...

  5. Siki_Unity_2-1_API常用方法和类详细讲解(下)

    Unity 2-1 API常用方法和类详细讲解(下) 任务101&102:射线检测 射线origin + direction:射线检测:射线是否碰撞到物体 (物体需要有碰撞器),碰撞物体的信息 ...

  6. Python3中@的作用

    可能是自己理解能力差,网上看了一大堆教程,完全没搞懂. 自己敲几行代码,终于理解是怎么回事了. #python 3.6 #!/usr/bin/env python # -*- coding:utf-8 ...

  7. [HNOI2017]大佬

    参考题解 \(\text{Solution}\) 我们发现5个行为中2操作与其它操作无关,所以我们采用贪心,尽量让多的时间去攻击大佬. 设 \(f[i][j]\) 表示前 \(i\) 天剩 \(j\) ...

  8. 您的下个中文网站可以使用的5个高质量中文Webfont

    你有没有考虑为什么中文网站的版式风格不像大多数现代英文网站那样丰富?您想了解如何让您的下一个中文网站项目更吸引用户的眼球么?继续往下读吧…… 根据Smashing Magazine进行的一项调查显示  ...

  9. [转载]Tensorflow中reduction_indices 的用法

    Tensorflow中reduction_indices 的用法 默认时None 压缩成一维

  10. 基于Kubernetes(k8s)的RabbitMQ 集群

    目前,有很多种基于Kubernetes搭建RabbitMQ集群的解决方案.今天笔者今天将要讨论我们在Fuel CCP项目当中所采用的方式.这种方式加以转变也适用于搭建RabbitMQ集群的一般方法.所 ...