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 ...
随机推荐
- cocos2d-x ndk adt mac 路径配置
export PATH=/bin:/sbin:/usr/local/mysql/bin export PATH=$PATH:/Applications/MacVim-snapshot-68 expor ...
- mybatis 打印SQL语句
在log4j文件中配置 log4j.rootLogger=DEBUG log4j.logger.com.ibatis=DEBUG log4j.logger.org.mybatis=DEBUG
- scu - 3254 - Rain and Fgj(最小点权割)
题意:N个点.M条边(2 <= N <= 1000 , 0 <= M <= 10^5),每一个点有个权值W(0 <= W <= 10^5),现要去除一些点(不能去掉 ...
- 同时显示多个 Notification
主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- 高性能 TCP & UDP 通信框架 HP-Socket v3.2.2 正式公布
HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...
- Unity3D在一建筑GL材料可以改变颜色和显示样本
void CreateLineMaterial() { if (!mat) { mat = new Material("Sha ...
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- Google Maps Android API v2 (1)- 入门
才可以开始工作的API,你将需要下载的API,并确保你有一个谷歌地图Android的API V2关键.API和关键是免费提供的. 概观 获得谷歌地图Android的API V2 谷歌地图API密钥 显 ...
- lambda Join /Group by/ Contains
1.Join Contains