hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号: pid=2121" target="_blank">hdoj 2121 Ice_cream’s world II
题意:题目是一道躶题,给n个点,m条边的有向图。然后找一个点。到全部点的距离和最小。找出这个点并输入距离。
分析:非常明显是求一个最小树形图,可是没有说根节点。要找跟节点,我们能够虚拟一个节 点 x 。x 到全部节点连边距离为前面全部距离和+1为 dis 。
然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离。
假设图不连通,或者ans》=2*dis 说明不存在,都则与 x 点出发的边就是结果点
AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1100;
const int inf = 0x3f3f3f3f;
int n,m;
struct Node
{
int from,to,dis;
};
vector<Node> e;
int ha[N],vis[N],father[N],in[N];
int Minroot;
int zhuliu(int root)
{
int ans = 0;
while(true)
{
for(int i=0;i<n;i++)
in[i] = inf;
memset(father,-1,sizeof(father));
for(int i=0; i<e.size(); i++) //找最小入边
{
int to = e[i].to;
if(e[i].dis<in[to] && e[i].from!=e[i].to)
{
in[to] = e[i].dis;
father[to] = e[i].from;
if(e[i].from == root) //找最小
Minroot = i;
}
}
for(int i=0;i<n;i++)
{//printf("%d ",in[i]);
if(i!=root && in[i]==inf)
return -1;
}
int cnt = 0;
in[root] = 0;
memset(ha,-1,sizeof(ha));
memset(vis,-1,sizeof(vis));
for(int i=0;i<n;i++) //找自环
{ ans += in[i];
int v = i;
while(v!=root && ha[v]==-1 && vis[v]!=i)
{
vis[v] = i;
v = father[v];
}
if(v!=root && ha[v]==-1)
{
for(int j = father[v];j != v;j=father[j])
{
ha[j] = cnt;
}
ha[v] = cnt++;
}
}
if(cnt == 0) //跳出条件
break;
for(int i=0;i<n;i++)
if(ha[i]==-1)
ha[i]=cnt++;
for(int i = 0; i< e.size();i++)
{
int tmp = e[i].to;
e[i].from = ha[e[i].from];
e[i].to = ha[e[i].to];
if(e[i].from != e[i].to)
e[i].dis -= in[tmp];
}
n = cnt;
root = ha[root];
}
return ans;
}
int main()
{
//freopen("Input.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
int num = 0;
for(int i=0;i<m;i++)
{
int x,y,s;
scanf("%d%d%d",&x,&y,&s);
e.push_back((Node){x,y,s});
num = num + s;
}
num++;
for(int i=0;i<n;i++)
e.push_back((Node){n,i,num});
n++;
int ans = zhuliu(n-1);
//printf("%d %d\n",ans,num);
if(ans==-1||ans>=2*num)
puts("impossible");
else
printf("%d %d\n",ans-num,Minroot-m);
puts("");
e.clear();
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】的更多相关文章
- HDU 2121 Ice_cream’s world II 最小树形图 模板
开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- 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 ...
- 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 ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
- 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 ...
随机推荐
- 阅读zepto.js的core中的Core methods
学习zepto.js,參考资料:http://www.zeptojs.cn/ 跟jQuery一样.其选择符号也是$; 首先接触的是 $.() 选择 $(selector, [context]) ⇒ ...
- 《JavaScript设计模式与开发实践》读书笔记之单例模式
1.单例模式 保证一个类仅有一个实例,并提供一个访问它的全局访问点 1.1 传统的单例模式 var Singleton=function(name){ this.name=name; } Single ...
- 提高SQL执行效率
原文地址:http://www.cnblogs.com/hlxs/archive/2012/05/07/2487082.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ...
- Android SDK 5.0 这个语句带来折腾 - 生命在于折腾!
Android SDK 5.0 带来的这番折腾 - 生命在于折腾! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一 ...
- 深入浅出KnockoutJS
深入浅出KnockoutJS 写在前面,本文资料大多来源网上,属于自己的学习笔记整理. 其中主要内容来自learn.knockoutjs.com,源码解析部分资料来自司徒正美博文<knockou ...
- windows phone (13) 样式继承
原文:windows phone (13) 样式继承 在上一遍文章中已经介绍到可以在Resources集合中定义样式,我们也可以在一个样式上引用其他的样式,这就是继承的概念,使用方法是将引用的样式放置 ...
- HQApi命令行接口配置
执行的命令行前准备 在您的个人文件夹中第一次创建 型材client.properties 如下面 cd C:\Users\scnyli\ mkdir ".hq" 创建一个 clie ...
- Oracle学习(十四):管理用户安全性
--用户(user) SQL> --创建一个名为 grace password是password 的用户,新用户没有不论什么权限 SQL> create user grace identi ...
- 简单ESB的服务架构
简单ESB的服务架构 这几个月一直在修改架构,所以迟迟没有更新博客. 新的架构是一个基于简单esb的服务架构,主要构成是esb服务注册,wcf服务,MVC项目构成. 首先,我门来看一看解决方案, 1. ...
- 【iOS开发-22】navigationBar导航栏,navigationItem建立:获取导航栏中的基本文本和button以及各种跳跃
(1)navigationBar导航栏可以被看作是self.navigationController一个属性导航控制器,它可以由点直接表示self.navigationController.navig ...