hdu2121-Ice_cream’s world II
给出一个有向图,求最小树形图和它的最小的根。
分析
这个题又写了一晚上~我之前的朱刘算法写法是我乱想的,只有那道题可以过……所以去找了一份代码来看,发现我的写法超级麻烦啊,所以就学习了一下那种写法,非常漂亮,但是有一些判断要考虑。
这是一个不定根问题,经典解决方法是添加一个超级根,向每个点连一条长度大于所有边权和的边,设它为\(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的更多相关文章
- hdu2121 Ice_cream's world II
hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...
- 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 ...
- hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...
- hdu2121 Ice_cream’s world II 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- HDU2121 Ice_cream’s world II (最小树形图)
在建图的时候对原图进行加边 建立一个超级源点~ #include<cstdio> #include<algorithm> #include<cstring> usi ...
- 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 ...
- 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 ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
- HDU 2121 Ice_cream’s world II 最小树形图 模板
开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- 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 ...
随机推荐
- 成都Uber优步司机奖励政策(1月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- SpringCloud Eureka 服务注册与服务发现
一.Eureka简介 spring Cloud Netflix技术栈中,Eureka作为服务注册中心对整个微服务架构起着最核心的整合作用.有了服务发现与注册,你就不需要整天改服务调用的配置文件了,你只 ...
- hive 优化
参考: http://www.csdn.net/article/2015-01-13/2823530 http://www.cnblogs.com/smartloli/p/4288493.html h ...
- 「LeetCode」0002-Longest Substring Without Repeating Characters(C++)
分析 贪心思想.注意更新每次判断的最长不同子串的左区间的时候,它是必须单调增的(有时候会在这里翻车). 代码 关掉流同步能有效提高速度. static const auto io_sync_off = ...
- linux基础——文件挂载,lamp安装
一. 文件挂载 lsblk -f 显示文件系统信息 mount -t vfat UUID="ffffffffff" /mnt 挂载到/mnt目录 Linux针对于各式U盘挂载方 ...
- Linux命令应用大词典-第24章 任务计划
24.1 contab:针对个人用户维护crontab文件
- TPO-15 C2 Performance on a biology exam
TPO-15 C2 Performance on a biology exam 第 1 段 1.Listen to part of a conversation between a Student a ...
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- post接口_form表单上传
上传文件的本质是浏览器读取本地文件的内容,以二进制数据方式传输到服务端,服务端新建一个文件,将获取到的数据复制到文件中 LR中上传操作可以通过web_submit_data函数实现,支持录制要点:we ...
- 在页面使用echarts的地图(解决地图不完整)
测试环境:IDEA+Tomcat7 谷歌浏览器 创建好web工程,编写jsp页面,在自己编写的JSP页面上导包 现在echarts停止了在其网站上下载地图脚本,直接通过src引用网站上的china.j ...