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 ...
随机推荐
- PDF数据防扩散系统方案
在企业信息化过程中.大量的企业重要图纸和资料都是以电子文件的方式存在.为了避免内部关键数据的外泄,採取了多种方式:设计部门的门禁管制.防火墙.禁止计算机的USB接口等等. 可是泄密问题还是时有发生,原 ...
- bootstrap jQuery Ztree异步载入数据,check选择&可加入、改动、删除节点
效果图: 一.下载zTree插件 地址:http://www.ztree.me 二.html代码 <link href="../Scripts/zTree/css/zTreeStyle ...
- EF 分离实体
具体步骤: 新建测试项目:如图 其中Respository和Model层均为类库项目, 1.在Respository层添加ADO.NET实体数据模型, 2.复制Model.tt文件到Model层,这是 ...
- java去全半角空格,trim(), replaceAll(" +",""),replaceAll("\\s*", ""), replaceAll(" | ", "")
JAVA中去掉空格 . String.trim() trim()是去掉首尾空格 .str.replace(" ", ""); 去掉所有空格,包括首尾.中间 St ...
- java它们的定义ArrayList序列, 大神跳跃
一个list有两种类型的对象,今天有需求必须责令不同的约会对象,这里是代码 /** *@author xh1991101@163.com */ List<Message> messages ...
- lightoj1030(期望dp)
有n个格子,初始的时候pos=1,然后丢骰子,然后新的pos为pos+骰子的点数,走到新的pos,可以捡走该pos上的黄金. 特殊的是,如果新的pos超过了n,那么是不会走的,要重新丢骰子. 所以要分 ...
- [Network]Application Layer
1 Principles of Network Applications 1.1 Application Architectures Client-Server Peer-to-Peer Hybird ...
- oracle转Mysql中,varchar2(10)和number应该转换为什么类型? (转)
一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...
- 深度分析 Java 的 ClassLoader 机制(源码级别)(转)
写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoa ...
- BeagleBone Black 板第三课:Debian7.5系统安装和远程控制BBB板
BBB板第三课:Debian7.5系统安装和远程控制BBB板 由于BBB板系统是Debian 7.4.据说使用Debian系统能够实现非常多BBB板的无缝连接.能够更好的学习和控制BBB板,所以就决定 ...