HDU 2121 Ice_cream’s world II 最小树形图 模板
开始学习最小树形图,模板题。
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
【Sample Output】
impossible
【分析】
一道裸的最小树形图模板题,算法思路比较简单,但是写起来感觉很不优美...仅作为模板记录吧。
本题不确定起点,故在读入完数据之后建立一个虚根,把所有的点都与这个虚根相连,边权为所有边边权和+1(保证虚边足够长),最后减掉即可。
确定起点的题可以从模板中删掉这部分。
然后就是这个模板是从0开始存储...强迫症好不爽啊...
详细参见:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU 2121
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ;
const int M = N*N; typedef struct nod
{
int a,b,c;
} node; node edge[M]; int predge[N],id[N],visit[N];//id用来标记圈的
int in[N];//入弧最小的 int ansroot; int dmst(int root,int n,int m)//n表示点数,m表示边数,root表示根
{
int u,v;
int ret=;
while(true)
{
for(int i=;i<n;i++) in[i]=INT_MAX;
for(int i=;i<m;i++)
{
if(edge[i].c<in[edge[i].b]&&edge[i].a!=edge[i].b)
{
predge[edge[i].b]=edge[i].a;//找出每个点的最小入弧
if(edge[i].a==root) ansroot=i;
in[edge[i].b]=edge[i].c;
}
}
for(int i=;i<n;i++)
{
if(i==root) continue;
if(in[i]==INT_MAX) return -;
}
in[root]=;
int cnt=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
for(int i=;i<n;i++)
{
ret+=in[i];//进行缩圈
v=i;
while(visit[v]!=i&&id[v]==-&&v!=root)
{
visit[v]=i;
v=predge[v];
}
if(v!=root&&id[v]==-)
{
for(u=predge[v];u!=v;u=predge[u])
id[u]=cnt;
id[v]=cnt++;
}
}
if (cnt==) break;
for (int i=;i<n;i++)
if (id[i]==-) id[i]=cnt++;
for (int i=;i<m;i++)
{
v=edge[i].b;//进行缩点,重新标记。
edge[i].a=id[edge[i].a];
edge[i].b=id[edge[i].b];
if (edge[i].a!=edge[i].b) edge[i].c-=in[v];
}
n=cnt;
root=id[root];
}
return ret;
}
int main()
{
freopen("2121.txt","r",stdin);
int n,m,m1;
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b;
int r=;
m1=m;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
r+=edge[i].c;
}
r++;
for(int i=;i<n;i++)
{
edge[m].a=n;
edge[m].b=i;
edge[m].c=r;
m++;
}
int ans=dmst(n,n+,m);
ansroot-=m1;//最小根对应的标号为i-m1
if(ans==-||ans>=*r) printf("impossible\n");
else printf("%d %d\n",ans-r,ansroot);
printf("\n");
}
return ;
}
HDU 2121 Ice_cream’s world II 最小树形图 模板的更多相关文章
- HDU 2121 Ice_cream’s world II 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...
- 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 ...
- HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】
Ice_cream’s world II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 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 ...
- hdu 2121 Ice_cream’s world II (无定根最小树形图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...
- HDU - 2121 Ice_cream’s world II 无根最小树形图
HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...
- 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 ...
- hdu2121 Ice_cream’s world II 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
随机推荐
- Mac上安装Tomcat
参考文档:http://blog.csdn.net/j2ee_me/article/details/7928493 0.如果java没有被初始安装,所以你要自己去找Apple的安装程序这里来安装,或者 ...
- CocoaAsyncSocket框架的简单封装
在iOS开发中使用socket(CFNetwork),一般都是用第三方库AsyncSocket. 参考博客:http://my.oschina.net/worldligang/blog/396881? ...
- linux 守护进程创建流程
#include <sys/stat.h> #include <fcntl.h> /* Bit-mask values for 'flags' argument of beco ...
- libevent在windows下使用步骤详解
一 环境 官方下载地址:http://libevent.org/版本:libevent-2.0.22-stable 二 编译静态库 1 解压把上面下载到libevent-2.0.22-stable.t ...
- URL设置问题
URL设置那里删除了<item path="index.aspx" pattern="index.aspx"/>后,访问首页就不出来了,要加上/in ...
- 上海赛趣-top.mainFrame.tabAddHandler方法详解
top.mainFrame.tabAddHandler("item"+Id,'项目:'+itemname,'<%=basePath%>bizitem/goEditIte ...
- 无线手柄+步进电机——控制方向
今天测试了一下无线手柄控制电机转向的改变 1: #include <PS2X_lib.h> //for v1.6 2: #include <Stepper.h> 3: 4: ...
- push以及pop,shift,unshift
压入数组:往数组后面加:push arr.push()返回值为添加后数组的长度 往数组前面加:unshift arr.unshift()返回值为添加后数组的长度 拿出数组:拿掉 ...
- Box2d FilterData
Box2D.Dynamics.b2ContactFilter类,碰撞源码: public virtual function ShouldCollide(fixtureA:b2Fixture, fixt ...
- play1.x vs play2.x 对比(转)
个人看到对比play1.x和play2.x比较的文章中,写的最深入,最清晰的一个.转自:http://freewind.me/blog/20120728/965.html 为了方便群中的Play初学者 ...